-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.java
110 lines (103 loc) · 3.23 KB
/
List.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
public class List<T> {
public int length;
public Node<T> head;
public List(){
this.length = 0;
this.head = null;
}
public String toString(){
String tempString = "";
if(this.length == 0 || this.head == null){
return tempString;
}
tempString += this.head.toString();
for(Node<T> tempNode = this.head.next; tempNode != null; tempNode = tempNode.next){
tempString += "," + tempNode.toString();
}
return tempString;
}
public void append(T val){
Node<T> insertNode = new Node<T>(val);
if(this.length == 0 && this.head == null){
this.head = insertNode;
this.length += 1;
}
else {
Node<T> nodePtr = this.head;
while(nodePtr.next != null){
nodePtr = nodePtr.next;
}
nodePtr.next = insertNode;
this.length += 1;
}
}
public boolean remove(T val){
Node<T> previousNode = null;
Node<T> nodePtr = this.head;
if(this.head == null || this.length == 0){
return false;
}
while(nodePtr != null && nodePtr.data.equals(val) == false){
previousNode = nodePtr;
nodePtr = nodePtr.next;
}
if(previousNode == null && this.head.data.equals(val) == true){
this.head = this.head.next;
this.length -= 1;
return true;
}
else if(nodePtr != null && previousNode != null && nodePtr.data.equals(val) == true){
previousNode.next = nodePtr.next;
this.length -= 1;
return true;
}
return false;
}
public boolean remove(List<T> val){
int counter = 0;
Node<T> tempCurrent = val.head;
while(tempCurrent != null){
boolean removed = this.remove(tempCurrent.data);
if(removed == true){
counter += 1;
}
tempCurrent = tempCurrent.next;
}
if(counter >= 1){
return true;
}
return false;
}
public boolean contains(T search){
if(this.head != null){
Node<T> tempListHead = this.head;
while(tempListHead != null){
if(tempListHead.data.equals(search) == true){
return true;
}
tempListHead = tempListHead.next;
}
}
return false;
}
public boolean equals(List<T> other){
if(this.length != other.length){
return false;
}
int counter = 0;
Node<T> tempCurrent = other.head;
Node<T> tempCurrent2 = this.head;
while(tempCurrent != null && tempCurrent2 != null){
if(this.contains(tempCurrent.data) == false || tempCurrent.data.equals(tempCurrent2.data) == false){
return false;
}
counter += 1;
tempCurrent = tempCurrent.next;
tempCurrent2 = tempCurrent2.next;
}
if(counter != this.length){
return false;
}
return true;
}
}