-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingTwoStacks.java
More file actions
52 lines (46 loc) · 1.17 KB
/
Copy pathQueueUsingTwoStacks.java
File metadata and controls
52 lines (46 loc) · 1.17 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
public class QueueUsingTwoStacks extends StackImplementationWithArray {
private StackImplementationWithArray newStack;
private StackImplementationWithArray oldStack;
public QueueUsingTwoStacks(int size) {
// TODO Auto-generated constructor stub
//super();
newStack=new StackImplementationWithArray(size);
oldStack=new StackImplementationWithArray(size);
}
public void push(int data) throws Exception
{
newStack.push(data);
}
public StackImplementationWithArray shiftStack() throws Exception
{
while(!newStack.isEmpty())
{
oldStack.push(newStack.pop());
}
return oldStack;
}
public int pop() throws Exception
{
StackImplementationWithArray s=shiftStack();
return s.pop();
}
public int peek() throws Exception
{
StackImplementationWithArray s=shiftStack();
return s.peek();
}
public static void main(String[] args) throws Exception
{
QueueUsingTwoStacks queue=new QueueUsingTwoStacks(3);
queue.push(1);
queue.push(2);
queue.push(3);
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
queue.push(4);
queue.push(5);
queue.push(6);
System.out.println(queue.pop());
}
}