-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyListImpl.java
150 lines (121 loc) · 3.78 KB
/
MyListImpl.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
public class MyListImpl<T> implements MyList<T> {
private MyNode<T> firstNode;
private MyNode<T> lastNode;
private int size = 0;
public MyListImpl() {}
public int size() {
return size;
}
public void add(T elem) {
MyNode<T> newNode = new MyNode<T>(elem);
if (size == 0) {
firstNode = newNode;
} else {
MyNode<T> prevNode = lastNode;
prevNode.setNext(newNode);
}
lastNode = newNode;
size++;
}
public MyNode<T> get(int index) {
MyNode<T> myNode = null;
if (index >= 0 && index < size) {
myNode = firstNode;
for (int i = 0 ; i < index; i++) {
myNode = myNode.getNext();
}
}
return myNode;
}
public boolean isEmpty() {
return size == 0;
}
public void remove(int index) {
if (index >= 0 && index < size) {
//remove node's value
get(index).setValue(null);
// if node to be removed has a previous node and a node afterwards, fill the gap
if (hasPrevious(index) && hasNext(index)) {
MyNode<T> prevNode = get(index - 1);
MyNode<T> nextNode = get(index + 1);
prevNode.setNext(nextNode);
} else {
// if node to be removed has index 0 and a node afterwards, set that node as the first node
if (hasNext(index)) {
firstNode = get(1);
}
}
size--;
}
}
public boolean contains(T elem) {
if (size > 0 ) {
MyNode<T> newNode = new MyNode<T>(elem);
MyNode<T> myNode = firstNode;
while (myNode != null) {
if (newNode.equals(myNode)) {
return true;
}
myNode = myNode.getNext();
}
}
return false;
}
public int indexOf(T elem) {
if (contains(elem)) {
int index = 0;
MyNode<T> myNode = firstNode;
for (int i = 0; i < size; i++) {
while (!(myNode.getValue().equals(elem))) {
index++;
myNode = myNode.getNext();
}
}
return index;
}
//if elem not in a list, return -1
return -1;
}
public int countRepetition(T elem) {
int count = 0;
MyNode<T> node = new MyNode<T>(elem);
MyNode<T> myNode = firstNode;
while (myNode != null) {
if (node.equals(myNode)) {
count++;
}
myNode = myNode.getNext();
}
return count;
}
public boolean hasDuplicates() {
return listDuplicates().size() > 0;
}
public MyListImpl<T> listDuplicates() {
MyListImpl<T> duplicates = new MyListImpl<T>();
if( size > 0 ) {
MyNode<T> myNode = firstNode;
while (myNode != null) {
int count = countRepetition(myNode.getValue());
boolean nodeHasADuplicate = count >= 2;
boolean nodeHasNotBeenChecked = !(duplicates.contains(myNode.getValue()));
if (nodeHasADuplicate && nodeHasNotBeenChecked) {
duplicates.add(myNode.getValue());
}
myNode = myNode.getNext();
}
}
return duplicates;
}
private boolean hasPrevious(int index) {
return get(index - 1) != null;
}
private boolean hasNext(int index) {
return get(index + 1) != null;
}
//TODO more methods to add
// reverse order
// filtering if int by number?
// sort if String by alphabet?
// return list [start, end]
}