CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 )...
Transcript of CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 )...
![Page 1: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/1.jpg)
CS200: Queues
n Prichard Ch. 8
CS200 - Queues 1
![Page 2: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/2.jpg)
Queues
n First In First Out (FIFO) structure n Imagine a checkout line n So removing and adding are done from
opposite ends of structure.
q add to tail (back), remove from head (front) n Used in operating systems (e.g. print queue).
1 2 3 4 5
CS200 -Queues 2
![Page 3: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/3.jpg)
Possible Queue Operations
n enqueue(in newItem: QueueItemType) q Add new item at the back of a queue
n dequeue(): QueueItemType!q Retrieve and remove the item at the front of a queue
n peek(): QueueItemType!q Retrieve item from the front of the queue. Retrieve the
item that was added earliest.
n isEmpty():boolean!
n createQueue()!
3 CS200 - Queues
![Page 4: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/4.jpg)
Reference-Based Implementation 1
A linked list with two external references q A reference to the front q A reference to the back
At which end do we enqueue / dequeue?
CS200 - Stacks 4
.
70 . 55 .
Reference of the Last Node Reference of the First Node .
60 Item Next
WHY?
![Page 5: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/5.jpg)
Reference-Based Implementation 2
A circular linked list with one external reference q lastNode references the back of the queue q lastNode.getNext() references the front
CS200 -Queues 5
70 . 55 60 .
Last Node: node reference .
![Page 6: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/6.jpg)
.
Inserting an item into a nonempty queue
6
70 . 55 60 .
Last Node
1. newNode.next = lastNode.next; 2. lastNode.next = newNode; 3. lastNode = newNode;
85
. New node
CS200 -Queues
![Page 7: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/7.jpg)
Inserting a New Item
7
.
n Insert a new item into the empty queue
Last Node
60 .
CS200 -Queues
![Page 8: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/8.jpg)
Insert new item into the queue
public void enqueue (Object newItem){! Node newNode = new Node(newItem);!
if (isEmpty()){! newNode.next = newNode;! } else {!
newNode.next = lastNode.next;! lastNode.next = newNode;! }!
lastNode = newNode;!} !
8
A. Empty queue
B. items in queue
CS200 -Queues
![Page 9: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/9.jpg)
Removing an item from queue public Object dequeue() throws QueueException{! if (!isEmpty()){! Node firstNode = lastNode.next;! if (firstNode == lastNode) {! lastNode = null;! }! else{! lastNode.next = firstNode.next;! }! return firstNode.item;! }! else { exception handling..! }!}!
9
Why?
CS200 -Queues
![Page 10: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/10.jpg)
Removing an Item
10
70 . 60 . 80 .
Last Node .
Node firstNode = lastNode.next;!if (firstNode == lastnode) {! lastNode = null;}! else{lastNode.next = firstNode.next;}!return firstNode.item;!
First Node .
What happens to this node?
CS200 -Queues
![Page 11: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/11.jpg)
Naïve Array-Based Implementation
Drift wastes space How do we initialize front and back? (Hint: what does a queue with a single element look like? what des an empty queue look like? )
CS200 -Queues 11
![Page 12: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/12.jpg)
Solving Drift: Circular implementation of a queue
12
1 6
5
4 3
2
0MAX_QUEUE-1
i
a
e
o
FRONT
BACK
CS200 -Queues
![Page 13: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/13.jpg)
Solving Drift: n Delete
13
1 6
5
4 3
2
0MAX_QUEUE-1
i
a
e
o
BACK
CS200 -Queues
![Page 14: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/14.jpg)
Solving Drift: n Delete
14
1 6
5
4 3
2
0MAX_QUEUE-1
i
e
o
BACK
FRONT
CS200 -Queues
![Page 15: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/15.jpg)
Solving Drift n Insert u
15
1 6
5
4 3
2
0MAX_QUEUE-1
i
o
FRONT
u
When either front or back advances past MAX_QUEUE-1, it wraps around (to 0: using % MAX_QUEUE)
CS200 -Queues
![Page 16: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/16.jpg)
Queue with Single Item n back and front are pointing at the same slot.
16
1 6
5
4 3
2
0MAX_QUEUE-1
u
CS200 -Queues
![Page 17: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/17.jpg)
Empty Queue: remove Single Item
Remove last item. q front passed back.
17
1 6
5
4 3
2
0MAX_QUEUE-1
u
CS200 -Queues
![Page 18: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/18.jpg)
Insert the last item back catches up to front when the queue becomes full.
18
1 6
5
4 3
2
0MAX_QUEUE-1
u
BACK
o
i
e
a
b
f
n
When the queue is FULL, front is one slot ahead of back as well.
Problem?
CS200 -Queues
Maintain size: 0:empty max_queue: full
Solution?
![Page 19: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/19.jpg)
Wrapping the values for front and back
n Initializing front = 0!back = MAX_QUEUE-1!count = 0!
n Adding !back = (back+1) % MAX_QUEUE;!!items[back] = newItem;!!++count;!
!n Deleting !deleteItem = items[front]; !front = (front +1) % MAX_QUEUE;!!--count;!
19 CS200 -Queues
![Page 20: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/20.jpg)
enqueue with Array
public void enqueue(Object newItem) throws QueueException{!
if (!isFull()){! back = (back+1) % (MAX_QUEUE);! items[back] = newItem;! ++count;!
}else { ! throw new QueueException(your_message);! }!}!
20 CS200 -Queues
![Page 21: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/21.jpg)
dequeue()
public Object dequeue() throws QueueException{!
if (!isEmpty()){! Object queueFront = items[front];! front = (front+1) % (MAX_QUEUE);! --count;! return queueFront;! }else{!
throw new QueueException (your_message);! }!}!
21 CS200 -Queues
![Page 22: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/22.jpg)
Implementation with (Array)List
n You can implement operation dequeue() as the list operation remove(0).
n peek() as get(0)!n enqueue() as add(newItem) // at tail!
22
a e i o
Front of queue Back of queue
Position in the list 0 1 2 3
CS200 -Queues
![Page 23: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/23.jpg)
Questions
n What is an advantage of the circular array implementation over linked list?
A. Faster to enqueue B. Uses less memory C. Can more easily fix and enforce a maximum
size D. Fewer allocations
CS200 -Queues 23
![Page 24: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/24.jpg)
Expressions: infix to postfix conversion
Prichard: 7.4 Let’s do some 2 + 3 * 4 2 * 3 + 4 2 + 3 - 4 2 + (3 - 4) 1 - (2 + 3 * 4) / 5 observations?
CS200 -Queues 24
![Page 25: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/25.jpg)
Expressions: infix to postfix conversion
2 + 3 * 4 à 2 3 4 * + 2 * 3 + 4 à 2 3 * 4 + 2 + 3 – 4 à 2 3 + 4 - 2 + (3 - 4) à 2 3 4 - + 1 - (2 + 3 * 4) / 5 à 1 2 3 4 * + 5 / - 1. operand order does not change 2. operators come after second operand and obey
associativity and precedence rules 3. ( ) converts the inner expression to an independent
postfix expression
CS200 -Queues 25
![Page 26: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/26.jpg)
infix to postfix implementation
n Use a queue to create the resulting postfix expression q the operands get immediately enqueued
n Use a stack to store the operators q operators get pushed on the stack
n when to pop and enqueue? q let’s play
CS200 -Queues 26
![Page 27: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/27.jpg)
2 + 3 * 4 stack queue action 2 + 3 * 4 enqueue + 3 * 4 2 push 3 * 4 + 2 enqueue * 4 + 2 3 push
* 4 + 2 3 enqueue * + 2 3 4 pop; enqueue + 2 3 4 * pop; enqueue 2 3 4 * +
CS200 -Queues 27
![Page 28: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/28.jpg)
2 * 3 + 4 stack queue action 2 * 3 + 4 enqueue * 3 + 4 2 push 3 + 4 * 2 enqueue + 4 * 2 3 push? NO!! Because * has higher precedence than + and so binds to 2 3 + 4 * 2 3 pop; enqueue + 4 2 3 * push 4 + 2 3 * enqueue + 2 3 * 4 pop; enqueue 2 3 * 4 +
CS200 -Queues 28
![Page 29: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/29.jpg)
2 - 3 + 4 stack queue action 2 - 3 + 4 enqueue - 3 + 4 2 push 3 + 4 - 2 enqueue + 4 - 2 3 push? NO!! Because of left associativity – binds to 2 3 + 4 - 2 3 pop; enqueue + 4 2 3 - push 4 + 2 3 - enqueue + 2 3 - 4 pop; enqueue 2 3 - 4 +
CS200 -Queues 29
![Page 30: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/30.jpg)
2 – ( 3 + 4 ) stack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the ( ) makes its own independent postfix, so we push the ( then use the stack as before until we see a ) then we pop all the operators off the stack and enqueue them, until we see a ( and delete the ( ( 3 + 4 ) - 2 enqueue ( + 4 ) - 2 3 push + ( 4 ) - 2 3 enqueue continued next page
CS200 -Queues 30
![Page 31: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/31.jpg)
2 – ( 3 + 4 ) stack queue action + ( 4 ) - 2 3 enqueue + ( ) - 2 3 4 pop, enqueue until (, delete ( - 2 3 4 + pop, enqueue until stack empty 2 3 4 + -
CS200 -Queues 31
![Page 32: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/32.jpg)
in2post algorithm when encountering
operand: enqueue open: push close: pop and enqueue operators, until open on stack pop open operator: if stack empty or top is open push, else pop and enqueue operators with greater or equal precedence, until operator with lower precedence on stack, or open on stack, or stack empty end of input: pop and enqueue all operators until stack empty
CS200 -Queues 32
Do it for: 1-(2+3*4)/5
![Page 33: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/33.jpg)
What about unary operators?
n e.g. not in logic expressions such as: not true and false not ( true or false ) not not true not has higher priority than and, true and not false is true and (not false)
and has higher priority than or not is right associative not not true is not ( not true )
CS200 -Queues 33
![Page 34: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/34.jpg)
not true and false stack queue action not true and false push true and false not enqueue and false not true not higher priority pop, enqueue not and false true not push false and true not enqueue and true not false pop, enqueue true not false and CS200 -Queues 34
![Page 35: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/35.jpg)
not(true or false) stack queue action not (true or false) push (true or false) not push ( true or false) not enqueue ( or false ) not true push or ( false ) not true enqueue
CS200 -Queues 35
![Page 36: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/36.jpg)
not(true or false) continued
stack queue action or ( false ) not true enqueue or ( pop, enqueue ) not true false until ( not true false or pop, enqueue true false or not
CS200 -Queues 36
![Page 37: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/37.jpg)
not not true stack queue action not not true push not true not push or enqueue? push! not is right associative, its operand is ahead of it not true not enqueue not not true pop and enqueue true not not
CS200 -Queues 37
![Page 38: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/38.jpg)
in2post algorithm when encountering
operand: enqueue open: push close: pop and enqueue operators, until open on stack pop open end of input: pop and enqueue all operators until stack empty
CS200 -Queues 38
![Page 39: CS200: Queues › ~cs200 › Fall15 › slides › 03-queues.pdfstack queue action 2 – ( 3 + 4 ) enqueue - ( 3 + 4 ) 2 push ( 3 + 4 ) - 2 enqueue or push? the expression inside the](https://reader033.fdocuments.us/reader033/viewer/2022052801/5f13ee050414a6799e30ed34/html5/thumbnails/39.jpg)
in2post continued when encountering
and, or: if stack empty or top is open push, else pop and enqueue operators with greater or equal precedence, until operator with lower precedence on stack, or open on stack, or stack empty not: push do it for not (not true or false)
CS200 -Queues 39