-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingTwoStack.java
More file actions
56 lines (46 loc) · 1.31 KB
/
QueueUsingTwoStack.java
File metadata and controls
56 lines (46 loc) · 1.31 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
import java.util.Stack;
public class QueueUsingTwoStack {
static class myQueue {
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
public void enqueue(int val) {
// s1 --> s2
while(!s1.isEmpty()) {
s2.push(s1.pop());
}
//push elements in s1
s1.push(val);
//s2 --> s1
while(!s2.isEmpty()) {
s1.push(s2.pop());
}
}
public int dequeue() {
if(s1.isEmpty()) {
System.out.println("Queue is underflow");
return -1;
}
return s1.pop();
}
public int front() {
if(s1.isEmpty()) {
System.out.println("Queue is underflow");
return -1;
}
return s1.peek();
}
public int size() {
return s1.size();
}
}
public static void main(String[] args) {
myQueue q = new myQueue();
q.enqueue(25);
q.enqueue(50);
q.enqueue(75);
q.enqueue(100);
System.out.println("Size of queue:-"+q.size());
System.out.println("front of queue:-"+q.front());
System.out.println("Dequeue:-"+q.dequeue());
}
}