-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularlyLinkedList.java
249 lines (201 loc) · 4.39 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package projectCode20280;
import java.util.Iterator;
/*
* Circularly linkedList class is very similar to the single linked list class
* with the addition of a rotate method.
*/
public class CircularlyLinkedList<E> implements List<E> {
private CircularlyLinkedList<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() {
if(this.head == null) {
return true;
}
else
return false;
}
@Override
public E get(int i) {
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) {
if(e==null){
return;
}
if (this.head==null||i<=1){
this.head = new Node<E>(e, this.head);
}else {
int count=0;
Node<E> front=this.head;
while (front.next!=null&&count<i-1){
front=front.next;
count++;
}
front.next=new Node<E>(e,front.next);
}
}
@Override
public E remove(int i) {
Node<E> curr=head;
Node<E> ret=new Node<E>(null,null);
if(head==null)
return null;
if(i==0)
{
ret=head;
head=curr.next;
return ret.getElement();
}
else {
for(int j=0;j<i-1;j++)
{
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() {
return new ListIterator();
}
private class ListIterator implements Iterator<E> {
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() {
Node<E> top=head;
head=top.next;
return top.getElement();
}
@Override
public E removeLast() {
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) {
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 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;
}
//This method makes the 2nd element the head and the first the last
public void rotate() {
Node <E> last = head;
Node<E> update=head;
while(last.next != null) {
last=last.next;
}
head=update.next;
last.next=new Node<E>(update.getElement(),last.next);
}
public static void main(String[] args) {
CircularlyLinkedList<Integer> ll = new CircularlyLinkedList<Integer>();
for(int i = 10; i < 20; ++i) {
ll.addLast(i);
}
System.out.println(ll);
ll.removeFirst();
System.out.println(ll);
ll.removeLast();
ll.rotate();
System.out.println(ll);
ll.removeFirst();
ll.rotate();
System.out.println(ll);
ll.removeLast();
ll.rotate();
System.out.println(ll);
for (Integer e : ll) {
System.out.println("value: " + e);
}
}
}