Download presentation
Presentation is loading. Please wait.
1
QueueStack CS1020
2
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
3
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>();
4
<<interface>> Queue<E>
//see Java API + add(E) : boolean + peek() : E + poll() : E … Java Queue Interface
5
<<interface>> Queue<E>
//see Java API add(E) + add(E) : boolean + peek() : E + poll() : E … Queue Java Queue Interface
6
<<interface>> Queue<E>
//see Java API poll() + add(E) : boolean + peek() : E + poll() : E … Queue Java Queue Interface
7
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
8
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
9
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
10
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
11
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
12
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
13
QueueStack Class QueueStack QueueStack
bigQueue : LinkedList<Stack<Integer>> // TODO… :) QueueStack
14
QueueStack public class QueueStack {
private Queue<Stack<Integer>> bigQueue; public QueueStack() { this.bigQueue = new LinkedList<Stack<Integer>>(); } //TODO :) class Stack<E> { QueueStack
15
Big Queue of Stacks Dequeue Enqueue X Head Tail Query 1 : CREATE x 15
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
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
26
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
27
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
28
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.