-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinkedList.java
More file actions
198 lines (187 loc) · 5.86 KB
/
CircularLinkedList.java
File metadata and controls
198 lines (187 loc) · 5.86 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
import java.util.Scanner;
class CircularLinkedList {
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public Node head = null;
public Node tail = null;
public void insertBegin(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
tail.next = head;
} else {
newNode.next = head;
head = newNode;
tail.next = newNode;
}
System.out.println(data + " successfully inserted");
}
public void insertEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
tail.next = head;
} else {
tail.next = newNode;
tail = newNode;
tail.next = head;
}
System.out.println(data + " successfully inserted");
}
public void insertAtPos(int pos, int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
tail.next = head;
} else if (pos == 1) {
newNode.next = head;
head = newNode;
tail.next = newNode;
} else {
Node temp = head;
Node prev = null;
int count = 1;
while (temp.next != head && count != pos) {
count++;
prev = temp;
temp = temp.next;
}
prev.next = newNode;
newNode.next = temp;
// if (temp == head) {
// tail = newNode;
// }
}
System.out.println(data + " successfully inserted");
}
public void deleteBegin() {
if (head == null) {
System.out.println("List is empty");
} else if (head.next == head) {
System.out.println(head.data + " deleted successfully");
head = null;
tail = null;
} else {
System.out.println(head.data + " deleted successfully");
head = head.next;
tail.next = head;
}
}
public void deleteEnd() {
if (head == null) {
System.out.println("List is empty");
} else if (head.next == head) {
System.out.println(head.data + " deleted successfully");
head = null;
tail = null;
} else {
Node temp = head;
while (temp.next != tail) {
temp = temp.next;
}
System.out.println(tail.data + " deleted successfully");
tail = temp;
tail.next = head;
}
}
public void deleteAtPos(int pos) {
if (head == null) {
System.out.println("List is empty");
} else if (head.next == head) {
System.out.println(head.data + " deleted successfully");
head = null;
tail = null;
} else {
Node temp = head;
Node prev = null;
int count = 1;
while (temp.next != head && count != pos) {
prev = temp;
temp = temp.next;
count++;
}
prev.next = temp.next;
if (temp == tail) {
tail = prev;
}
System.out.println(temp.data + " deleted successfully");
}
}
public void display() {
Node temp = head;
if (head == null) {
System.out.println("List is empty");
} else {
do {
System.out.print(temp.data + " ---> ");
temp = temp.next;
} while (temp != head);
System.out.println("NULL");
}
}
public static void main(String[] args) {
CircularLinkedList ll = new CircularLinkedList();
Scanner sc = new Scanner(System.in);
int choice = 0;
while (choice != 9) {
System.out.println(
"1. Insert at begin\n2. Insert at end\n3. Insert at position\n4. Delete at begin\n5. Delete at end\n6. Delete at position\n7. Display\n8. Exit\nEnter your choice");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the data");
int data = sc.nextInt();
ll.insertBegin(data);
ll.display();
break;
case 2:
System.out.println("Enter the data");
data = sc.nextInt();
ll.insertEnd(data);
ll.display();
break;
case 3:
System.out.println("Enter the data");
data = sc.nextInt();
System.out.println("Enter the position");
int pos = sc.nextInt();
ll.insertAtPos(pos, data);
ll.display();
break;
case 4:
ll.deleteBegin();
ll.display();
break;
case 5:
ll.deleteEnd();
ll.display();
break;
case 6:
System.out.println("Enter the position");
pos = sc.nextInt();
ll.deleteAtPos(pos);
ll.display();
break;
case 7:
ll.display();
break;
case 8:
sc.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
}