-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCircularLinkedList.java
More file actions
225 lines (211 loc) · 3.06 KB
/
CircularLinkedList.java
File metadata and controls
225 lines (211 loc) · 3.06 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
package CircularLinkedList;
import CircularLinkedList.Node;
public class CircularLinkedList
{
Node head;
public void insert(int data)
{
Node node=new Node();
node.data=data;
node.prev=null;
node.next=null;
if(head==null)
{
head=node;
head.next=head.prev;
}
else
{
Node temp;
temp=head;
if(temp.next==null)
{
temp.next=node;
node.prev=temp;
node.next=temp;
temp.prev=node;
}
while(temp.next!=head)
{
temp=temp.next;
}
temp.next=node;
node.prev=temp;
node.next=head;
head.prev=node;
}
}
public void show()
{
System.out.println();
if(head==null)
{
System.out.println("No data found");
}
else
{
Node temp;
temp=head;
if(temp.next!=null)
{
while(temp.next!=head)
{
System.out.print(temp.data);
System.out.print(" <--> ");
temp=temp.next;
}
System.out.print(temp.data);
}
else
{
System.out.println(temp.data);
}
}
}
public void insertAt(int index,int data)
{
if(index>len())
{
System.out.println("Index not found");
}
else
{
try
{
Node node=new Node();
node.data=data;
if(head.next==null)
{
head.next=node;
node.prev=head;
node.next=head;
head.prev=node;
}
else
{
if(index==1)
{
node.prev=head.prev;
node.next=head;
head.prev=node;
node.prev.next=node;
head=node;
}
else
{
Node temp;
temp=head;
for(int i=1;i<index-1;i++)
{
temp=temp.next;
}
node.prev=temp;
node.next=temp.next;
temp.next=node;
node.next.prev=node;
}
}
} catch (Exception e)
{
System.out.println("\nIndex not found");
}
}
}
public int len()
{
if(head==null)
{
return 0;
}
try
{
Node temp=head;
int count=1;
while(temp.next!=head)
{
count++;
temp=temp.next;
}
return count;
} catch (Exception e)
{
return 1;
}
}
public boolean isFound(int data)
{
Node temp;
temp=head;
boolean found=false;
if(temp==null)
{
return found;
}
if(temp.data==data)
{
found=true;
return found;
}
while(temp.next!=head)
{
if(temp.data==data || temp.next.data==data)
{
found=true;
break;
}
temp=temp.next;
}
return found;
}
public void delete()
{
try
{
if(head.next==null)
{
head=null;
}
else if(head.next.next==head)
{
head.next=null;
head.prev=null;
}
else
{
Node temp;
temp=head;
while(temp.next!=head)
{
temp=temp.next;
}
temp.prev.next=head;
head.prev=temp.prev;
}
} catch (Exception e)
{
System.out.println("\nNo Data to Delete");
}
}
public void deleteAt(int index)
{
if(index==1)
{
Node temp;
temp=head;
head=temp.next;
temp.prev.next=head;
head.prev=temp.prev;
}
else
{
Node temp;
temp=head;
for(int i=1;i<index;i++)
{
temp=temp.next;
}
temp.prev.next=temp.next;
temp.next.prev=temp.prev;
}
}
}