-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueusingStack.java
More file actions
43 lines (35 loc) · 855 Bytes
/
QueueusingStack.java
File metadata and controls
43 lines (35 loc) · 855 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
40
41
42
43
import java.util.Stack;
public class QueueusingStack {
private Stack<Integer> first ;
private Stack<Integer> second ;
public QueueusingStack(){
first = new Stack<>();
second= new Stack<>();
}
public void add(int number){
first.push(number);
}
public int remove () throws Exception{
while (!first.isEmpty()){
second.push(first.pop());
}
int removed = second.pop();
while(!second.isEmpty()){
first.push(second.pop());
}
return removed ;
}
public int peek () throws Exception {
while (!first.isEmpty()){
second.push(first.pop());
}
int peeked = second.peek();
while(!second.isEmpty()){
first.push(second.pop());
}
return peeked ;
}
public boolean isEmpty () {
return first.isEmpty() ;
}
}