-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_double_Linkedlist_Insert.java
More file actions
122 lines (107 loc) · 3.43 KB
/
Circular_double_Linkedlist_Insert.java
File metadata and controls
122 lines (107 loc) · 3.43 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
package datastructures;
public class Circular_double_Linkedlist_Insert {
public static void main(String[] args) {
// TODO Auto-generated method stub
Circular_double_Linkedlist_Insert cdll = new Circular_double_Linkedlist_Insert();
cdll.insertAtBeginning(10);
cdll.insertAtBeginning(20);
cdll.insertAtEnd(30);
cdll.insertAfter(cdll.head.next, 25);
System.out.println("Circular Doubly Linked List (Forward):");
cdll.displayForward();
System.out.println("Circular Doubly Linked List (Backward):");
cdll.displayBackward();
}
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_Insert() {
head = null;
tail = null;
}
// Method to insert a new node at the beginning of the list
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
newNode.next = head;
newNode.prev = tail;
} else {
newNode.next = head;
newNode.prev = tail;
head.prev = newNode;
tail.next = newNode;
head = newNode;
}
}
// 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 insert a new node after a given node
public void insertAfter(Node prevNode, int data) {
if (prevNode == null) {
System.out.println("The given previous node cannot be null");
return;
}
Node newNode = new Node(data);
newNode.next = prevNode.next;
newNode.prev = prevNode;
prevNode.next.prev = newNode;
prevNode.next = newNode;
if (prevNode == tail) {
tail = newNode;
}
}
// 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();
}
}