1 stacks and queues sections 3.6 and 3.7 chapter 3 lists, stacks, and queues abstract data types,...

62
1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

Upload: job-black

Post on 23-Dec-2015

249 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

1

Stacks and Queues

Sections 3.6 and 3.7

Chapter 3 Lists, Stacks, and QueuesAbstract Data Types, Vectors

Page 2: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

2

Stack ADT - LIFO

• Collections: – Elements of some proper type T

• Operations: – Feature: Last In, First Out

– void push(T t)

– void pop()

– T top()

– bool empty()

– unsigned int size()

– constructor and destructor

Page 3: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

3

Stack Model—LIFO

• Empty stack S– S.empty() is true

– S.top() not defined

– S.size() == 0

food chain stack

Page 4: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

4

Stack Model—LIFO

• S.push(“mosquito”)– S.empty() is false

– S.top() == “mosquito”

– S.size() == 1

mosquito

food chain stack

Page 5: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

5

Stack Model—LIFO

• S.push(“fish”)– S.empty() is false

– S.top() == “fish”

– S.size() == 2

fish

mosquito

food chain stack

Page 6: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

6

Stack Model—LIFO

• S.push(“raccoon”)– S.empty() is false

– S.top() == “raccoon”

– S.size() == 3

raccoon

fish

mosquito

food chain stack

Page 7: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

7

Stack Model—LIFO

• S.pop()– S.empty() is false

– S.top() == “fish”

– S.size() == 2

fish

mosquito

food chain stack

Page 8: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

8

Implementations and Uses of Stack ADT

• Implementations – Any list implementation– list and vector C++ STL– Vector/List ADTs

• push_back()/pop_back() • push_front()/?

• Uses– Depth first search / backtracking– Evaluating postfix expressions– Converting infix to postfix– Function calls (runtime stack)– Recursion

Page 9: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

9

Depth First Search—Backtracking

• Problem– Discover a path from start to goal

• Solution– Start from

• Node start

– Stop • If node is goal

– Go deep• If there is an unvisited

neighbor, go there

– Backtrack• Retreat along the path to find

an unvisited neighbor, if cannot go deeper

• Outcome– If there is a path from start to goal, DFS finds one such path

1

2 3 4

5 6 87

9 10 1211

start

goal

Page 10: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

10

Depth First Search—Backtracking (2)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal1Push

Page 11: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

11

Depth First Search—Backtracking (3)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal

2

1Push

Push

Page 12: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

12

Depth First Search—Backtracking (4)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal

5

2

1Push

Push

Push

Page 13: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

13

Depth First Search—Backtracking (5)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal

6

5

2

1Push

Push

Push

Push

Page 14: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

14

Depth First Search—Backtracking (6)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal

9

6

5

2

1Push

Push

Push

Push

Push

Page 15: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

15

Depth First Search—Backtracking (7)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal

6

5

2

1Push

Push

Push

Push

Pop

Page 16: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

16

Depth First Search—Backtracking (8)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal

5

2

1Push

Push

Push

Pop

Page 17: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

17

Depth First Search—Backtracking (9)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal

2

1Push

Push

Pop

Page 18: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

18

Depth First Search—Backtracking (10)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal1Push

Pop

Page 19: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

19

Depth First Search—Backtracking (11)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal

3

1Push

Push

Page 20: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

20

Depth First Search—Backtracking (12)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal

7

3

1Push

Push

Push

Page 21: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

21

Depth First Search—Backtracking (13)

• Stack

1

2 3 4

5 6 87

9 10 1211

start

goal

10

7

3

1Push

Push

Push

Push

Page 22: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

22

DFS Implementation

DFS() {stack<location> S;

// mark the start location as visitedS.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);

}

Page 23: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

23

DFS Implementation (2)

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

S.pop();}

}

Success(S) {// print successwhile (!S.empty()) {

output(S.top());S.pop();

}}

Page 24: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

24

Runtime Stack

• Runtime environment– Static

• Executable code

• Global variables

– Stack• Push for each function call

• Pop for each function return

• Local variables

– Heap• Dynamically allocated memories

• new and delete

static

stack

heap

program memory

Page 25: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

25

Queue ADT - FIFO

• Collection– Elements of some proper type T

• Operations– Feature: First In, First Out– void push(T t)– void pop()– T front()– bool empty()– unsigned int size()– Constructors and destructors

Page 26: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

26

Queue Model—FIFO

• Empty Q

animal parade queue

Page 27: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

27

Queue Model—FIFO

• Q.Push(“ant”)

ant

front

back

animal parade queue

Page 28: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

