Presentation is loading. Please wait.

Presentation is loading. Please wait.

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() 

Similar presentations


Presentation on theme: "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() "— Presentation transcript:

1 Stacks and Queues Sections 3.6 and 3.7

2 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

3 Uses of ADT Stack Depth first search / backtracking Evaluating postfix expressions Converting infix to postfix Function calls (runtime stack) Recursion

4 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

5 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

6 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

7 Queue Model—FIFO Empty Q animal_parade_queue front back

8 Queue Model—FIFO Q.push( “ant” ) Q.push( “bee” ) Q.push( “cat” ) Q.push( “dog” ) front back animal_parade_queue back

9 Queue Model—FIFO Q.pop(); Q.push( “eel” ); Q.pop(); front animal_parade_queue back

10 Uses of ADT Queue Buffers Breadth first search Simulations Producer-Consumer Problems

11 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

12 Depth First Search—Backtracking (2) Stack start goal 1 Push

13 Depth First Search—Backtracking (3) Stack start goal 2 1 Push

14 Depth First Search—Backtracking (4) Stack start goal Push

15 Depth First Search—Backtracking (5) Stack start goal Push

16 Depth First Search—Backtracking (6) Stack start goal Push

17 Depth First Search—Backtracking (7) Stack start goal Push Pop

18 Depth First Search—Backtracking (8) Stack start goal Push Pop

19 Depth First Search—Backtracking (9) Stack start goal 2 1 Push Pop

20 Depth First Search—Backtracking (10) Stack start goal 1 Push Pop

21 Depth First Search—Backtracking (11) Stack start goal 3 1 Push

22 Depth First Search—Backtracking (12) Stack start goal Push

23 Depth First Search—Backtracking (13) Stack start goal Push

24 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); }

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

26 Breadth First Search Problem  Find a shortest path from start to goal start goal

27 Breadth First Search (2) Queue start goal 1 push

28 Breadth First Search (3) Queue start goal pop

29 Breadth First Search (4) Queue start goal 234 pop

30 Breadth First Search (5) Queue start goal pop 34

31 Breadth First Search (6) Queue start goal 3456 push

32 Breadth First Search (7) Queue start goal 456 Pop

33 Breadth First Search (8) Queue start goal Push

34 Breadth First Search (9) Queue start goal 5678 Pop

35 Breadth First Search (10) Queue start goal 678 Pop

36 Breadth First Search (11) Queue start goal 78 Pop

37 Breadth First Search (12) Queue start goal 789 Push

38 Breadth First Search (13) Queue start goal 89 Pop

39 Breadth First Search (14) Queue start goal 8910 Push

40 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); }

41 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, )

42 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

43 Evaluating Postfix Expressions (3) Most CPUs have hardware support for this algorithm Translation from infix to postfix also uses a stack (software)

44 Evaluating Postfix Expressions (4) Original expression: 1 + (2 + 3) * Evaluate: * + 5 +

45 Evaluating Postfix Expressions (5) Input: * Push(1) 1

46 Evaluating Postfix Expressions (6) Input: * Push(2) 2 1

47 Evaluating Postfix Expressions (7) Input: * Push(3) 3 2 1

48 Evaluating Postfix Expressions (8) Input: + 4 * Pop() == 3 Pop() == 2 1

49 Evaluating Postfix Expressions (9) Input: + 4 * Push(2 + 3) 5 1

50 Evaluating Postfix Expressions (10) Input: 4 * Push(4) 4 5 1

51 Evaluating Postfix Expressions (11) Input: * Pop() == 4 Pop() == 5 1

52 Evaluating Postfix Expressions (12) Input: * Push(5 * 4) 20 1

53 Evaluating Postfix Expressions (13) Input: Pop() == 20 Pop() == 1

54 Evaluating Postfix Expressions (14) Input: Push(1 + 20) 21

55 Evaluating Postfix Expressions (15) Input: 5 + Push(5) 5 21

56 Evaluating Postfix Expressions (16) Input: + Pop() == 21 Pop() == 5

57 Evaluating Postfix Expressions (17) Input: + Push(21 + 5) 26

58 Evaluating Postfix Expressions (18) Input: Pop() == 26

59 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 }

60 Reading Exercise How to use stack to  Balance brackets  Convert infix to postfix

61 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

62 Recursion Order 1:  function calls itself Order 2:  f() calls g(), and g() calls f() Facilitated by stack

63 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

64 Stack Adaptor Requirements Stack  push()  pop()  top()  empty()  size() Can use List, Deque

65 Queue Adaptor Requirements Queue  push()  pop ()  front()  back()  empty()  size() Can use List, Deque

66 Class Stack template 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(); } };

67 Declarations Stack > floatStack; Stack > intStack;

68 Class Queue template 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(); } };


Download ppt "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() "

Similar presentations


Ads by Google