-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdll.java
142 lines (128 loc) · 4.27 KB
/
dll.java
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
import java.util.*;
class node {
int data;
node next;
node prev;
public node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
node head;
node tail;
public DoublyLinkedList() {
this.head = null;
this.tail = null;
}
}
public class dll {
static DoublyLinkedList doublyLinkedList = new DoublyLinkedList();
public static void main(String args[]) {
int c;
while (true) {
System.out.println("\n1.addend()\n2.delend()\n3.addfront()\n4.delfront()\n5.traverse()\n6.Exit\n");
Scanner sc = new Scanner(System.in);
System.out.print("Enter the choice:");
c = sc.nextInt();
switch (c) {
case 1:
addEnd();
break;
case 2:
delEnd();
break;
case 3:
addFront();
break;
case 4:
delFront();
break;
case 5:
traverse();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Invalid Choice");
}
}
}
private static void traverse() {
node t = doublyLinkedList.head;
while (t != null) {
System.out.print(t.data + " ");
t = t.next;
}
System.out.println(); // Add a newline after printing the list
}
private static void addEnd() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number to insert:");
int n = sc.nextInt();
if (doublyLinkedList.head == null) {
node newnode = new node(n);
newnode.next = null;
newnode.prev = null;
doublyLinkedList.head = newnode;
doublyLinkedList.tail = newnode;
} else {
node newnode = new node(n);
newnode.next = null;
newnode.prev = doublyLinkedList.tail;
doublyLinkedList.tail.next = newnode;
doublyLinkedList.tail = newnode;
}
}
private static void delEnd() {
if (doublyLinkedList.head == null) {
System.out.println("The list is empty");
} else if (doublyLinkedList.head == doublyLinkedList.tail) {
node temp = doublyLinkedList.head;
doublyLinkedList.head = null;
doublyLinkedList.tail = null;
System.out.println("The deleted element is " + temp.data);
} else {
System.out.println("The deleted element is " + doublyLinkedList.tail.data);
doublyLinkedList.tail = doublyLinkedList.tail.prev;
doublyLinkedList.tail.next = null;
}
}
private static void delFront() {
if (doublyLinkedList.head == null) {
System.out.println("The list is empty");
}
else if (doublyLinkedList.head == doublyLinkedList.tail) {
node temp = doublyLinkedList.head;
doublyLinkedList.head = null;
doublyLinkedList.tail = null;
System.out.println("The deleted element is " + temp.data);
}
else {
node temp = doublyLinkedList.head;
doublyLinkedList.head = doublyLinkedList.head.next;
doublyLinkedList.head.prev = null;
System.out.println("The deleted element is " + temp.data);
}
}
private static void addFront() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number to insert:");
int n = sc.nextInt();
node newnode = new node(n);
if (doublyLinkedList.head == null) {
newnode.next = null;
newnode.prev = null;
doublyLinkedList.head = newnode;
doublyLinkedList.tail = newnode;
}
else {
newnode.prev = null;
newnode.next = doublyLinkedList.head;
doublyLinkedList.head.prev = newnode;
doublyLinkedList.head = newnode;
}
}
}