stacks and queues - ucsb-cs24-s18.github.iostacks and queues problem solving with computers-ii....

14
STACKS AND QUEUES Problem Solving with Computers-II

Upload: others

Post on 13-Jun-2020

8 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

• 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