-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyNode.java
158 lines (129 loc) · 4.02 KB
/
MyNode.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
151
152
153
154
155
156
157
158
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) {
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.getNext() != null) {
myNode = myNode.getNext();
if (newNode.equals(myNode)) {
return true;
}
}
if (newNode.equals(firstNode)) {
return true;
}
}
return false;
}
public int indexOf(T elem) {
int index = 0;
MyNode<T> myNode = firstNode;
for(int i = 0; i < size; i++) {
if (!(myNode.getValue().equals(elem))) {
index++;
if (myNode.getNext() != null) {
myNode = myNode.getNext();
}
}
}
if (index == size ) { index = -1; }
return index;
}
public int countRepetition(T elem) {
int count = 0;
MyNode<T> node = new MyNode<T>(elem);
MyNode<T> myNode = firstNode;
while (myNode.getNext() != null) {
myNode = myNode.getNext();
if (node.equals(myNode)) {
count++;
}
}
if (node.equals(firstNode)) {
count++;
}
return count;
}
public boolean hasDuplicates() {
MyListImpl<T> duplicates = listDuplicates();
return listDuplicates().size() > 0;
}
public MyListImpl<T> listDuplicates() {
MyListImpl<T> duplicates = new MyListImpl<T>();
if(size > 0) {
MyNode<T> myNode = firstNode;
int count = countRepetition(firstNode.getValue());
if (count >= 2) {
duplicates.add(firstNode.getValue());
}
while (myNode.getNext() != null) {
myNode = myNode.getNext();
count = countRepetition(myNode.getValue());
if (count >= 2 && !(duplicates.contains(myNode.getValue()))) {
duplicates.add(myNode.getValue());
}
}
}
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?
// show duplicates & boolean containsDuplicates
// return list [start, end]
}