|
15 | 15 | class QueueWithStack { |
16 | 16 |
|
17 | 17 | // Stack to keep track of elements inserted into the queue |
18 | | - private Stack inStack; |
| 18 | + private Stack<Object> inStack; |
19 | 19 | // Stack to keep track of elements to be removed next in queue |
20 | | - private Stack outStack; |
| 20 | + private Stack<Object> outStack; |
21 | 21 |
|
22 | 22 | /** Constructor */ |
23 | 23 | public QueueWithStack() { |
24 | | - this.inStack = new Stack(); |
25 | | - this.outStack = new Stack(); |
| 24 | + this.inStack = new Stack<>(); |
| 25 | + this.outStack = new Stack<>(); |
26 | 26 | } |
27 | 27 |
|
28 | 28 | /** |
@@ -82,6 +82,24 @@ public Object peekBack() { |
82 | 82 | public boolean isEmpty() { |
83 | 83 | return (this.inStack.isEmpty() && this.outStack.isEmpty()); |
84 | 84 | } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns true if the inStack is empty. |
| 88 | + * |
| 89 | + * @return true if the inStack is empty. |
| 90 | + */ |
| 91 | + public boolean isInStackEmpty() { |
| 92 | + return (inStack.size() == 0); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns true if the outStack is empty. |
| 97 | + * |
| 98 | + * @return true if the outStack is empty. |
| 99 | + */ |
| 100 | + public boolean isOutStackEmpty() { |
| 101 | + return (outStack.size() == 0); |
| 102 | + } |
85 | 103 | } |
86 | 104 |
|
87 | 105 | /** |
@@ -118,7 +136,7 @@ public static void main(String args[]) { |
118 | 136 | System.out.println(myQueue.isEmpty()); // Will print false |
119 | 137 |
|
120 | 138 | System.out.println(myQueue.remove()); // Will print 1 |
121 | | - System.out.println(myQueue.peekBack()); // Will print NULL |
| 139 | + System.out.println((myQueue.isInStackEmpty()) ? "null" : myQueue.peekBack()); // Will print NULL |
122 | 140 | // instack: [] |
123 | 141 | // outStack: [(top) 2, 3, 4] |
124 | 142 |
|
|
0 commit comments