-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_queue.java
More file actions
303 lines (262 loc) · 6.35 KB
/
Copy pathStack_queue.java
File metadata and controls
303 lines (262 loc) · 6.35 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Java Program to Create a Stack using Two Queues
*/
import java.util.*;
class Test
{
Queue<Integer> q1 ;
Queue<Integer> q2 ; //temporary Queue
/* Constructor */
public Test()
{
q1 = new LinkedList<Integer>();
q2 = new LinkedList<Integer>();
}
/* Function to push an element to the stack */
public void push(int data)
{
/* if no element is present in queue q1 then
* enqueue the new element into q1 */
if (q1.size() == 0)
{
q1.add(data);
}
else
{
/* if elements are present in q1 then
* dequeue all the elements to
* temporary queue q2 */
int n = q1.size();
for (int i = 0; i < n; i++)
{
q2.add(q1.remove());
}
/* enqueue the new element into q1 */
q1.add(data);
/* dequeue all the elements from
* temporary queue q2 to q1 */
for (int i = 0; i < n; i++)
{
q1.add(q2.remove());
}
}
}
/* Function to remove top element from the stack */
public int pop()
{
if (q1.size() == 0)
{
throw new NoSuchElementException("stack is Underflow");
}
return q1.remove();
}
/* Function to check the top element of the stack */
public int peek()
{
if (q1.size() == 0)
{
throw new NoSuchElementException("stack is Underflow");
}
return q1.peek();
}
/* Function to check if stack is empty */
public boolean isEmpty()
{
return q1.size() == 0 ;
}
/* Function to get the size of the stack */
public int getSize()
{
return q1.size();
}
/* Function to display the status of the stack */
public void display()
{
System.out.print("\nElements in Stack = ");
int n = getSize();
if (n == 0)
{
System.out.print("Stack is Empty\n");
}
else
{
Iterator it = q1.iterator();
while (it.hasNext())
{
System.out.print(it.next()+" ");
}
System.out.println();
}
}
}
/* Class Stack_queue */
public class Stack_queue
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Test t = new Test();
/* Perform Stack Operations */
System.out.println("Stack Using Two Queues\n");
char ch;
do
{
System.out.println("\nStack Operations");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to push");
t.push( scan.nextInt() );
break;
case 2 :
try
{
System.out.println("Popped Element is = "+ t.pop() );
}
catch (Exception e)
{
System.out.println("Found Error : " + e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Peeked Element is = "+ t.peek() );
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+ t.isEmpty() );
break;
case 5 :
System.out.println("Size = "+ t.getSize() );
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* Display Stack */
t.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
/*
-- OUTPUT--
Stack Using Two Queues
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. size
1
Enter integer element to push
30
Elements in Stack = 30
Do you want to continue (Type y or n)
y
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. size
1
Enter integer element to push
40
Elements in Stack = 40 30
Do you want to continue (Type y or n)
y
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. size
3
Peeked Element is = 40
Elements in Stack = 40 30
Do you want to continue (Type y or n)
y
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. size
4
Empty status = false
Elements in Stack = 40 30
Do you want to continue (Type y or n)
y
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. size
Stack Using Two Queues
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. size
1
Enter integer element to push
30
Elements in Stack = 30
Do you want to continue (Type y or n)
y
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. size
1
Enter integer element to push
40
Elements in Stack = 40 30
Do you want to continue (Type y or n)
y
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. size
3
Peeked Element is = 40
Elements in Stack = 40 30
Do you want to continue (Type y or n)
y
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. size
4
Empty status = false
Elements in Stack = 40 30
Do you want to continue (Type y or n)
y
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. size
*/