-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCircularLinkedList.java
248 lines (240 loc) · 5.46 KB
/
CircularLinkedList.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
public class CircularLinkedList
{
private CLLNode head;
private int length;
//constructor
public CircularLinkedList()
{
head=null;
length=0;
}
/**
* INSERTION
* 1. AT HEAD
* 2. AT TAIL
* 3. AT MIDDLE
**/
/**
* addToHead()
* it will insert node at head
* @param int for data
* @return void
**/
public void addToHead(int data)
{
CLLNode newNode = new CLLNode(data);
if(head==null) //if list is empty
{
head=newNode;
head.setNext(head);
}
else
{
newNode.setNext(head);
CLLNode current=head.getNext();
while(current.getNext()!=head)
{
current=current.getNext();
}
current.setNext(newNode);
head=newNode;
}
length++;
}
/**
* addToTail()
* it will add the node to the tail
* @param int for data
* @return void
**/
public void addToTail(int data)
{
CLLNode newNode = new CLLNode(data);
if(head==null) //if empty
{
head=newNode;
head.setNext(head);
}
else
{
newNode.setNext(head);
CLLNode current=head.getNext();
while(current.getNext()!=head)
{
current=current.getNext();
}
current.setNext(newNode);
}
length++;
}
/**
* addToMiddle()
* it will add node to middle
* @param void
* @return void
**/
public void addToMiddle(int data,int position)
{
CLLNode newNode = new CLLNode(data);
if(position==0)
{
if(head==null)
{
head=newNode;
}
else
{
newNode.setNext(head);
CLLNode current = head.getNext();
while(current.getNext()!=head)
{
current=current.getNext();
}
head=newNode;
current.setNext(head);
}
}
else
{
//insert except head
CLLNode current=head;
CLLNode prev=head;
int it=0;
while(current!=null)
{
if(it==position)
{
break;
}
prev=current;
current=current.getNext();
it++;
}
prev.setNext(newNode);
newNode.setNext(current);
}
}
/**
* DELETION
* 1. FROM HEAD
* 2. FROM TAIL
* 3. FROM MIDDLE
**/
/**
* removeFromHead()
* it will remove node from head
* @param void
* @return int for removed node
**/
public int removeFromHead()
{
if(head==null)
{
return -1;
}
CLLNode temp = head;
CLLNode current=head.getNext();
while(current.getNext()!=head)
{
current=current.getNext();
}
head=head.getNext();
current.setNext(head);
temp.setNext(null);
length--;
return temp.getData();
}
/**
* removeFromTail()
* it will remove node from the tail
* @param void
* @return int for removed node
**/
public int removeFromTail()
{
if(head==null)
{
return -1;
}
CLLNode current=head.getNext();
CLLNode prev=head.getNext();
while(current.getNext()!=head)
{
prev=current;
current=current.getNext();
}
current.setNext(null);
prev.setNext(head);
length--;
return current.getData();
}
/**
* removeFromMiddle()
* @param void
* @return the removed node data
**/
public int removeFromMiddle(int position)
{
if(position==0)
{
//delete from head
CLLNode current=head;
while(current.getNext()!=head)
{
current=current.getNext();
}
CLLNode temp=head;
head=head.getNext();
current.setNext(head);
return temp.getData();
}
else
{
//delete from middle
CLLNode prev = head;
CLLNode current= head;
int it=0;
while(current!=null)
{
if(it==position)
{
break;
}
prev=current;
current=current.getNext();
it++;
}
CLLNode nextNode = current.getNext();
prev.setNext(nextNode);
current.setNext(null);
return current.getData();
}
}
/**
* length()
* @param void
* @return the length of the list
**/
public int length()
{
return length;
}
public void PrintCircularListData()
{
if(head==null)
{
System.out.println("null");
}
else
{
System.out.print(head.getData()+"->");
CLLNode current = head.getNext();
while(current!=head)
{
System.out.print(current.getData()+"->");
current=current.getNext();
}
System.out.println("HEAD");
}
}
}