stacks and queues
play

Stacks and Queues 25 Stack In/Out LIFO: Last-in First-out Push - PowerPoint PPT Presentation

Stacks and Queues 25 Stack In/Out LIFO: Last-in First-out Push Pop Undo/Redo Back/Forward Function call/return Stack 26 Queue HPC FIFO: First-in First-out Back Front In Out Jobs Enqueue Dequeue Queue 27 Queue and Stack ADT


  1. Stacks and Queues 25

  2. Stack In/Out LIFO: Last-in First-out Push Pop Undo/Redo Back/Forward Function call/return Stack 26

  3. Queue HPC FIFO: First-in First-out Back Front In Out Jobs Enqueue Dequeue Queue 27

  4. Queue and Stack ADT Queue Stack Enqueue Push Dequeue Pop Front Top Size Size Empty? Empty? 28

  5. Stack/Queue Implementation Queue List Enqueue Push_back Dequeue Pop_back Front Size Push_front Empty Pop_front Stack Front Back Push Pop Size Top Empty Size Empty 29

  6. Queue Implementation Queue Array Impl. Linked List Impl. Enqueue O(1) O(1) Dequeue O(n) O(1) Front O(1) O(1) Memory overhead Small Big Random access O(1) O(n) 30

  7. Circular Array Queue Front Back 31

  8. Circular Array Queue Front Enqueue Back 32

  9. Circular Array Queue Dequeue Front Back 33

  10. Circular Linked List Queue Front Back 34

  11. Queue Implementation Queue Circular Array Circular Linked Impl. List Impl. Enqueue O(1) O(1) Dequeue O(1) O(1) Front O(1) O(1) Memory overhead Small Big 35

  12. Standard Template Library (STL) Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why? For the sake of learning, you are not allowed to use STL during this class unless otherwise mentioned 36

  13. Stack Applications Expression evaluation Human-friendly infix expressions The operator falls between the two operands 3 + 2 × 5 = 13 Easier to read and understand Can be easily broken into pieces Machine-friendly postfix expressions The operator is placed after the two operands 325 ×+= 13 Easier to compute in one pass No need for parentheses 37

  14. Evaluate postfix expressions Infix: 3 × 5 + 4/2 × 2 = 34 Postfix: 35 × 42/+2 × ⨉ ⨉ 3 5 4 2 / + 2 Stack of operands 38

  15. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Stack of operands 39

  16. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 3 Stack of operands 40

  17. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 5 3 Stack of operands 41

  18. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Pop 5 3 Stack of operands 42

  19. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Pop 5 3 Stack of operands 43

  20. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 ⨉ 5 3 Stack of operands 44

  21. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 15 Stack of operands 45

  22. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 4 15 Stack of operands 46

  23. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 2 4 15 Stack of operands 47

  24. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 2 4 15 Stack of operands 48

  25. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Pop 4 2 / Pop Push 2 15 Stack of operands 49

  26. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Pop 15 2 + Pop Push 17 Stack of operands 50

  27. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 2 17 Stack of operands 51

  28. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Pop 17 ⨉ 2 Pop Push 34 Stack of operands 52

  29. Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 34 Stack of operands 53

  30. Infix to Postfix Conversion Convert the input into a sequence of operators and operands Account for operator precedence Account for parentheses Example Infix (input): 3 × 5 + 4/2 × 2 Postfix (desired output): 35 × 42/+2 × 54

  31. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output ( Stack of operators 55

  32. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 3 ( Stack of operators 56

  33. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 3 ⨉ ( Stack of operators 57

  34. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ ( Stack of operators 58

  35. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ + ( Stack of operators 59

  36. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 4 + ( Stack of operators 60

  37. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 4 / + ( Stack of operators 61

  38. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 42 / + ( Stack of operators 62

  39. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 42/+ Stack of operators 63

  40. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 42/+ ⨉ Stack of operators 64

  41. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 42/+2 ⨉ Stack of operators 65

  42. Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 42/+2 ⨉ Stack of operators 66

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend