Skip to content

Commit 598783d

Browse files
authored
Merge pull request TheAlgorithms#229 from sahilb2/master
Added QueueUsingTwoStacks.java class in Other folder
2 parents acc6f31 + ea164f1 commit 598783d

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

Others/QueueUsingTwoStacks.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* This implements Queue using two Stacks.
5+
*
6+
* Big O Runtime:
7+
* insert(): O(1)
8+
* remove(): O(1) amortized
9+
* isEmpty(): O(1)
10+
*
11+
* A queue data structure functions the same as a real world queue.
12+
* The elements that are added first are the first to be removed.
13+
* New elements are added to the back/rear of the queue.
14+
*
15+
* @author sahilb2
16+
*
17+
*/
18+
class QueueWithStack {
19+
20+
// Stack to keep track of elements inserted into the queue
21+
private Stack inStack;
22+
// Stack to keep track of elements to be removed next in queue
23+
private Stack outStack;
24+
25+
/**
26+
* Constructor
27+
*/
28+
public QueueWithStack() {
29+
this.inStack = new Stack();
30+
this.outStack = new Stack();
31+
}
32+
33+
/**
34+
* Inserts an element at the rear of the queue
35+
*
36+
* @param x element to be added
37+
*/
38+
public void insert(Object x) {
39+
// Insert element into inStack
40+
this.inStack.push(x);
41+
}
42+
43+
/**
44+
* Remove an element from the front of the queue
45+
*
46+
* @return the new front of the queue
47+
*/
48+
public Object remove() {
49+
if(this.outStack.isEmpty()) {
50+
// Move all elements from inStack to outStack (preserving the order)
51+
while(!this.inStack.isEmpty()) {
52+
this.outStack.push( this.inStack.pop() );
53+
}
54+
}
55+
return this.outStack.pop();
56+
}
57+
58+
/**
59+
* Returns true if the queue is empty
60+
*
61+
* @return true if the queue is empty
62+
*/
63+
public boolean isEmpty() {
64+
return (this.inStack.isEmpty() && this.outStack.isEmpty());
65+
}
66+
67+
}
68+
69+
/**
70+
* This class is the example for the Queue class
71+
*
72+
* @author sahilb2
73+
*
74+
*/
75+
public class QueueUsingTwoStacks {
76+
77+
/**
78+
* Main method
79+
*
80+
* @param args Command line arguments
81+
*/
82+
public static void main(String args[]){
83+
QueueWithStack myQueue = new QueueWithStack();
84+
myQueue.insert(1);
85+
// instack: [(top) 1]
86+
// outStack: []
87+
myQueue.insert(2);
88+
// instack: [(top) 2, 1]
89+
// outStack: []
90+
myQueue.insert(3);
91+
// instack: [(top) 3, 2, 1]
92+
// outStack: []
93+
myQueue.insert(4);
94+
// instack: [(top) 4, 3, 2, 1]
95+
// outStack: []
96+
97+
System.out.println(myQueue.isEmpty()); //Will print false
98+
99+
System.out.println(myQueue.remove()); //Will print 1
100+
// instack: []
101+
// outStack: [(top) 2, 3, 4]
102+
103+
myQueue.insert(5);
104+
// instack: [(top) 5]
105+
// outStack: [(top) 2, 3, 4]
106+
107+
myQueue.remove();
108+
// instack: [(top) 5]
109+
// outStack: [(top) 3, 4]
110+
myQueue.remove();
111+
// instack: [(top) 5]
112+
// outStack: [(top) 4]
113+
myQueue.remove();
114+
// instack: [(top) 5]
115+
// outStack: []
116+
myQueue.remove();
117+
// instack: []
118+
// outStack: []
119+
120+
System.out.println(myQueue.isEmpty()); //Will print true
121+
122+
}
123+
}

0 commit comments

Comments
 (0)