-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackQueue
More file actions
39 lines (30 loc) · 694 Bytes
/
StackQueue
File metadata and controls
39 lines (30 loc) · 694 Bytes
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
package edu;
import java.util.Stack;
public class StackQueue {
static Stack<Integer> s1 = new Stack<Integer>();
static Stack<Integer> s2 = new Stack<Integer>();
public static void enqueue(int data) {
s1.push(data);
}
public static int dqueue() {
if(s1.isEmpty() && s2.isEmpty()) {
System.out.println("Queue is already empty");
return -1;
}
if(s2.isEmpty()) {
while(!s1.isEmpty()) {
s2.push(s1.pop());
}
}
return s2.pop();
}
public static void main(String[] args) {
enqueue(10);
enqueue(20);
enqueue(30);
System.out.println(dqueue());
System.out.println(dqueue());
System.out.println(dqueue());
System.out.println(dqueue());
}
}