1 / 62

Stacks and Queues

Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors. Stacks and Queues. Sections 3.6 and 3.7. 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()

arvin
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. Chapter 3 Lists, Stacks, and QueuesAbstract Data Types, Vectors Stacks and Queues Sections 3.6 and 3.7

  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

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

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

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

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

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

  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

  9. 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 Depth First Search—Backtracking 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  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 heap stack static program memory

  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

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

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

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

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

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

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

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

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

  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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  49. 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 of node t) { Q.push(n); if (n == goal) Success(S); } Q.pop(); } Failure(Q); }

  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

More Related