-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular_Linked_LIST.java
191 lines (174 loc) · 4.62 KB
/
Circular_Linked_LIST.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
class CLL {
class node {
int data;
node next;
node(int data) {
this.data = data;
this.next = null;
}
}
node first = null;
void insertatfirst(int data) {
node n = new node(data);
if (first == null) {
first = n;
n.next = first;
} else {
node c = first;
while (c.next != first) {
c = c.next;
}
n.next = first;
c.next = n;
first = n;
}
}
void insertatlast(int data) {
node n = new node(data);
if (first == null) {
first = n;
n.next = first;
} else {
node c = first;
while (c.next != first) {
c = c.next;
}
c.next = n;
n.next = first;
}
}
void insertbefore(int value, int data) {
if (first == null) {
System.out.println("List is Empty..");
} else {
int flag = 0;
node c = first;
do {
if (c.data == value) {
flag = 1;
}
c = c.next;
} while (c != first);
if (flag == 0) {
System.out.println("Value not found..");
} else {
if (first.data == value) {
insertatfirst(data);
} else {
node c1 = first;
while (c1.next.data != value) {
c1 = c1.next;
}
node n = new node(data);
n.next = c1.next;
c1.next = n;
}
}
}
}
void insertafter(int data, int value) {
if (first == null) {
System.out.println("List is Empty..");
} else {
int flag = 0;
node c = first;
do {
if (c.data == value) {
flag = 1;
}
c = c.next;
} while (c != first);
if (flag == 0) {
System.out.println("Element not found..");
} else {
node c1 = first;
while (c1.data != value) {
c1 = c1.next;
}
node n = new node(data);
n.next = c1.next;
c1.next = n;
}
}
}
void deletefirst() {
if (first.next == first) {
first = null;
} else {
node c = first;
while (c.next != first) {
c = c.next;
}
node del = first;
c.next = first.next;
first = first.next;
del.next = null;
}
}
void deletelast() {
if (first == null) {
System.out.println("List is Empty..");
} else if (first.next == first) {
first = null;
} else {
node c = first;
while (c.next.next != first) {
c = c.next;
}
node del = c.next;
c.next = first;
del.next = null;
}
}
void deletevalue(int value) {
if (first == null) {
System.out.println("List is Empty");
} else {
int flag = 0;
node c = first;
do {
if (c.data == value) {
flag = 1;
}
} while (c != first);
if (flag == 0) {
System.out.println("Value does not exist..");
} else {
node c1 = first;
while (c1.next.data != value) {
c1 = c1.next;
}
node del = c1.next;
c1.next = c1.next.next;
del.next = null;
}
}
}
void display() {
if (first == null) {
System.out.println("List is Empty..");
} else {
node c = first;
do {
System.out.print(c.data + "->");
c = c.next;
} while (c.next != first);
System.out.print(c.data + "->");
System.out.print("CLL..");
System.out.println();
}
}
}
class Main1 {
public static void main(String[] args) {
CLL c = new CLL();
c.insertatfirst(30);
c.insertatfirst(20);
c.insertatfirst(10);
c.insertatlast(40);
c.insertatlast(50);
c.insertafter(25, 20);
c.insertbefore(35, 40);
c.display();
}
}