Stacks and queues sections 3 6 and 3 7
Download
1 / 68

Stacks and Queues Sections 3.6 and 3.7 - PowerPoint PPT Presentation


  • 75 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Stacks and Queues Sections 3.6 and 3.7' - xander


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Stacks and queues sections 3 6 and 3 7

Stacks and QueuesSections 3.6 and 3.7


Stack adt
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
Uses of ADT Stack

  • Depth first search / backtracking

  • Evaluating postfix expressions

  • Converting infix to postfix

  • Function calls (runtime stack)

  • Recursion


Stack model lifo
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

"fish"

"mosquito"

food chain stack


Stack model lifo1
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

"raccoon"

"fish"

"mosquito"

food chain stack


Queue adt fifo
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

back

front

Queue Model—FIFO

  • Empty Q

animal_parade_queue


Queue model fifo1

back

back

back

back

back

front

Queue Model—FIFO

  • Q.push( “ant” )

  • Q.push( “bee” )

  • Q.push( “cat” )

  • Q.push( “dog” )

animal_parade_queue

ANT

BEE

CAT

DOG


Queue model fifo2

back

back

back

back

front

Queue Model—FIFO

  • Q.pop();

  • Q.pop();

  • Q.push( “eel” );

  • Q.pop();

  • Q.pop();

animal_parade_queue

BEE

DOG

CAT

EEL

ANT

DOG

EEL

BEE

CAT

EEL

DOG

CAT

DOG


Uses of adt queue
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

Depth First Search—Backtracking

start

1

2

3

4

5

6

7

8

9

10

11

12

goal


Depth first search backtracking 2
Depth First Search—Backtracking (2)

  • Stack

1

start

2

3

4

5

6

7

8

9

10

11

12

goal

Push


Depth first search backtracking 3
Depth First Search—Backtracking (3)

  • Stack

1

start

2

3

4

5

6

7

8

9

10

11

12

Push

goal

Push


Depth first search backtracking 4
Depth First Search—Backtracking (4)

  • Stack

1

start

2

3

4

5

6

7

8

Push

9

10

11

12

Push

goal

Push


Depth first search backtracking 5
Depth First Search—Backtracking (5)

  • Stack

1

start

2

3

4

Push

5

6

7

8

Push

9

10

11

12

Push

goal

Push


Depth first search backtracking 6
Depth First Search—Backtracking (6)

  • Stack

1

start

Push

2

3

4

Push

5

6

7

8

Push

9

10

11

12

Push

goal

Push


Depth first search backtracking 7
Depth First Search—Backtracking (7)

  • Stack

1

start

Pop

2

3

4

Push

5

6

7

8

Push

9

10

11

12

Push

goal

Push


Depth first search backtracking 8
Depth First Search—Backtracking (8)

  • Stack

1

start

2

3

4

Pop

5

6

7

8

Push

9

10

11

12

Push

goal

Push


Depth first search backtracking 9
Depth First Search—Backtracking (9)

  • Stack

1

start

2

3

4

5

6

7

8

Pop

9

10

11

12

Push

goal

Push


Depth first search backtracking 10
Depth First Search—Backtracking (10)

  • Stack

1

start

2

3

4

5

6

7

8

9

10

11

12

Pop

goal

Push


Depth first search backtracking 11
Depth First Search—Backtracking (11)

  • Stack

1

start

2

3

4

5

6

7

8

9

10

11

12

Push

goal

Push


Depth first search backtracking 12
Depth First Search—Backtracking (12)

  • Stack

1

start

2

3

4

5

6

7

8

Push

9

10

11

12

Push

goal

Push


Depth first search backtracking 13
Depth First Search—Backtracking (13)

  • Stack

1

start

2

3

4

Push

5

6

7

8

Push

9

10

11

12

Push

goal

Push


Dfs implementation
DFS Implementation

