-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingTwoStack.java
More file actions
127 lines (72 loc) · 3.18 KB
/
QueueUsingTwoStack.java
File metadata and controls
127 lines (72 loc) · 3.18 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Dell
*/
// we use two stacks , to represent a queue , we select an element from the top of the
// stack2 as oldest elements will be added on top of stack2 , hence it can represent queue datastructure
// QUEUE represents FIFO , thats is oldest element is removed first and stack in the inverse of STACK
//STACK removes most recent elements added to the stack.
import java.util.*;
public class QueueUsingTwoStack {
//declaring two stacks to perform stack operation
static Stack<Integer> st1 = new Stack<Integer>();
static Stack<Integer> st2 = new Stack<Integer>();
public static void enqueue(int e){
st1.push(e);
Iterator<Integer> itr = st1.iterator();
while(itr.hasNext()){
//st1.push(st1.pop());
System.out.print(itr.next() + " ");
}
System.out.println("");
}
public static void dequeue(){
//if stack 2 is not empty , then we pop from it.
if(!st2.isEmpty()){
System.out.println("Popped element is ::" + st2.pop());
}
// if stack 1 is empty that means no more elements added to the queue, hence we display no elements in queue.
else if(st1.isEmpty()){
System.out.println("No element in the queue");
}
//if stack2 is empty and new elements are added in stack1 then we inverse the stack1 by popping from stack1 and pushing it to stack2.
if(st2.isEmpty()){
Iterator<Integer> itr = st1.iterator();
while(itr.hasNext()){
st2.push(st1.pop());
}
Iterator<Integer> itr2 = st2.iterator();
while(itr2.hasNext()){
System.out.print(itr2.next() + " ");
}
System.out.println("");
}
}
public static void main(String[] args){
enqueue(10);
enqueue(20);
enqueue(40);
dequeue();
enqueue(50);
enqueue(60);
enqueue(70);
enqueue(80);
enqueue(90);
enqueue(140);
enqueue(100);
enqueue(200);
enqueue(400);
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
}
}