-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinkedList.java
252 lines (195 loc) · 4.69 KB
/
SinglyLinkedList.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package projectCode20280;
import java.util.Iterator;
public class SinglyLinkedList<E> implements List<E> {
public SinglyLinkedList<E>.Node<E> head;
public int size=0;
private class Node<E> {
private E element;
private Node<E> next;
public Node(E e, Node<E> n)
{
element = e;
next = n;
}
public E getElement() {
return element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> n) {
this.next = n;
} //end of nested node class
}
@Override
public boolean isEmpty() { //returns true if the list is empty, false otherwise
if(this.head == null) {
return true;
}
else
return false;
}
@Override
public E get(int i) { //returns the indexed element
if(this.head!=null&&i>=0){
int count=0;
Node<E> p=this.head;
while(p!=null&&count<i){
p=p.next;
count++;
}
if(p!=null){
return p.element;
}
}
return null;
}
@Override
public void add(int i, E e) { //adds an element to anywhere in the list
Node<E> newest = new Node<>(e, null);
Node<E> prev= head;
int count = 1;
while(count < i){
prev = prev.next;
count++;
}
Node<E> current = prev.next;
newest.next = current;
prev.next = newest;
}
@Override
public E remove(int i) { //removes elements from the list
Node<E> curr=head;
Node<E> ret=new Node<E>(null,null);
if(head==null)
return null;
if(i==0) //base case
{
ret=head;
head=curr.next;
return ret.getElement();
}
else {
for(int j=0;j<i-1;j++) //removes the ith element
{
curr=curr.next;
ret=curr.next;
}
if(curr == null )
{
return null;
}
Node<E> next=curr.next.next;
curr.next=next;
return ret.getElement();
}
}
@Override
public Iterator<E> iterator() { //iterates through the list
return new ListIterator();
}
private class ListIterator implements Iterator<E> { //iterates through the list from first to last
Node<E> curr;
public ListIterator() {
curr = head;
}
public boolean hasNext() {
return curr != null;
}
@Override
public E next() {
E ret = (E) curr.getElement();
curr = curr.getNext();
return ret;
}
}
@Override
public int size() {
int length=0;
Node<E> p=head;
while (p!=null){
length++;
p=p.next;
}
return length;
}
@Override
public E removeFirst() { //2nd element becomes the new head
Node<E> top=head;
head=top.next;
return top.getElement();
}
@Override
public E removeLast() { //2nd last element becomes the last
if(head==null)
return null;
Node <E> last = this.head;
while(last.next.next != null) {
last=last.next;
}
Node<E> ret=last.next;
last.next=null;
return ret.getElement();
}
@Override
public void addFirst(E e) {
head= new Node<E>(e, head);
size++;
}
@Override
public void addLast(E e) { //adds the element to the end of the list
if(head==null)
head=new Node<E>(e,head);
else {
Node<E> newest=head;
while(newest.next != null) {
newest=newest.next;
}
newest.next=new Node<E>(e,newest.next);
}
}
public E first() { //returns first element
if(head==null)
return null;
return head.getElement();
}
public E last() { //returns last element
if(head==null)
return null;
Node <E> last = head;
while(last.next != null)
last=last.next;
return last.getElement();
}
public String toString() //This function acts a "toString" for objects.
{
Node<E> node = head;
String s1= "[";
while (node != null){
s1+=node.getElement();
node = node.next;
if(node != null)
s1+=", ";
}
s1+="]";
return s1;
}
public static void main(String[] args) {
String[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
SinglyLinkedList<String> sll = new SinglyLinkedList<String>();
for (String s : alphabet) {
sll.addFirst(s);
sll.addLast(s);
}
System.out.println(sll.toString());
sll.removeFirst();
System.out.println(sll.toString());
sll.removeLast();
System.out.println(sll.toString());
sll.remove(2);
System.out.println(sll.toString());
for (String s : sll) {
System.out.print(s + ", ");
}
}
}