Docsity

Lecture Slides on Stacks and Queues - Data Structure Algorithms | COP 4530, Study notes of Computer Science

Toggle sidebar
bg1
1
Stacks and Queues
Sections 3.6 and 3.7
Stack ADT
Collections:
ļ‚”Elements of some proper type T
Operations:
ļ‚”void push(T t)
ļ‚”void pop()
ļ‚”T top()
ļ‚”bool empty()
ļ‚”unsigned int size()
ļ‚”constructor and destructor
Uses of ADT Stack
Depth first search / backtracking
Evaluating postfix expressions
Converting infix to postfix
Function calls (runtime stack)
Recursion
Stack Model—LIFO
Empty stack S
ļ‚”S.empty() is true
ļ‚”S.top() not defined
ļ‚”S.size() == 0
S.push(ā€œmosquitoā€)
ļ‚”S.empty() is false
ļ‚”S.top() == ā€œmosquitoā€
ļ‚”S.size() == 1
S.push(ā€œfishā€)
ļ‚”S.empty() is false
ļ‚”S.top() == ā€œfishā€
ļ‚”S.size() == 2 food chain stack
Stack Model—LIFO
S.push(ā€œraccoonā€)
ļ‚”S.empty() is false
ļ‚”S.top() ==
ā€œraccoonā€
ļ‚”S.size() == 3
S.pop()
ļ‚”S.empty() is false
ļ‚”S.top() == ā€œfishā€
ļ‚”S.size() == 2
food chain stack
Queue ADT - FIFO
Collection
ļ‚”Elements of some proper type T
Operations
ļ‚”void push(T t)
ļ‚”void pop()
ļ‚”T front()
ļ‚”bool empty()
ļ‚”unsigned int size()
ļ‚”Constructors and destructors
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Slides on Stacks and Queues - Data Structure Algorithms | COP 4530 and more Study notes Computer Science in PDF only on Docsity!

Stacks and Queues

Sections 3.6 and 3.

Stack ADT

 Collections:

ļ‚”Elements of some proper type T

 Operations:

ļ‚” void push(T t)

ļ‚” void pop()

ļ‚” T top()

ļ‚” bool empty()

ļ‚” unsigned int size()

ļ‚”constructor and destructor

Uses of ADT Stack

 Depth first search / backtracking

 Evaluating postfix expressions

 Converting infix to postfix

 Function calls (runtime stack)

 Recursion

Stack Model—LIFO

 Empty stack S

ļ‚” S.empty() is true ļ‚” S.top() not defined ļ‚” S.size() == 0

 S.push(ā€œmosquitoā€)

ļ‚” S.empty() is false ļ‚” S.top() == ā€œmosquitoā€ ļ‚” S.size() == 1

 S.push(ā€œfishā€)

ļ‚” S.empty() is false ļ‚” S.top() == ā€œfishā€ ļ‚” S.size() == 2 (^) food chain stack Stack Model—LIFO

 S.push(ā€œraccoonā€)

ļ‚” S.empty() is false

ļ‚” S.top() ==

ā€œraccoonā€

ļ‚” S.size() == 3

 S.pop()

ļ‚” S.empty() is false

ļ‚” S.top() == ā€œfishā€

ļ‚” S.size() == 2

food chain stack Queue ADT - FIFO

 Collection

ļ‚”Elements of some proper type T

 Operations

ļ‚” void push(T t)

ļ‚” void pop()

ļ‚” T front()

ļ‚” bool empty()

ļ‚” unsigned int size()

ļ‚”Constructors and destructors

Queue Model—FIFO

 Empty Q

animal_parade_queue front back Queue Model—FIFO

 Q.push( ā€œantā€ )

 Q.push( ā€œbeeā€ )

 Q.push( ā€œcatā€ )

 Q.push( ā€œdogā€ )

front back animal_parade_queue back back back back Queue Model—FIFO

 Q.pop();

 Q.pop();

 Q.push( ā€œeelā€ );

 Q.pop();

 Q.pop();

front animal_parade_queue back back back back Uses of ADT Queue

 Buffers

 Breadth first search

 Simulations

 Producer-Consumer Problems

Depth First Search—Backtracking

 Problem

ļ‚” Discover a path from start to goal

 Solution

ļ‚” Go deep  If there is an unvisited neighbor, go there ļ‚” Backtrack  Retreat along the path to find an unvisited neighbor

 Outcome

ļ‚” If there is a path from start to goal , DFS finds one such path

start goal Depth First Search—Backtracking (2)

 Stack

start goal

Push

Depth First Search—Backtracking (9)

 Stack

start goal

Push^2

Push Pop Depth First Search—Backtracking (10)

 Stack

start goal

Push Pop Depth First Search—Backtracking (11)

 Stack

start goal

Push^3

Push Depth First Search—Backtracking (12)

 Stack

