-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_double_Linkedlist_Delete.java
More file actions
152 lines (132 loc) · 4.19 KB
/
Circular_double_Linkedlist_Delete.java
File metadata and controls
152 lines (132 loc) · 4.19 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
package datastructures;
public class Circular_double_Linkedlist_Delete {
public static void main(String[] args) {
// TODO Auto-generated method stub
Circular_double_Linkedlist_Delete cdll = new Circular_double_Linkedlist_Delete();
cdll.insertAtEnd(10);
cdll.insertAtEnd(20);
cdll.insertAtEnd(30);
cdll.insertAtEnd(40);
System.out.println("Circular Doubly Linked List (Forward):");
cdll.displayForward();
cdll.deleteFromBeginning();
System.out.println("List after deleting from beginning:");
cdll.displayForward();
cdll.deleteFromEnd();
System.out.println("List after deleting from end:");
cdll.displayForward();
cdll.deleteNode(cdll.head.next);
System.out.println("List after deleting specific node:");
cdll.displayForward();
}
static class Node {
int data;
Node prev;
Node next;
// Constructor to create a new node
Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
// Head and tail of the circular doubly linked list
private Node head;
private Node tail;
// Constructor to create an empty circular doubly linked list
public Circular_double_Linkedlist_Delete() {
head = null;
tail = null;
}
// Method to insert a new node at the end of the list
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (tail == null) {
head = newNode;
tail = newNode;
newNode.next = head;
newNode.prev = tail;
} else {
newNode.next = head;
newNode.prev = tail;
tail.next = newNode;
head.prev = newNode;
tail = newNode;
}
}
// 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;
tail = null;
} else {
head = head.next;
head.prev = tail;
tail.next = head;
}
}
// Method to delete a node from the end of the list
public void deleteFromEnd() {
if (tail == null) {
System.out.println("List is empty, no node to delete.");
return;
}
if (tail.prev == tail) {
head = null;
tail = null;
} else {
tail = tail.prev;
tail.next = head;
head.prev = tail;
}
}
// Method to delete a specific node
public void deleteNode(Node delNode) {
if (head == null || delNode == null) {
System.out.println("List is empty or node to delete is null.");
return;
}
// If node to be deleted is head node
if (head == delNode) {
deleteFromBeginning();
return;
}
// If node to be deleted is tail node
if (tail == delNode) {
deleteFromEnd();
return;
}
delNode.prev.next = delNode.next;
delNode.next.prev = delNode.prev;
}
// Method to display the list from head to tail
public void displayForward() {
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();
}
// Method to display the list from tail to head
public void displayBackward() {
if (tail == null) {
System.out.println("List is empty");
return;
}
Node current = tail;
do {
System.out.print(current.data + " ");
current = current.prev;
} while (current != tail);
System.out.println();
}
}