QueueStack CS1020
Manage a “big queue of stacks”: Create a stack with number X. Insert an integer Y to a stack with number X. Note that you must simulate a Queue, and therefore, can only access the first stack in the big queue. Merge the first two stacks in the big queue. Print the integer at the top of the stack in the front of the big queue. Problem Description 2
FIFO = First-In First-Out Queues are “one-way”. Only insert from the back, can only take the head. (FIFO) FIFO = First-In First-Out Big Queue of Stacks 1 4 5 3 22 3 8 9 5 17 100 1 4 5 3 7 Dequeue Enqueue Elaborate on how Queue is an interface and using LinkedList is one way of using that interface since LinkedList in java implements the Queue Interface. Head Tail Queue<E> = new LinkedList<E>(); http://docs.oracle.com/javase/8/docs/api/java/util/Queue.html
<<interface>> Queue<E> //see Java API + add(E) : boolean + peek() : E + poll() : E … Java Queue Interface
<<interface>> Queue<E> //see Java API add(E) + add(E) : boolean + peek() : E + poll() : E … Queue Java Queue Interface
<<interface>> Queue<E> //see Java API poll() + add(E) : boolean + peek() : E + poll() : E … Queue Java Queue Interface
Stack Class for this problem Class Stack<E> Stack<E> index: int elements: LinkedList<E> + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean Implement your own stack… Stack Class for this problem
Stack Class for this problem Class Stack<E> Stack<E> index: int elements: LinkedList<E> + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean 1 3 5 Implement your own stack… 4 1 Stack Class for this problem
Stack Class for this problem push(10) Class Stack<E> Stack<E> index: int elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean 1 3 5 4 1 Stack Class for this problem
Stack Class for this problem peek() Class Stack<E> Returns 10 Stack<E> index: int elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean 1 3 5 Elaborate on how peek returns 10 but the top of the stack stays inside the stack. 4 1 Stack Class for this problem
Stack Class for this problem pop() Class Stack<E> Returns 10 Stack<E> index: int elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean 1 3 5 Elaborate on how pop returns 10 and remove the top element 4 1 Stack Class for this problem
Stack Class for this problem Class Stack<E> public int getSize() { return elements.size(); } public int getIndex() { return this.index; public boolean isEmpty() { return this.getSize() == 0; Stack<E> index: int elements: LinkedList<E> + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean Stack Class for this problem
QueueStack Class QueueStack QueueStack bigQueue : LinkedList<Stack<Integer>> // TODO… :) QueueStack
QueueStack public class QueueStack { private Queue<Stack<Integer>> bigQueue; public QueueStack() { this.bigQueue = new LinkedList<Stack<Integer>>(); } //TODO :) class Stack<E> { QueueStack
Big Queue of Stacks Dequeue Enqueue X Head Tail Query 1 : CREATE x 15
Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary M Tail Dequeue Enqueue M 4 5 3 1 N 3 1 X 5 Q 8 20 17 Head Tail Temporary M Tail Head Query 2 : INSERT y x 16
Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary N M Tail Dequeue Enqueue N 3 1 X 5 Q 8 20 17 Head Tail Temporary N M Tail Head Query 2 : INSERT y x 17
Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary N M Tail Dequeue Enqueue X 5 Q 8 20 17 Head Tail Temporary N M Tail Head Query 2 : INSERT y x 18
Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary N M Tail Dequeue Enqueue X 5 Q 8 20 17 Y Head Tail Temporary N M Tail Head Query 2 : INSERT y x 19
Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary X N M Dequeue Enqueue X 5 Y Q 8 20 17 Head Tail Temporary X N M Tail Head Query 2 : INSERT y x 20
Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary Q X N M Dequeue Enqueue Q 8 20 17 Head Tail Temporary Q X N M Tail Head Query 2 : INSERT y x 21
Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary Q X N M Dequeue Enqueue M 4 5 3 1 N 3 1 X 5 Y Q 8 20 17 Head Tail Temporary Q X N M Tail Head Query 2 : INSERT y x 22
private void insert(int toBeInserted, int stackIndex) { Queue<Stack<Integer>> temp = new LinkedList<Stack<Integer>>(); int queueSize = bigQueue.size(); for (int i = 0; i < queueSize; i++) { } bigQueue = temp; Search for stack with index stackIndex and push the element. As you search, enqueue all stacks to a temporary queue and continue until all stacks are in the temporary queue. Query 2 : INSERT y x 23
Query 3 : MERGE Big Queue of Stacks Tail Head 3 2 1 9 Dequeue Enqueue 4 5 2 8 20 17 1 5 9 9 8 Head Tail Query 3 : MERGE 24
Query 3 : MERGE Big Queue of Stacks Tail Head 3 2 1 9 Dequeue Enqueue 8 20 17 1 5 9 9 8 4 3 5 5 3 4 Head Tail Query 3 : MERGE 25
Query 3 : MERGE Big Queue of Stacks Tail Head 2 1 9 Dequeue Enqueue 8 20 17 1 5 9 9 8 4 5 3 Head Tail private void merge() { Stack<Integer> firstStack = //poll or peek? Why? Stack<Integer> secondStack = //poll or peek? Why? int size = firstStack.getSize(); for (int k = 0; k < size; k++) { //TODO :) } Query 3 : MERGE 26
Query 4 : PRINT Big Queue of Stacks Tail Head 2 1 9 Dequeue Enqueue 27 8 3 5 4 20 17 1 5 9 9 8 Head Tail Query 4 : PRINT 27
Query 4 : PRINT Big Queue of Stacks Tail Head 2 1 9 Dequeue Enqueue 8 3 5 4 20 17 1 5 9 9 8 Head Tail private void print() { Stack<Integer> head = bigQueue.peek(); //why peek instead of poll? //TODO :) } Query 4 : PRINT 28