28

Queue Model—FIFO

• Q.Push(“bee”)

ant bee

front

back

animal parade queue

Page 29: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

29

Queue Model—FIFO

• Q.Push(“cat”)

ant bee cat

front

back

animal parade queue

Page 30: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

30

Queue Model—FIFO

• Q.Push(“dog”)

ant bee cat dog

front

back

animal parade queue

Page 31: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

31

Queue Model—FIFO

• Q.Pop()

bee cat dog

front

back

animal parade queue

Page 32: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

32

Queue Model—FIFO

• Q.Pop()

cat dog

front

back

animal parade queue

Page 33: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

33

Queue Model—FIFO

• Q.Push(“eel”)• Q.Pop()• Q.Pop()

eel

front

back

animal parade queue

Page 34: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

34

Implementations and Uses of Queue ADT

• Implementations– Any list implementation

•push_front()/pop_back()•push_back()/?

• Uses– Buffers– Breadth first search– Simulations– Producer-Consumer Problems

Page 35: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

35

Breadth First Search

• Problem– Find a shortest path from start to goal

• Solution– Start from

• Node start

– Visit• All neighbors of the node

– Stop • If a neighbor is goal

– Otherwise• Visit neighbors two hops

away

– Repeat (Stop/Otherwise)• Visiting neighbors N hops

away

1

2 3 4

5 6 87

9 10 1211

start

goal

Page 36: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

36

Breadth First Search (2)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

1

Push

Page 37: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

37

Breadth First Search (3)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

Pop

Page 38: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

38

Breadth First Search (4)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

2 3 4

Push Push Push

Page 39: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

39

Breadth First Search (5)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

Pop

3 4

Page 40: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

40

Breadth First Search (6)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

3 4 5 6

Push Push

Page 41: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

41

Breadth First Search (7)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

4 5 6

Pop

Page 42: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

42

Breadth First Search (8)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

4 5 6 7 8

Push Push

Page 43: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

43

Breadth First Search (9)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

5 6 7 8

Pop

Page 44: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

44

Breadth First Search (10)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

6 7 8

Pop

Page 45: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

45

Breadth First Search (11)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

7 8

Pop

Page 46: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

46

Breadth First Search (12)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

7 8 9

Push

Page 47: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

47

Breadth First Search (13)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

8 9

Pop

Page 48: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

48

Breadth First Search (14)

• Queue

1

2 3 4

5 6 87

9 10 1211

start

goal

8 9 10

Push

Page 49: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

49

BFS Implementation

BFS {queue<location> Q;

// mark the start location as visitedQ.push(start);while (Q is not empty) {

t = Q.front();for (// each unvisited neighbor n of node

t) {Q.push(n);if (n == goal) Success(S);

}Q.pop();

}Failure(Q);

}

Page 50: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

50

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

Page 51: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

51

Stack Adaptor Requirements

• Stack– push()– pop()– top()– empty()– size()

• Can use List, Deque– Push(): push_back()– Pop(): pop_back()

Page 52: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

52

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

};

• Declaration– Stack<float, List<float> > floatStack;– Stack<int, Vector<int> > intStack;

• For STL stack container– template <typename T, typename Container = deque<T> > class stack;– stack<char> charStack;

Page 53: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

53

Queue Adaptor Requirements

• Queue– push()– pop ()– front()– empty()– size()

• Can use List, Deque– Push(): push_front()– Pop(): pop_back()

Page 54: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

54

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

};

• DeclarationQueue<float, List<float> > floatQueue;Queue<int, List<int> > intQueue;

• For STL stack containertemplate <typename T, typename Container = deque<T> > class queue;queue<char> charQueue;

Page 55: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

55

Circular Array

frontback

animal parade queue

Page 56: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

56

Circular Array

• Q.Push(“ant”)

ant

front

back

animal parade queue

Page 57: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

57

Queue Model—FIFO

• Q.Push(“bee”)

ant bee

front back

animal parade queue

Page 58: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

58

Queue Model—FIFO

• Q.Push(“cat”)

cat ant bee

frontback

animal parade queue

Page 59: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

59

Queue Model—FIFO

• Q.Push(“dog”)

cat dog ant bee

frontback

animal parade queue

Page 60: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

60

Queue Model—FIFO

• Q.Pop()

cat dog bee

frontback

animal parade queue

Page 61: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

61

Queue Model—FIFO

• Q.Pop()

cat dog

front back

animal parade queue

Page 62: 1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

62

Expanding the Array

a b c a b c

Where are front and back?

Why can’t we use all four locations?

c a b c a b

c a b a b c