-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLinkDeque.java
More file actions
210 lines (174 loc) · 5.15 KB
/
DLinkDeque.java
File metadata and controls
210 lines (174 loc) · 5.15 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//// 4.Write program to implement a DEQUE using Doubly Linked List.
import java.io.*;
import java.util.*;
class DLink {
public int data;
public DLink prev;
public DLink next;
public DLink(int d) {
data = d;
next = null;
}
public void displayLink() {
System.out.println(data);
}
}
class Deque {
private DLink first;
private DLink last;
public Deque() {
first = null;
last = null;
}
public boolean isEmpty() {
return (first == null);
}
// insert at first position
public void add_front(int d) {
DLink nl = new DLink(d);
if (isEmpty()) {
first = nl;
last = nl;
} else {
nl.next = first;
first.prev = nl;
first = nl;
}
}
// insert at last position
public void add_rear(int d) {
DLink nl = new DLink(d);
if (isEmpty()) {
first = nl;
last = nl;
} else {
nl.prev = last;
last.next = nl;
last = nl;
}
}
// Delete first
public int remove_front() {
int temp = first.data;
if (first.next == null) {
first = null;
last = null;
} else {
first.next.prev = null;
first = first.next;
}
return temp;
}
// Delete last
public int remove_rear() {
int temp = last.data;
last.prev.next = null;
last = last.prev;
return temp;
}
// size
public int size() {
DLink cur = first;
int n = 1;
while (cur.next != null) {
cur = cur.next;
n++;
}
return n;
}
// display all elements in the forward direction
public void displayForward() {
DLink current = first;
// if list is Empty
if (isEmpty()) {
System.out.println("THE DEQUE IS EMPTY");
}
while (current != null) {
current.displayLink();
current = current.next;
}
}
// display all elements in the backward direction
public void displayBackward() {
DLink current = last;
// if list is Empty
if (isEmpty()) {
System.out.println("THE DEQUE IS EMPTY");
}
while (current != null) {
current.displayLink();
current = current.prev;
}
}
}
class DLinkDeque {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
Deque obj = new Deque();
int x, value, value1, z;
do {
System.out.println("1.DISPLAY FORWARD");
System.out.println("2.DISPLAY BACKWARD");
System.out.println("3.INSERT FIRST");
System.out.println("4.INSERT LAST");
System.out.println("5.DELETE FIRST");
System.out.println("6.DELETE LAST");
System.out.println("7.SIZE");
x = sc.nextInt();
switch (x) {
case 1: {
System.out.println("Display all elements in the Forward direction");
obj.displayForward();
break;
}
case 2: {
System.out.println("Display all elements in the Backward direction");
obj.displayBackward();
break;
}
case 3: {
System.out.println("Enter the data to be inserted to the first position");
value = sc.nextInt();
obj.add_front(value);
break;
}
case 4: {
System.out.println("Enter the data to be inserted to the last position");
value = sc.nextInt();
obj.add_rear(value);
break;
}
case 5: {
if (obj.isEmpty()) {
System.out.println("The Deque is Empty");
} else {
value = obj.remove_front();
System.out.println(value + " is Deleted");
}
break;
}
case 6: {
if (obj.isEmpty()) {
System.out.println("The Deque is Empty");
} else {
value = obj.remove_rear();
System.out.println(value + " is Deleted");
}
break;
}
case 7: {
if (obj.isEmpty()) {
System.out.println("The Deque is Empty");
} else {
value = obj.size();
System.out.println("Total elements in the Deque is " + value);
}
break;
}
default:
System.out.println("Invalid input");
break;
}
} while (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 || x == 6);
}
}