Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions Others/QueueUsingTwoStacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
class QueueWithStack {

// Stack to keep track of elements inserted into the queue
private Stack inStack;
private Stack<Object> inStack;
// Stack to keep track of elements to be removed next in queue
private Stack outStack;
private Stack<Object> outStack;

/** Constructor */
public QueueWithStack() {
this.inStack = new Stack();
this.outStack = new Stack();
this.inStack = new Stack<>();
this.outStack = new Stack<>();
}

/**
Expand Down Expand Up @@ -82,6 +82,24 @@ public Object peekBack() {
public boolean isEmpty() {
return (this.inStack.isEmpty() && this.outStack.isEmpty());
}

/**
* Returns true if the inStack is empty.
*
* @return true if the inStack is empty.
*/
public boolean isInStackEmpty() {
return (inStack.size() == 0);
}

/**
* Returns true if the outStack is empty.
*
* @return true if the outStack is empty.
*/
public boolean isOutStackEmpty() {
return (outStack.size() == 0);
}
}

/**
Expand Down Expand Up @@ -118,7 +136,7 @@ public static void main(String args[]) {
System.out.println(myQueue.isEmpty()); // Will print false

System.out.println(myQueue.remove()); // Will print 1
System.out.println(myQueue.peekBack()); // Will print NULL
System.out.println((myQueue.isInStackEmpty()) ? "null" : myQueue.peekBack()); // Will print NULL
// instack: []
// outStack: [(top) 2, 3, 4]

Expand Down