-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularSinglyLinkedList.java
More file actions
167 lines (150 loc) · 4.71 KB
/
Copy pathCircularSinglyLinkedList.java
File metadata and controls
167 lines (150 loc) · 4.71 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
package dsaCodes;
public class CircularSinglyLinkedList {
private Node head;
private Node tail;
private int size;
private static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public CircularSinglyLinkedList() {
head = null;
tail = null;
size = 0;
}
// Insertion at the beginning
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
tail.next = head; // Circular connection
} else {
newNode.next = head;
head = newNode;
tail.next = head; // Update tail's next to point to head
}
size++;
}
// Insertion at the end
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
tail.next = head; // Circular connection
} else {
tail.next = newNode;
tail = newNode;
tail.next = head; // Update tail's next to point to head
}
size++;
}
// Insertion at a specific position
public void insertAtPosition(int position, int data) {
if (position < 0 || position > size) {
throw new IndexOutOfBoundsException("Invalid position: " + position);
}
if (position == 0) {
insertAtBeginning(data);
} else if (position == size) {
insertAtEnd(data);
} else {
Node newNode = new Node(data);
Node current = head;
for (int i = 0; i < position - 1; i++) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
size++;
}
}
// Deletion at the beginning
public void deleteFromBeginning() {
if (head == null) {
System.out.println("List is empty");
return;
}
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
tail.next = head; // Update tail's next to point to new head
}
size--;
}
// Deletion at the end
public void deleteFromEnd() {
if (head == null) {
System.out.println("List is empty");
return;
}
if (head == tail) {
head = null;
tail = null;
} else {
Node current = head;
while (current.next != tail) {
current = current.next;
}
current.next = head; // Update current node's next to point to head
tail = current; // Update tail to the previous node
}
size--;
}
// Deletion at a specific position
public void deleteFromPosition(int position) {
if (position < 0 || position >= size) {
throw new IndexOutOfBoundsException("Invalid position: " + position);
}
if (position == 0) {
deleteFromBeginning();
} else if (position == size - 1) {
deleteFromEnd();
} else {
Node current = head;
for (int i = 0; i < position - 1; i++) {
current = current.next;
}
current.next = current.next.next;
size--;
}
}
// Utility function to print the list
public void printList() {
if (head == null) {
System.out.println("List is empty");
return;
}
Node current = head;
do {
System.out.print(current.data + " -> ");
current = current.next;
} while (current != head);
System.out.println(" (Head)");
}
public static void main(String[] args) {
CircularSinglyLinkedList cll = new CircularSinglyLinkedList();
// Insertions
cll.insertAtBeginning(3);
cll.insertAtEnd(5);
cll.insertAtEnd(7);
cll.printList(); // Output: 3 -> 5 -> 7 -> (Head)
// Insert at specific position
cll.insertAtPosition(2, 6);
cll.printList(); // Output: 3 -> 5 -> 6 -> 7 -> (Head)
// Deletions
cll.deleteFromBeginning();
cll.printList(); // Output: 5 -> 6 -> 7 -> (Head)
cll.deleteFromEnd();
cll.printList(); // Output: 5 -> 6 -> (Head)
cll.deleteFromPosition(1);
cll.printList(); // Output: 5 -> (Head)
}
}