stacks and queues - ucsb-cs24-s18.github.iostacks and queues problem solving with computers-ii....
TRANSCRIPT
![Page 1: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/1.jpg)
STACKS AND QUEUESProblem Solving with Computers-II
![Page 2: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/2.jpg)
Stacks – container class available in the C++ STL• Container class that uses the Last In First Out (LIFO) principle • Methods i. push() ii. pop() iii. top() iv. empty()
2
Demo reversing a string
![Page 3: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/3.jpg)
Notations for evaluating expression• Infix number operator number ( 7 + ( 3 * 5) ) – ( 4 / 2 ) • Prefix operators precede the operands • Postfix operators come after the operands
3
![Page 4: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/4.jpg)
Lab05 – part 1: Evaluate a fully parenthesized infix expression
4
![Page 5: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/5.jpg)
( ( 2 * 2 ) + ( 8 + 4 ) )
5
![Page 6: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/6.jpg)
( ( 2 * 2 ) + ( 8 + 4 ) )
6
What should be the next step after the first right parenthesis is encountered? A. Push the right parenthesis
onto the stack B. If the stack is not empty pop
the next item on the top of the stack
C. Ignore the right parenthesis and continue checking the next character
D.None of the above
![Page 7: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/7.jpg)
Evaluating a fully parenthesized infix expression
8
![Page 8: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/8.jpg)
Evaluating a fully parenthesized infix expression
9
![Page 9: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/9.jpg)
Evaluating a fully parenthesized infix expression
10
![Page 10: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/10.jpg)
( ( 2 * 2 ) + ( 8 + 4 ) )
7
![Page 11: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/11.jpg)
Evaluating a fully parenthesized infix expression
11
![Page 12: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/12.jpg)
Lab 05, part2 : Evaluating post fix expressions using a single stackPostfix: 7 3 5 * + 4 2 / - Infix: ( 7 + ( 3 * 5) ) – ( 4 / 2 )
12
![Page 13: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/13.jpg)
![Page 14: STACKS AND QUEUES - ucsb-cs24-s18.github.ioSTACKS AND QUEUES Problem Solving with Computers-II. Stacks – container class available in the C++ STL](https://reader033.vdocuments.net/reader033/viewer/2022053002/5f0690837e708231d4189dca/html5/thumbnails/14.jpg)
• Like stacks, queues have many applications. • Items enter a queue at the rear and leave a queue at
the front. • Queues can be implemented using an array or using a
linked list.
Summary