-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCircularLinkedList.java
238 lines (228 loc) · 5.58 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
public class CircularLinkedList
{
private CLLNode tail;
private int length;
//constructor
public CircularLinkedList()
{
tail=null;
length=0;
}
/*
* INSERTION
* 1. at head
* 2. at tail
*/
/*==================> INSERTION AT HEAD <=================*/
public void addToHead(int data)
{
CLLNode newNode = new CLLNode(data);
if(tail==null) //if list is empty
{
tail=newNode;
tail.setNext(tail);
}
else
{
newNode.setNext(tail.getNext());
tail.setNext(newNode);
}
length++;
}
/*===================> INSERTION AT TAIL <=================*/
public void addToTail(int data)
{
CLLNode newNode = new CLLNode(data);
if(tail==null) //if empty
{
tail=newNode;
tail.setNext(tail);
}
else
{
newNode.setNext(tail.getNext());
tail.setNext(newNode);
tail=tail.getNext();
}
length++;
}
/*
* DELETION
* 1. FROM HEAD
* 2. FROM TAIL
* 3. FROM MIDDLE USING VALUE
*/
/*===============> DELETION FROM HEAD <=================*/
public int removeFromHead()
{
CLLNode temp = tail.getNext();
if(tail==tail.getNext()) //single node
{
tail=null;
}
else
{
tail.setNext(temp.getNext());
temp.setNext(null);
}
length--;
return temp.getData();
}
/*===============> DELETION FROM TAIL <=================*/
public int removeFromTail()
{
if(tail==null)
{
return -1;
}
CLLNode current=tail;
while(current.getNext()!=tail)
{
current=current.getNext();
}
CLLNode temp = tail;
current.setNext(tail.getNext());
tail=current;
length--;
return temp.getData();
}
/*===========> DELETION FROM MIDDLE USING VALUE <===============*/
public int remove_node_using_value(int data)
{
if(tail==null) //empty list
{
return -1;
}
CLLNode current=tail.getNext();
CLLNode prev=tail;
int compares;
for(compares=0;compares<length && (!(current.getData()==data));compares++)
{
prev=current;
current=current.getNext();
}
if(current.getData()==data)
{
if(tail==tail.getNext()) //single node
{
tail=null;
}
else
{
if(current==tail)
{
tail=prev;
}
prev.setNext(prev.getNext().getNext());
}
current.setNext(null);
length--;
return current.getData();
}
else
return -1;
}
/****************************************************/
/*================> return head node <==============*/
public int peek()
{
return tail.getNext().getData();
}
/****************************************************/
/*================> return tail node <==============*/
public int tailPeek()
{
return tail.getData();
}
/****************************************************/
/*==============> LENGTH OF LINKED LIST <===========*/
public int length()
{
return length;
}
public int size()
{
return length;
}
/*
* TRAVESING THE CIRCULAR LINKED LIST
*/
/*===========> printing circular linked list <===========*/
public void PrintCircularListData()
{
if(tail==null) //list empty
{
System.out.println("null");
}
else
{
System.out.print(tail.getData()+"->");
CLLNode current = tail.getNext();
while(current!=tail)
{
System.out.print(current.getData()+"->");
current=current.getNext();
}
System.out.println();
System.out.println("^");
System.out.println("TAIL");
}
}
/*
* SEARCHING
* --LINEAR SEARCH
*/
/*==============> linear search <=================*/
public boolean search(int data)
{
if(tail==null)
{
return false;
}
if(tail.getData()==data)
{
return true;
}
CLLNode current=tail.getNext();
while(current!=tail)
{
if(current.getData()==data)
{
return true;
}
current=current.getNext();
}
return false;
}
/***********************************************/
/*=============> IS EMPTY OR NOT <=============*/
public boolean isEmpty()
{
return(tail==null);
}
/***********************************************/
/*==============> CLEAR LIST <=================*/
public void clear()
{
tail=null;
length=0;
}
/************************************************/
/*==================> TO STRING <===============*/
public String toString()
{
String result="[";
if(tail==null)
{
return result+"]";
}
result=result+tail.getData();
CLLNode current=tail.getNext();
while(current!=tail)
{
result=result+","+current.getData();
current=current.getNext();
}
return result+"]";
}
}