DFS() {

stack<location> 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
DFS Implementation (2)

BackTrack(S) {

while (!S.empty() && S.top() has no unvisited neighbors) {

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
Breadth First Search

  • Problem

    • Find a shortest path from start to goal

start

1

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 2
Breadth First Search (2)

  • Queue

push

start

1

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 3
Breadth First Search (3)

  • Queue

pop

start

1

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 4
Breadth First Search (4)

  • Queue

pop

pop

pop

start

1

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 5
Breadth First Search (5)

  • Queue

pop

start

1

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 6
Breadth First Search (6)

  • Queue

push

push

1

start

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 7
Breadth First Search (7)

  • Queue

Pop

1

start

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 8
Breadth First Search (8)

  • Queue

Push

Push

1

start

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 9
Breadth First Search (9)

  • Queue

Pop

1

start

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 10
Breadth First Search (10)

  • Queue

Pop

1

start

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 11
Breadth First Search (11)

  • Queue

Pop

1

start

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 12
Breadth First Search (12)

  • Queue

Push

1

start

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 13
Breadth First Search (13)

  • Queue

Pop

1

start

2

3

4

5

6

7

8

9

10

11

12

goal


Breadth first search 14
Breadth First Search (14)

  • Queue

Push

1

start

2

3

4

5

6

7

8

9

10

11

12

goal


Bfs implementation
BFS Implementation

BFS {

queue<location> 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
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
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
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
Evaluating Postfix Expressions (4)

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

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


Evaluating postfix expressions 5
Evaluating Postfix Expressions (5)

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

  • Push(1)


Evaluating postfix expressions 6
Evaluating Postfix Expressions (6)

  • Input: 2 3 + 4 * + 5 +

  • Push(2)


Evaluating postfix expressions 7
Evaluating Postfix Expressions (7)

  • Input: 3 + 4 * + 5 +

  • Push(3)


Evaluating postfix expressions 8
Evaluating Postfix Expressions (8)

  • Input: + 4 * + 5 +

  • Pop() == 3

  • Pop() == 2


Evaluating postfix expressions 9
Evaluating Postfix Expressions (9)

  • Input: + 4 * + 5 +

  • Push(2 + 3)


Evaluating postfix expressions 10
Evaluating Postfix Expressions (10)

  • Input: 4 * + 5 +

  • Push(4)


Evaluating postfix expressions 11
Evaluating Postfix Expressions (11)

  • Input: * + 5 +

  • Pop() == 4

  • Pop() == 5


Evaluating postfix expressions 12
Evaluating Postfix Expressions (12)

  • Input: * + 5 +

  • Push(5 * 4)


Evaluating postfix expressions 13
Evaluating Postfix Expressions (13)

  • Input: + 5 +

  • Pop() == 20

  • Pop() == 1


Evaluating postfix expressions 14
Evaluating Postfix Expressions (14)

  • Input: + 5 +

  • Push(1 + 20)



Evaluating postfix expressions 16
Evaluating Postfix Expressions (16)

  • Input: +

  • Pop() == 21

  • Pop() == 5




Postfix evaluation implementation
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
Reading Exercise

  • How to use stack to

    • Balance brackets

    • Convert infix to postfix


Runtime stack
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

heap

stack

static

program memory


Recursion
Recursion

  • Order 1:

    • function calls itself

  • Order 2:

    • f() calls g(), and g() calls f()

  • Facilitated by stack


Adaptor class
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 Adaptor Requirements

  • Stack

    • push()

    • pop()

    • top()

    • empty()

    • size()

  • Can use List, Deque


Queue adaptor requirements
Queue Adaptor Requirements

  • Queue

    • push()

    • pop ()

    • front()

    • back()

    • empty()

    • size()

  • Can use List, Deque


Class stack
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(); }

};


Declarations
Declarations

  • Stack< float, List<float> > floatStack;

  • Stack< int, Vector<int> > intStack;


Class queue
Class Queue

template <typename T, class Container>

class Queue {

protected:

Container c;

public:

void push(const T & x) { c.push_back(x); }

void pop() { c.pop_front(); }

T front() const { return c.front(); }

int empty() const { return c.empty(); }

unsigned int size() const { return c.size(); }

void clear() { c.clear(); }

};


ad