-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_singular_Linkedlist_Delete.java
More file actions
144 lines (123 loc) · 3.92 KB
/
Copy pathcircular_singular_Linkedlist_Delete.java
File metadata and controls
144 lines (123 loc) · 3.92 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
package datastructures;
public class circular_singular_Linkedlist_Delete {
public static void main(String[] args) {
// TODO Auto-generated method stub
circular_singular_Linkedlist_Delete csll = new circular_singular_Linkedlist_Delete();
csll.insertAtEnd(10);
csll.insertAtEnd(20);
csll.insertAtEnd(30);
csll.insertAtEnd(40);
System.out.println("Circular Singly Linked List:");
csll.displayList();
csll.deleteFromBeginning();
System.out.println("List after deleting from beginning:");
csll.displayList();
csll.deleteFromEnd();
System.out.println("List after deleting from end:");
csll.displayList();
csll.deleteNode(30);
System.out.println("List after deleting node with value 30:");
csll.displayList();
}
static class Node {
int data;
Node next;
// Constructor to create a new node
Node(int data) {
this.data = data;
this.next = null;
}
}
// Head of the circular singly linked list
private Node head;
// Constructor to create an empty circular singly linked list
public circular_singular_Linkedlist_Delete() {
head = null;
}
// Method to insert a new node at the end of the list
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
newNode.next = head;
} else {
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
}
}
// Method to delete a node from the beginning of the list
public void deleteFromBeginning() {
if (head == null) {
System.out.println("List is empty, no node to delete.");
return;
}
if (head.next == head) {
head = null;
} else {
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = head.next;
head = head.next;
}
}
// Method to delete a node from the end of the list
public void deleteFromEnd() {
if (head == null) {
System.out.println("List is empty, no node to delete.");
return;
}
if (head.next == head) {
head = null;
} else {
Node temp = head;
Node prev = null;
while (temp.next != head) {
prev = temp;
temp = temp.next;
}
prev.next = head;
}
}
// Method to delete a specific node
public void deleteNode(int key) {
if (head == null) {
System.out.println("List is empty, no node to delete.");
return;
}
// If head needs to be removed
if (head.data == key) {
deleteFromBeginning();
return;
}
Node temp = head;
Node prev = null;
// Search for the node to be deleted
do {
prev = temp;
temp = temp.next;
} while (temp != head && temp.data != key);
// If node to be deleted was found
if (temp.data == key) {
prev.next = temp.next;
}
}
// Method to display the list
public void displayList() {
if (head == null) {
System.out.println("List is empty");
return;
}
Node temp = head;
do {
System.out.print(temp.data + " ");
temp = temp.next;
} while (temp != head);
System.out.println();
}
}