Top Banner
Marco Alvarez Department of Computer Science and Statistics University of Rhode Island CSC 212: Data Structures and Abstractions Stacks and Queues Spring 2020 2 Stacks LIFO: Last In First Out Basic Operations Push inserts one element onto the stack Pop returns the element at the top of the stack (and removes it) IsEmpty not necessary, but sometimes useful 3 4 https://en.cppreference.com/w/cpp/container/stack
5

Stacks · Dijkstra’s two stacks algorithm 10 s1 s2 Element Action operand (value) push it onto the s1 operator push it onto s2 left parenthesis ignore right parenthesis pop operator

Jul 13, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Stacks · Dijkstra’s two stacks algorithm 10 s1 s2 Element Action operand (value) push it onto the s1 operator push it onto s2 left parenthesis ignore right parenthesis pop operator

Marco AlvarezDepartment of Computer Science and Statistics

University of Rhode Island

CSC 212: Data Structures and AbstractionsStacks and Queues

Spring 2020

2

Stacks

LIFO: Last In First Out

Basic Operations‣ Push

✓ inserts one element onto the stack

‣ Pop✓ returns the element at the top of the stack (and removes it)

‣ IsEmpty✓ not necessary, but sometimes useful

3 4https://en.cppreference.com/w/cpp/container/stack

Page 2: Stacks · Dijkstra’s two stacks algorithm 10 s1 s2 Element Action operand (value) push it onto the s1 operator push it onto s2 left parenthesis ignore right parenthesis pop operator

Implementation‣ Arrays

✓ push and pop at the end of the array (easier and efficient)✓ can be fixed-length✓ can also use a dynamic array (grows over time)

- additional cost for dynamic arrays

5

https://www.cs.usfca.edu/~galles/visualization/StackArray.html

30 1 20 14

top

Implementation‣ Linked Lists

✓ push and pop at front (could use the other end as well)

6https://www.cs.usfca.edu/~galles/visualization/StackLL.html

1 7 3 5

Head

Considerations‣ Underflow

✓ error can be thrown when calling pop on an empty stack

‣ Overflow✓ error can be thrown when calling push on a full stack

(especially in fixed-length implementations)

7

Applications‣ Undo in software applications

‣ Stack in compilers/programming languages

‣ Parsing expressions

‣ …

8

Page 3: Stacks · Dijkstra’s two stacks algorithm 10 s1 s2 Element Action operand (value) push it onto the s1 operator push it onto s2 left parenthesis ignore right parenthesis pop operator

Example‣ Fully parenthesized infix expressions

✓ infix arithmetic expressions: operators are placed between two operands

✓ fully parenthesized infix expression: infix arithmetic expression where every operator and its arguments are contained in parentheses

✓ operator precedence and associativity don't matter

9

((5 + ((10 - 4) * (3+2))) + 25)

Dijkstra’s two stacks algorithm

10

s1 s2

Element Action

operand (value) push it onto the s1

operator push it onto s2

left parenthesis ignore

right parenthesis pop operator from s2 and pop two values from s1, then apply operator to those values and push the result onto s1

((5 + ((10 - 4) * (3+2))) + 25)

11(🗑 #🔝₁ ⊚🔝₂ )🍿₁🍿₁🍿₂🔝₁

12

Queues

FIFO: First In First Out

Page 4: Stacks · Dijkstra’s two stacks algorithm 10 s1 s2 Element Action operand (value) push it onto the s1 operator push it onto s2 left parenthesis ignore right parenthesis pop operator

Basic Operations‣ Enqueue

✓ inserts one element onto the queue

‣ Dequeue✓ returns the next element from the queue (and removes it)

‣ IsEmpty✓ not necessary, but sometimes useful

13 14https://en.cppreference.com/w/cpp/container/queue

Basic Operations (enqueue/dequeue)

15

Implementation‣ Arrays

✓ enqueue and dequeue at different ends of the array✓ can be fixed-length✓ can also use a dynamic array (grows over time)

- additional cost for dynamic arrays

16

https://www.cs.usfca.edu/~galles/visualization/QueueArray.html

20 14 21 3 12

topbase

Page 5: Stacks · Dijkstra’s two stacks algorithm 10 s1 s2 Element Action operand (value) push it onto the s1 operator push it onto s2 left parenthesis ignore right parenthesis pop operator

Implementation‣ Linked Lists

✓ enqueue and dequeue at different ends

17https://www.cs.usfca.edu/~galles/visualization/QueueLL.html

1 7 3 5

Head Tail

Considerations‣ Underflow

✓ error can be thrown when calling dequeue on an empty queue

‣ Overflow✓ error can be thrown when calling enqueue on a full queue

(especially in fixed-length implementations)

18

Applications‣ Media Playlists (Youtube, Spotify, Music, etc.)

‣ Process management in Operating Systems

‣ Simulations

‣ Used in other algorithms

‣ …

19