-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplement Stack and Queue using Deque.java
243 lines (197 loc) · 5.71 KB
/
Implement Stack and Queue using Deque.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Java program to implement stack and
// queue using Deque
import java.lang.*;
class GFG {
// Class for a node of deque
static class DQueNode {
int value;
DQueNode next;
DQueNode prev;
}
// Implementation of deque class
static class deque {
// Pointers to head and tail of deque
private DQueNode head;
private DQueNode tail;
// Constructor
public deque() { head = tail = null; }
// If list is empty
boolean isEmpty()
{
if (head == null)
return true;
return false;
}
// count the number of nodes in list
int size()
{
// If list is not empty
if (!isEmpty()) {
DQueNode temp = head;
int len = 0;
while (temp != null) {
len++;
temp = temp.next;
}
return len;
}
return 0;
}
// Insert at the first position
void insert_first(int element)
{
// Allocating node of DQueNode type
DQueNode temp = new DQueNode();
temp.value = element;
// If the element is first element
if (head == null) {
head = tail = temp;
temp.next = temp.prev = null;
}
else {
head.prev = temp;
temp.next = head;
temp.prev = null;
head = temp;
}
}
// Insert at last position of deque
void insert_last(int element)
{
// Allocating node of DQueNode type
DQueNode temp = new DQueNode();
temp.value = element;
// If element is the first element
if (head == null) {
head = tail = temp;
temp.next = temp.prev = null;
}
else {
tail.next = temp;
temp.next = null;
temp.prev = tail;
tail = temp;
}
}
// Remove element at the first position
public void remove_first()
{
// If list is not empty
if (!isEmpty()) {
// If there is only one node
if (head == tail) {
head = tail = null;
return;
} else {
head = head.next;
head.prev = null;
}
} else {
System.out.println("List is Empty");
}
}
// Remove element at the last position
void remove_last()
{
// If list is not empty
if (!isEmpty()) {
DQueNode temp = tail;
tail = tail.prev;
if (tail != null) {
tail.next = null;
}
return;
}
System.out.print("List is Empty");
}
// Displays the elements in deque
void display()
{
// If list is not empty
if (!isEmpty()) {
DQueNode temp = head;
while (temp != null) {
System.out.print(temp.value + " ");
temp = temp.next;
}
return;
}
System.out.print("List is Empty");
}
}
// Class to implement stack using Deque
static class Stack {
deque d = new deque();
// push to push element at top of stack
// using insert at last function of deque
public void push(int element)
{
d.insert_last(element);
}
// Returns size
public int size() { return d.size(); }
// pop to remove element at top of stack
// using remove at last function of deque
public void pop() { d.remove_last(); }
// Display
public void display() { d.display(); }
}
// Class to implement queue using deque
static class Queue {
deque d = new deque();
// enqueue to insert element at last
// using insert at last function of deque
public void enqueue(int element)
{
d.insert_last(element);
}
// dequeue to remove element from first
// using remove at first function of deque
public void dequeue() { d.remove_first(); }
// display
public void display() { d.display(); }
// size
public int size() { return d.size(); }
}
// Driver Code
public static void main(String[] args)
{
// Object of Stack
Stack stk = new Stack();
// push 7 and 8 at top of stack
stk.push(7);
stk.push(8);
System.out.print("Stack: ");
stk.display();
// For new line
System.out.println();
// pop an element
stk.pop();
System.out.print("Stack: ");
stk.display();
// For new line
System.out.println();
// Object of Queue
Queue que = new Queue();
// Insert 12 and 13 in queue
que.enqueue(12);
que.enqueue(13);
System.out.print("Queue: ");
que.display();
// New line
System.out.println();
// Delete an element from queue
que.dequeue();
System.out.print("Queue: ");
que.display();
// New line
System.out.println();
System.out.println("Size of stack is "
+ stk.size());
System.out.println("Size of queue is "
+ que.size());
}
}
// This code is contributed by sujitmeshram
// This code is modified by Susobhan Akhuli
// This code is modified by K.Harichandana