1
+ public class CircularLinkedList
2
+ {
3
+ private CLLNode head ;
4
+ private int length ;
5
+
6
+ //constructor
7
+ public CircularLinkedList ()
8
+ {
9
+ head =null ;
10
+ length =0 ;
11
+ }
12
+
13
+ /**
14
+ * INSERTION
15
+ * 1. AT HEAD
16
+ * 2. AT TAIL
17
+ * 3. AT MIDDLE
18
+ **/
19
+ /**
20
+ * addToHead()
21
+ * it will insert node at head
22
+ * @param int for data
23
+ * @return void
24
+ **/
25
+ public void addToHead (int data )
26
+ {
27
+ CLLNode newNode = new CLLNode (data );
28
+ if (head ==null ) //if list is empty
29
+ {
30
+ head =newNode ;
31
+ head .setNext (head );
32
+ }
33
+ else
34
+ {
35
+ newNode .setNext (head );
36
+ CLLNode current =head .getNext ();
37
+ while (current .getNext ()!=head )
38
+ {
39
+ current =current .getNext ();
40
+ }
41
+ current .setNext (newNode );
42
+ head =newNode ;
43
+ }
44
+ length ++;
45
+ }
46
+
47
+ /**
48
+ * addToTail()
49
+ * it will add the node to the tail
50
+ * @param int for data
51
+ * @return void
52
+ **/
53
+ public void addToTail (int data )
54
+ {
55
+ CLLNode newNode = new CLLNode (data );
56
+ if (head ==null ) //if empty
57
+ {
58
+ head =newNode ;
59
+ head .setNext (head );
60
+ }
61
+ else
62
+ {
63
+ newNode .setNext (head );
64
+ CLLNode current =head .getNext ();
65
+ while (current .getNext ()!=head )
66
+ {
67
+ current =current .getNext ();
68
+ }
69
+ current .setNext (newNode );
70
+ }
71
+ length ++;
72
+ }
73
+
74
+ /**
75
+ * addToMiddle()
76
+ * it will add node to middle
77
+ * @param void
78
+ * @return void
79
+ **/
80
+ public void addToMiddle (int data ,int position )
81
+ {
82
+ CLLNode newNode = new CLLNode (data );
83
+ if (position ==0 )
84
+ {
85
+ if (head ==null )
86
+ {
87
+ head =newNode ;
88
+ }
89
+ else
90
+ {
91
+ newNode .setNext (head );
92
+ CLLNode current = head .getNext ();
93
+ while (current .getNext ()!=head )
94
+ {
95
+ current =current .getNext ();
96
+ }
97
+ head =newNode ;
98
+ current .setNext (head );
99
+ }
100
+ }
101
+ else
102
+ {
103
+ //insert except head
104
+ CLLNode current =head ;
105
+ CLLNode prev =head ;
106
+ int it =0 ;
107
+ while (current !=null )
108
+ {
109
+ if (it ==position )
110
+ {
111
+ break ;
112
+ }
113
+ prev =current ;
114
+ current =current .getNext ();
115
+ it ++;
116
+ }
117
+ prev .setNext (newNode );
118
+ newNode .setNext (current );
119
+ }
120
+ }
121
+
122
+ /**
123
+ * DELETION
124
+ * 1. FROM HEAD
125
+ * 2. FROM TAIL
126
+ * 3. FROM MIDDLE
127
+ **/
128
+
129
+ /**
130
+ * removeFromHead()
131
+ * it will remove node from head
132
+ * @param void
133
+ * @return int for removed node
134
+ **/
135
+ public int removeFromHead ()
136
+ {
137
+ if (head ==null )
138
+ {
139
+ return -1 ;
140
+ }
141
+ CLLNode temp = head ;
142
+ CLLNode current =head .getNext ();
143
+ while (current .getNext ()!=head )
144
+ {
145
+ current =current .getNext ();
146
+ }
147
+ head =head .getNext ();
148
+ current .setNext (head );
149
+ temp .setNext (null );
150
+ length --;
151
+ return temp .getData ();
152
+ }
153
+
154
+ /**
155
+ * removeFromTail()
156
+ * it will remove node from the tail
157
+ * @param void
158
+ * @return int for removed node
159
+ **/
160
+ public int removeFromTail ()
161
+ {
162
+ if (head ==null )
163
+ {
164
+ return -1 ;
165
+ }
166
+ CLLNode current =head .getNext ();
167
+ CLLNode prev =head .getNext ();
168
+ while (current .getNext ()!=head )
169
+ {
170
+ prev =current ;
171
+ current =current .getNext ();
172
+ }
173
+ current .setNext (null );
174
+ prev .setNext (head );
175
+ length --;
176
+ return current .getData ();
177
+ }
178
+
179
+ /**
180
+ * removeFromMiddle()
181
+ * @param void
182
+ * @return the removed node data
183
+ **/
184
+ public int removeFromMiddle (int position )
185
+ {
186
+ if (position ==0 )
187
+ {
188
+ //delete from head
189
+ CLLNode current =head ;
190
+ while (current .getNext ()!=head )
191
+ {
192
+ current =current .getNext ();
193
+ }
194
+ CLLNode temp =head ;
195
+ head =head .getNext ();
196
+ current .setNext (head );
197
+ return temp .getData ();
198
+ }
199
+ else
200
+ {
201
+ //delete from middle
202
+ CLLNode prev = head ;
203
+ CLLNode current = head ;
204
+ int it =0 ;
205
+ while (current !=null )
206
+ {
207
+ if (it ==position )
208
+ {
209
+ break ;
210
+ }
211
+ prev =current ;
212
+ current =current .getNext ();
213
+ it ++;
214
+ }
215
+ CLLNode nextNode = current .getNext ();
216
+ prev .setNext (nextNode );
217
+ current .setNext (null );
218
+ return current .getData ();
219
+ }
220
+ }
221
+ /**
222
+ * length()
223
+ * @param void
224
+ * @return the length of the list
225
+ **/
226
+ public int length ()
227
+ {
228
+ return length ;
229
+ }
230
+ public void PrintCircularListData ()
231
+ {
232
+ if (head ==null )
233
+ {
234
+ System .out .println ("null" );
235
+ }
236
+ else
237
+ {
238
+ System .out .print (head .getData ()+"->" );
239
+ CLLNode current = head .getNext ();
240
+ while (current !=head )
241
+ {
242
+ System .out .print (current .getData ()+"->" );
243
+ current =current .getNext ();
244
+ }
245
+ System .out .println ("HEAD" );
246
+ }
247
+ }
248
+ }
0 commit comments