1 / 85

Stacks and Queues

Stacks and Queues. Andy Wang Data Structures, Algorithms, and Generic Programming. Abstract Data Type. A collection of data A set of operations on the data or subsets of the data A set of axioms , or rules of behavior governing the interaction of operators

akamu
Download Presentation

Stacks and Queues

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks and Queues Andy Wang Data Structures, Algorithms, and Generic Programming

  2. Abstract Data Type • A collection of data • A set of operations on the data or subsets of the data • A set of axioms, or rules of behavior governing the interaction of operators • Examples: stack, queue, list, vector, deque, priority queue, table (map), associative array, set, graph, digraph

  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

  4. Stack ADT (2) • Axioms (for any stack S) • S.size, S.empty(), and S.push(t) are always defined • S.pop() and S.top() are defined iff S.empty() is false • S.empty(), S.size(), S.top() do not change S • S.empty() is true iff S.size() == 0 • S.push(t) followed by S.pop() leaves S unchanged

  5. Stack ADT (3) • Axioms (for any stack S) • After S.push(t), S.top() returns t • S.push(t) increases S.size() by 1 • S.pop() decreases S.size() by 1

  6. Stack Model—LIFO • Empty stack S • S.empty() is true • S.top() not defined • S.size() == 0 food chain stack

  7. Stack Model—LIFO • S.push(“mosquito”) • S.empty() is false • S.top() == “mosquito” • S.size() == 1 food chain stack

  8. Stack Model—LIFO • S.push(“fish”) • S.empty() is false • S.top() == “fish” • S.size() == 2 food chain stack

  9. Stack Model—LIFO • S.push(“raccoon”) • S.empty() is false • S.top() == “raccoon” • S.size() == 3 food chain stack

  10. Stack Model—LIFO • S.pop() • S.empty() is false • S.top() == “fish” • S.size() == 2 food chain stack

  11. Derivable Behaviors (Theorems) • If (S.size() == n) is followed by k push operations, then S.size() == n + k • If (S.size() == n) is followed by k pop operations, then S.size == n – k (k <= n) • The last element of S pushed onto S is the top of S • S.pop() removes the last element of S pushed onto S

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

  13. Queue ADT • Collection • Elements of some proper type T • Operations • void push(T t) • void pop() • T front() • bool empty() • unsigned int size() • Constructors and destructors

  14. Queue ADT • Axioms (for any Queue Q) • Q.size(), Q.empty(), Q.push(t) are always defined • Q.pop() and Q.front() are defined iff Q.empty() is false • Q.empty(), Q.size(), Q.front() do not change Q • Q.empty() is true iff Q.size() == 0 • Suppose Q.size() == n, and the next element pushed onto Q is t; then, after n elements have been popped from Q, t = Q.front()

  15. Queue ADT • Axioms (for any Queue Q) • Q.push(t) increases Q.size() by 1 • Q.pop() decreases Q.size() by 1 • If t = Q.front() then Q.pop() removes t from Q

  16. Queue Model—FIFO • Empty Q animal parade queue

  17. front back Queue Model—FIFO • Q.Push(“ant”) animal parade queue

  18. front back Queue Model—FIFO • Q.Push(“bee”) animal parade queue

  19. front back Queue Model—FIFO • Q.Push(“cat”) animal parade queue

  20. front back Queue Model—FIFO • Q.Push(“dog”) animal parade queue

  21. front back Queue Model—FIFO • Q.Pop() animal parade queue

  22. front back Queue Model—FIFO • Q.Pop() animal parade queue

  23. front back Queue Model—FIFO • Q.Push(“eel”) • Q.Pop() • Q.Pop() animal parade queue

  24. Derivable Behaviors (Theorems) • If (Q.size() == n) is followed by k push operations, then Q.size() == n + k • If (Q.size() == n) is followed by k pop operations, then Q.size() == n – k (k <= n) • The first element pushed onto Q is the the front of Q • Q.pop() removes the front element of Q

  25. Uses of ADT Queue • Buffers • Breadth first search • Simulations

  26. 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 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  27. Depth First Search—Backtracking (2) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 goal Push

  28. Depth First Search—Backtracking (3) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Push goal Push

  29. Depth First Search—Backtracking (4) • Stack 1 start 2 3 4 5 6 7 8 Push 9 10 11 12 Push goal Push

  30. Depth First Search—Backtracking (5) • Stack 1 start 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push

  31. 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

  32. 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

  33. Depth First Search—Backtracking (8) • Stack 1 start 2 3 4 Pop 5 6 7 8 Push 9 10 11 12 Push goal Push

  34. Depth First Search—Backtracking (9) • Stack 1 start 2 3 4 5 6 7 8 Pop 9 10 11 12 Push goal Push

  35. Depth First Search—Backtracking (10) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Pop goal Push

  36. Depth First Search—Backtracking (11) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Push goal Push

  37. Depth First Search—Backtracking (12) • Stack 1 start 2 3 4 5 6 7 8 Push 9 10 11 12 Push goal Push

  38. Depth First Search—Backtracking (13) • Stack 1 start 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push

  39. 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 // mark n visited; S.push(n); } else { BackTrack(S); } } Failure(S); }

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

  41. Breadth First Search • Problem • Find a shortest path from start to goal 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  42. Breadth First Search (2) • Queue Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  43. Breadth First Search (3) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  44. Breadth First Search (4) • Queue Push Push Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  45. Breadth First Search (5) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  46. Breadth First Search (6) • Queue Push Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  47. Breadth First Search (7) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  48. Breadth First Search (8) • Queue Push Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  49. Breadth First Search (9) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  50. Breadth First Search (10) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

More Related