-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularlyLinkedList.java
230 lines (216 loc) · 6.75 KB
/
CircularlyLinkedList.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
public class CircularlyLinkedList<C> {
// Instance Variables
private CircularlyLinkedNode<C> head, tail;
private int size;
//constructor takes no arguments
public void CircularlyLinkedNode()
{
head = null;
tail = null;
size = 0;
}
public void addFirst(C c)
{
CircularlyLinkedNode CircularlyLinkedNode = new CircularlyLinkedNode(c, null);
if (tail == null) // if this is the very first node added it also has to become the tail
{
tail = CircularlyLinkedNode;
}
CircularlyLinkedNode.setNext(head); // set next to be the current head
tail.setNext(CircularlyLinkedNode);
head = CircularlyLinkedNode; // make head the node we were passed
size++; // size increases by one
}
public void addLast(C c)
{
CircularlyLinkedNode CircularlyLinkedNode = new CircularlyLinkedNode(c, null);
CircularlyLinkedNode.setNext(head); //last node points to nothing
tail.setNext(CircularlyLinkedNode); // give current tail a next
tail = CircularlyLinkedNode; // new tail is the node we were passed
size++; // size increases by one
}
public C first()
{
return head.getData();
}
public CircularlyLinkedNode<C> getLast()
{
return tail;
}
public boolean isEmpty()
{
return size==0;
}
public C last()
{
return tail.getData();
}
public C removeFirst()
{
if (isEmpty()) //cant remove first
{
System.out.println("List is already empty!");
return null;
}
CircularlyLinkedNode<C> temp = head; //save current head
if (head == tail)
{
head=null;
tail=null;
}
head = head.getNext(); // head becomes 2nd in the list
tail.setNext(head);
temp.setNext(null);
size--; //reduce size by one
return temp.getData(); //return what was the head of the list
}
public int size()
{
return size;
}
public String toString()
{
String s = "Circularly Linked List contains: ";
if (isEmpty()) // size=0
{
s+=("Nothing!");
return s;
}
CircularlyLinkedNode<C> curr = head;
for (int i=0; i<size; i++)
{
s+=curr.getData().toString()+" ";
curr=curr.getNext();
}
return s;
}
public C get(int n)
{
if (isEmpty()) //cant get from empty list
{
System.out.println("List is empty!");
return null;
}
CircularlyLinkedNode<C> curr = head;
for (int i=0; i<n; i++)
{
curr=curr.getNext();
}
return curr.getData();
}
public C removeLast()
{
CircularlyLinkedNode<C> nodeBefore;
CircularlyLinkedNode<C> nodeToRemove;
if (isEmpty()) // list is empty, cant remove last
{
System.out.println("List is already empty!");
return null;
}
nodeBefore = head;
for (int i=0; i<size-2; i++) //loop through to find second last node
{
nodeBefore = nodeBefore.getNext();
}
nodeToRemove = tail;
if (head == tail)
{
head=null;
tail=null;
}
nodeBefore.setNext(head); //second last node now points to null
tail = nodeBefore; //tail is set to be second last node
size--; //size reduced by 1
return nodeToRemove.getData(); //return the original tail
}
public C remove(int index)
{
if(index == 0)
{
return removeFirst();
}
if(index == size-1)
{
return removeLast();
}
if(index <0||index>=size)
{
System.out.println("Index is out of bounds");
return null;
}
CircularlyLinkedNode<C> nodeBefore;
CircularlyLinkedNode<C> nodeToRemove;
nodeBefore = head;
for (int i=0; i<index-1; i++) //loop through to find node before the index
{
nodeBefore = nodeBefore.getNext();
}
nodeToRemove = nodeBefore.getNext();
nodeBefore.setNext(nodeToRemove.getNext()); //node before's next becomes node removed's next
size--; //size reduced by 1
return nodeToRemove.getData(); //return the removed node
}
public void add(int index, C c) //precondition: index must be greater than 0 (use addFirst otherwise)
{
CircularlyLinkedNode nodeToAdd = new CircularlyLinkedNode(c, null);
if (index == 0) // use addFirst in this case
{
addFirst(c);
return;
}
if (index >= size || index<0) // use addLast in this case or index out of bounds
{
System.out.println("Index is out of bounds");
return;
}
if(index==size-1)
{
addLast(c);
return;
}
CircularlyLinkedNode<C> nodeBefore;
nodeBefore = head;
for (int i=0; i<index-1; i++) //loop through to find node before the index
{
nodeBefore = nodeBefore.getNext();
}
nodeToAdd.setNext(nodeBefore.getNext());
nodeBefore.setNext(nodeToAdd);
size++; //size reduced by 1
}
public static void main (String[] args)
{
CircularlyLinkedList<Integer> ll = new CircularlyLinkedList<Integer>();
System.out.println(ll.toString());
ll.addFirst(0);
ll.addFirst(1);
ll.addFirst(3);
ll.addFirst(4);
ll.addFirst(5);
ll.add(3, 2);
System.out.println(ll.toString());
ll.addFirst(-100);
ll.addLast(100);
System.out.println(ll.toString());
ll.removeFirst();
ll.removeLast();
System.out.println(ll.toString());
//Removes the item in the specified index
ll.remove(2);
System.out.println(ll.toString());
ll.removeFirst();
System.out.println(ll.toString());
ll.removeLast();
System.out.println(ll.toString());
ll.removeFirst();
System.out.println(ll.toString());
ll.addFirst(9999);
ll.addFirst(8888);
ll.addFirst(7777);
System.out.println(ll.toString());
System.out.println(ll.get(0).toString());
System.out.println(ll.get(1).toString());
System.out.println(ll.get(2).toString());
System.out.println(ll.toString());
}
}