start goal

Push Push Push Depth First Search—Backtracking (13)

 Stack

start goal

Push Push Push Push DFS Implementation DFS() { stack S; // mark the start location as visited S.push(start); while (S is not empty) { t = S.top(); if (t == goal) Success(S); if (// t has unvisited neighbors) { // choose an unvisited neighbor n // mark n visited; S.push(n); } else { BackTrack(S); } } Failure(S); }

DFS Implementation (2) BackTrack while (!S.empty() && S.top() has no unvisited neighbors) {(S) { }^ S.pop(); } Success(S) { // print success while (!S.empty()) { output(S.top()); }^ S.pop(); } Failure(S) { // print failure while (!S.empty()) { S.pop(); }^ } Breadth First Search

 Problem

ļ‚”Find a shortest path from start to goal

start goal Breadth First Search (2)

 Queue

start goal

push Breadth First Search (3)

 Queue

start goal pop Breadth First Search (4)

 Queue

start goal

pop pop pop Breadth First Search (5)

 Queue

start goal pop

Breadth First Search (12)

 Queue

start goal

Push Breadth First Search (13)

 Queue

start goal

Pop Breadth First Search (14)

 Queue

start goal

Push BFS Implementation BFS { queue Q; // mark the start location as visited Q.push(start); while (Q is not empty) { t = Q.front(); for (// each unvisited neighbor n) { Q.push(n); if (n == goal) Success(S); } Q.pop(); } Failure(Q); } Evaluating Postfix Expressions

 Postfix expressions: operands precede operator

 Tokens: atomics of expressions, either operator

or operand

 Example:

ļ‚”z = 25 + x*(y – 5)

ļ‚”Tokens: z, =, 25, +, x, *, (, y, -, 5, )

Evaluating Postfix Expressions (2)

 Evaluation algorithm:

ļ‚”Use stack of tokens ļ‚”Repeat

If operand, push onto stack

If operator

  • pop operands off the stack
  • evaluate operator on operands
  • push result onto stack

Until expression is read

Return top of stack

Evaluating Postfix Expressions (3)

 Most CPUs have hardware support for this

algorithm

 Translation from infix to postfix also uses a

stack (software)

Evaluating Postfix Expressions (4)

 Original expression: 1 + (2 + 3) * 4 + 5

 Evaluate: 1 2 3 + 4 * + 5 +

Evaluating Postfix Expressions (5)

 Input: 1 2 3 + 4 * + 5 +

 Push(1)

Evaluating Postfix Expressions (6)

 Input: 2 3 + 4 * + 5 +

 Push(2)

Evaluating Postfix Expressions (7)

 Input: 3 + 4 * + 5 +

 Push(3)

Evaluating Postfix Expressions (8)

 Input: + 4 * + 5 +

 Pop() == 3

 Pop() == 2

Evaluating Postfix Expressions (15)

 Input: 5 +

 Push(5)

Evaluating Postfix Expressions (16)

 Input: +

 Pop() == 21

 Pop() == 5

Evaluating Postfix Expressions (17)

 Input: +

 Push(21 + 5)

Evaluating Postfix Expressions (18)

 Input:

 Pop() == 26

Postfix Evaluation Implementation Evaluate(postfix expression) { // use stack of tokens; while(// expression is not empty) { t = next token; if (t is operand) { // push onto stack } else { // pop operands for t off stack // evaluate t on these operands // push result onto stack } } // return top of stack } Reading Exercise

 How to use stack to

ļ‚”Balance brackets ļ‚”Convert infix to postfix

Runtime Stack

 Runtime environment

ļ‚”Static

Executable code

Global variables

ļ‚”Stack

Push for each function call

Pop for each function return

ļ‚”Heap

Dynamic new and delete

static stack heap program memory Recursion

 Order 1:

ļ‚”function calls itself

 Order 2:

ļ‚” f() calls g() , and g() calls f()

 Facilitated by stack

Adaptor Class

 Adapts the public interface of another class

 Adaptee: the class being used

 Adaptor: the new class being defined

 Uses protected object of the adaptee type

 Uses the adaptee’s methods to define adaptor

methods

 Stack and Queue implemented via adaptor

classes

Stack Adaptor Requirements

 Stack

ļ‚” push() ļ‚” pop() ļ‚” top() ļ‚” empty() ļ‚” size()

 Can use List, Deque

Queue Adaptor Requirements

 Queue

ļ‚” push() ļ‚” pop () ļ‚” front() ļ‚” back() ļ‚” empty() ļ‚” size()

 Can use List, Deque

Class Stack template <typename T, class Container> class Stack { protected: Container c; public: void push(const T & x) { c.push_back(x); } void pop() { c.pop_back(); } T top() const { return c.back(); } int empty() const { return c.empty(); } unsigned int size() const { return c.size(); } void clear() { c.clear(); } };