-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularSingleLinkedList.java
More file actions
267 lines (230 loc) · 6.76 KB
/
CircularSingleLinkedList.java
File metadata and controls
267 lines (230 loc) · 6.76 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// the linked list node
class Node{
int data;
Node next;
Node(int x){
this.data=x;
this.next=null;
}
}
public class CircularSingleLinkedList {
// head and tail of the linked list
Node head;
Node tail;
// function to add a new node at last of linked list
// Time Complexity - O(1) , Space Complexity - O(1)
public void addNode(int x){
Node newNode = new Node(x);
if(head==null){
head=newNode;
tail=newNode;
tail.next=head;
return;
}
// else
tail.next=newNode;
tail=newNode;
tail.next=head;
}
// function to traverse the linked list
// Time Complexity - O(n) , Space Complexity - O(1)
public void traverse(){
if(head==null){
System.out.println("Linked list is empty");
return;
}
Node temp= head;
do {
System.out.print(temp.data+" ");
temp=temp.next;
}while(temp!=head);
System.out.println();
System.out.println("Next node of tail or head is : "+tail.next.data);
}
// function to get the total no. of nodes present in linked list
// Time Complexity - O(n) , Space Complexity - O(1)
public int getCountOfNodes(){
int count=0;
if(head==null){
return count;
}
Node temp=head;
do{
count++;
temp=temp.next;
}while(temp!=head);
return count;
}
// function to insert a new node on the given loc
// Time Complexity - O(n) , Space Complexity - O(1)
public void insertNodeByLoc(int data, int loc){
if(head==null){
System.out.println("Linked List is empty");
return;
}
// else if linked list is not empty
// Validate the loc
int noOfNodes = getCountOfNodes();
if(loc<=0 || loc>noOfNodes+1){
System.out.println("Not a valid location to insert");
return;
}
// if loc is valid
Node temp=head;
Node newNode = new Node(data);
// if loc is start
if(loc==1){
head=newNode;
newNode.next=temp;
tail.next=head;
return;
}
// if loc is end
else if(loc==noOfNodes+1){
addNode(data);
}
// if loc is anywhere between
else{
// bring the temp pointer to previous node
for(int i=0;i<loc-2;i++){
temp = temp.next;
}
newNode.next=temp.next;
temp.next=newNode;
}
}
// function to delete a node at the given loc
// Time Complexity - O(n) , Space Complexity - O(1)
public void deleteNodeByLoc(int loc){
if(head==null){
System.out.println("Linked List is empty");
return;
}
int noOfNodes = getCountOfNodes();
// check the validity of loc
if(loc<=0 || loc>noOfNodes){
System.out.println("Not a valid location to delete");
return;
}
// if loc is a valid location to delete
// if we have to delete the head node
if(loc==1){
head=head.next;
tail.next=head;
return;
}
Node temp=head;
Node previous = null;
// bring temp to node to be deleted and previous to one node behind the node to be deleted
for(int i=0;i<loc-1;i++){
previous=temp;
temp=temp.next;
}
previous.next=temp.next;
if(loc==noOfNodes){
tail=previous;
}
}
// function to delete a node having given value
// Time Complexity - O(n) , Space Complexity - O(1)
public void deleteNodeByValue(int data){
if(head==null){
System.out.println("Linked list is already empty");
return;
}
Node temp = head;
Node previous = null;
do{
// if data to be deleted found in linked list
if(temp.data==data){
break;
}
// if data to be deleted is not in current node
previous=temp;
temp = temp.next;
}while(temp!=head);
//check if the data to be deleted is at head node
if(head.data==temp.data && temp.data==data){
// this means we have to delete head node
head=head.next;
tail.next=head;
return;
}
// if node to be deleted is last node
else if(temp==tail){
previous.next=tail.next;
tail=previous;
return;
}
// if the node to be deleted is nay node in between
else{
previous.next=temp.next;
return;
}
}
// function to delete complete linked list
// Time Complexity - O(1) , Space Complexity - O(1)
public void deleteCompleteLinkedList(){
if(head==null){
System.out.println("Linked list is empty, can't delete");
return;
}
head=null;
tail=null;
}
// function to search for a node with the given value in Linked List
public void searchNode(int x){
if(head==null){
System.out.println("Linked List is empty, can't search");
return;
}
Node temp = head;
int found=-1;
do{
if(temp.data==x){
found=0;
break;
}
temp=temp.next;
}while(temp!=head);
if(found==0){
System.out.println("Node with data: "+x+" found in Linked List");
}
else{
System.out.println("Node with data: "+x+" not found in Linked List");
}
}
public static void main(String[] args) throws Exception {
CircularSingleLinkedList ll = new CircularSingleLinkedList();
ll.addNode(1);
ll.addNode(2);
ll.addNode(3);
ll.addNode(4);
ll.traverse();
System.out.println(ll.getCountOfNodes());
ll.insertNodeByLoc(0,3);
ll.traverse();
ll.deleteNodeByValue(0);
ll.traverse();
ll.deleteNodeByLoc(3);
ll.traverse();
ll.searchNode(3);
ll.searchNode(4);
ll.deleteCompleteLinkedList();
ll.traverse();
}
}
/* =========================== OUTPUT ==============================
1 2 3 4
Next node of tail or head is : 1
4
1 2 0 3 4
Next node of tail or head is : 1
1 2 3 4
Next node of tail or head is : 1
1 2 4
Next node of tail or head is : 1
Node with data: 3 not found in Linked List
Node with data: 4 found in Linked List
Linked list is empty
=========================================================================*/