-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyLinkedList.java
184 lines (175 loc) · 3.61 KB
/
MyLinkedList.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
public class MyLinkedList<E> extends MyAbstractList<E> {
private Node<E> head, tail;
public MyLinkedList() {
}
public MyLinkedList(E[] objects) {
super(objects);
}
public E getFirst() {
if (size == 0) {
return null;
}
else {
return head.element;
}
}
public E getLast() {
if (size == 0) {
return null;
}
else {
return tail.element;
}
}
public void addFirst(E e) {
Node<E> newNode = new Node<>(e);
newNode.next = head;
head = newNode;
size++;
if (tail == null)
tail = head;
}
public void addLast(E e) {
Node<E> newNode = new Node<>(e);
if (tail == null) {
head = tail = newNode;
}
else {
tail.next = newNode;
tail = tail.next;
}
size++;
}
@Override
public void add(int index, E e) {
if (index == 0) addFirst(e);
else if (index >= size) addLast(e);
else {
Node<E> current = head;
for (int i = 1; i < index; i++)
current = current.next;
Node<E> temp = current.next;
current.next = new Node<E>(e);
(current.next).next = temp;
size++;
}
}
public E removeFirst(int index) {
if (size == 0) return null;
else {
Node<E> temp = head;
head = head.next;
size--;
if (head == null) tail = null;
return temp.element;
}
}
public E removeLast() {
if (size == 0) return null; // Nothing to remove
else if (size == 1) { // Only one element in the list
Node<E> temp = head;
head = tail = null; // list becomes empty
size = 0;
return temp.element;
}
else {
Node<E> current = head;
for (int i = 0; i < size - 2; i++)
current = current.next;
Node<E> temp = tail;
tail = current;
tail.next = null;
size--;
return temp.element;
}
}
public E remove(int index) {
if (index < 0 || index >= size) return null;
else if (index == 0) return removeFirst(index);
else if (index == size - 1) return removeLast();
else {
Node<E> previous = head;
for (int i = 1; i < index; i++) {
previous = previous.next;
}
Node<E> current = previous.next;
previous.next = current.next;
size--;
return current.element;
}
}
public String toString() {
StringBuilder result = new StringBuilder("[");
Node<E> current = head;
for (int i = 0; i < size; i++) {
result.append(current.element);
current = current.next;
if (current != null) {
result.append(", "); // Separate two elements with a comma
}
else {
result.append("]"); // Insert the closing ] in the string
}
}
return result.toString();
}
@Override
public void clear() {
size = 0;
head = tail = null;
}
@Override
public boolean contains(E e) {
System.out.println("Implementation left as an exercise");
return true;
}
@Override
public E get(int index) {
System.out.println("Implementation left as an exercise");
return null;
}
@Override
public int indexOf(E e) {
System.out.println("Implementation left as an exercise");
return 0;
}
@Override
public int lastIndexOf(E e) {
System.out.println("Implementation left as an exercise");
return 0;
}
@Override
public E set(int index, E e) {
System.out.println("Implementation left as an exercise");
return null;
}
@Override
public java.util.Iterator<E> iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator
implements java.util.Iterator<E> {
private Node<E> current = head;
@Override
public boolean hasNext() {
return (current != null);
}
@Override
public E next() {
E e = current.element;
current = current.next;
return e;
}
@Override
public void remove() {
System.out.println("Implementation left as an exercise");
}
}
private static class Node<E> {
E element;
Node<E> next;
public Node(E element) {
this.element = element;
}
}
}