Download presentation
Presentation is loading. Please wait.
Published bySusanna Wright Modified over 6 years ago
1
Stacks Chapter 7 introduces the stack data type.
Several example applications of stacks are given in that chapter. This lecture demonstrates an application of stacks: implementing backtracking to solve the N-Queens problem. The presentation includes a demonstration program which you can run at a couple points during the presentation. The demonstation requires EGA or VGA graphics on a PC. The best time for this lecture is after the students have read Chapter 7 on stacks. If the students want additional information about the N-queens problem, you can direct them to Programming Project 9 in Chapter 7. Data Structures and Other Objects Using C++
2
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
3
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
4
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Stack’s top
5
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Push 3 Stack’s top 3
6
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Push 6 Stack’s top 6 3
7
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Push 2 Stack’s top 2 6 3
8
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Push 5 5 Stack’s top 2 6 3
9
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Pop Stack’s top 2 6 3
10
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Pop Stack’s top 6 3
11
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Pop Stack’s top 3
12
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Push 4 Stack’s top 4 3
13
Introduction to Stacks and the STL Stack
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top) Last-In First-Out (LIFO) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Stack’s top 4 3
14
Introduction to Stacks and the STL Stack
STL Stack Class Template: - pop( ) - push(const Item& entry) - empty( ) - size( ) - top( ) - etc. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
15
Introduction to Stacks and the STL Stack
Stack Errors: stack overflow: - results from trying to push an item onto a full stack stack underflow: - results from trying to pop an item from an empty stack We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
16
Stack Applications Balanced Parentheses
using a stack, one can write a function that checks expressions to see if the parentheses match We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
17
Stack Applications Balanced Parentheses
using a stack, one can write a function that checks expressions to see if the parentheses match Example: (( X + Y * ( Z + 7 )) * (A + B )) – balanced We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
18
Stack Applications Balanced Parentheses
using a stack, one can write a function that checks expressions to see if the parentheses match Example: (( X + Y * ( Z + 7 )) * (A + B )) – balanced (( X + Y * ( Z + 7 ) * ( A + B )) – not balanced We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
19
Stack Applications Checking Balanced Parentheses
1. Read in the next character c in the expression We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
20
Stack Applications Checking Balanced Parentheses
1. Read in the next character c in the expression 2. If c is not a parenthesis, go to 1 We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
21
Stack Applications Checking Balanced Parentheses
1. Read in the next character c in the expression 2. If c is not a parenthesis, go to 1 3. Else If c is a left parenthesis, push it onto the stack We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
22
Stack Applications Checking Balanced Parentheses
1. Read in the next character c in the expression 2. If c is not a parenthesis, go to 1 3. Else If c is a left parenthesis, push it onto the stack 4. Else If c is a right parenthesis, pop the corresponding left parenthesis from the stack. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
23
Stack Applications Checking Balanced Parentheses
1. Read in the next character c in the expression 2. If c is not a parenthesis, go to 1 3. Else If c is a left parenthesis, push it onto the stack 4. Else If c is a right parenthesis, pop the corresponding left parenthesis from the stack. 5. Repeat the same process until the end of the expression is reached We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
24
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
25
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ‘(‘ pushed (
26
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ‘(‘ pushed (
27
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( nothing happened to the stack (
28
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( nothing happened to the stack (
29
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( nothing happened to the stack (
30
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( nothing happened to the stack (
31
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ( ‘(‘ pushed (
32
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ( nothing happened to the stack (
33
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ( nothing happened to the stack (
34
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ( nothing happened to the stack (
35
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( matching top ‘(’ popped (
36
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. matching top ‘(’ popped (
37
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. nothing happened to the stack (
38
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ‘(‘ pushed (
39
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( nothing happened to the stack (
40
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( nothing happened to the stack (
41
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( nothing happened to the stack (
42
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. matching top ‘(’ popped (
43
Stack Applications Checking Balanced Parentheses
Example: (( X + Y * ( Z + 7 )) * (A + B )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. matching top ‘(’ popped
44
Stack Applications Evaluating Arithmetic Expressions
We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
45
Stack Applications Evaluating Arithmetic Expressions
Example: ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
46
Stack Applications Evaluating Arithmetic Expressions
Assumptions: - The input is a fully parenthesized numeric expression. Example: ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
47
Stack Applications Evaluating Arithmetic Expressions
Assumptions: - The input is a fully parenthesized numeric expression. Examples: ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ((( ) + 3 ) + ( )) (( 6 + ( )) + ( ))
48
Stack Applications Evaluating Arithmetic Expressions
Assumptions: - The input is a fully parenthesized numeric expression. - +, -, *, and / are the operators Examples: ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ((( ) + 3 ) + ( )) (( 6 + ( )) + ( ))
49
Stack Applications Evaluating Arithmetic Expressions
The order of the evaluation: Examples: ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ((( ) + 3 ) + ( )) (( 6 + ( )) + ( ))
50
Stack Applications Evaluating Arithmetic Expressions
The order of the evaluation: Examples: (( 15 / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. (( ) + ( )) (( ) + ( ))
51
Stack Applications Evaluating Arithmetic Expressions
The order of the evaluation: Examples: (( 15 / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. (( ) + ( )) (( ) + ( ))
52
Stack Applications Evaluating arithmetic expressions
The order of the evaluation: Examples: ( 5 * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( 18 + ( )) ( 18 + ( ))
53
Stack Applications Evaluating arithmetic expressions
The order of the evaluation: Examples: ( 5 * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( 18 + ( )) ( 18 + ( ))
54
Stack Applications Evaluating arithmetic expressions
The order of the evaluation: Examples: ( 5 * 2 ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ) ( )
55
Stack Applications Evaluating arithmetic expressions
The order of the evaluation: Examples: ( 5 * 2 ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ) ( )
56
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
57
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Numbers Operators
58
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Numbers Operators
59
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 6 Numbers Operators
60
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 6 + Numbers Operators
61
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 9 6 + Numbers Operators
62
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 6+9 Numbers Operators
63
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 15 / Numbers Operators
64
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 3 15 / Numbers Operators
65
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 15 / 3 Numbers Operators
66
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 5 * Numbers Operators
67
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 6 5 * Numbers Operators
68
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 6 - 5 * Numbers Operators
69
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 4 6 - 5 * Numbers Operators
70
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 6 - 4 5 * Numbers Operators
71
Stack Applications Evaluating arithmetic expressions
will use two stacks: one for storing operands and the other for storing operators Example : ((( ) / 3 ) * ( )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 5 * 2 Numbers Operators
72
Stack Applications Evaluating arithmetic expressions
Scan the input from left to right. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
73
Stack Applications Evaluating arithmetic expressions
Scan the input from left to right. If the next character is a digit, push the number onto the NUMBERS stack. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
74
Stack Applications Evaluating arithmetic expressions
Scan the input from left to right. If the next character is a digit, push the number onto the NUMBERS stack. Else if it is an operator, push the operator onto the OPERATIONS stack. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
75
Stack Applications Evaluating arithmetic expressions
Scan the input from left to right. If the next character is a digit, push the number onto the NUMBERS stack. Else if it is an operator, push the operator onto the OPERATIONS stack. Else if it is a right parenthesis, evaluate the subexpression using the numbers in NUMBERS and the top operator in OPERATIONS. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
76
Stack Applications Evaluating arithmetic expressions
Scan the input from left to right. If the next character is a digit, push the number onto the NUMBERS stack. Else if it is an operator, push the operator onto the OPERATIONS stack. Else if it is a right parenthesis, evaluate the subexpression using the numbers in NUMBERS and the top operator in OPERATIONS. Otherwise, repeat the same process until the end of expression is reached. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
77
Stack Applications Evaluating arithmetic expressions
double read_and_evaluate(istream& ins) { const char DECIMAL = ‘.’ ; const char RIGHT_PARENTHESIS = ‘)’ ; stack<double> numbers ; stack<char> operations ; double number ; char symbol ; … } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
78
Stack Applications Evaluating arithmetic expressions
double read_and_evaluate(istream& ins) { const char DECIMAL = ‘.’ ; const char RIGHT_PARENTHESIS = ‘)’ ; stack<double> numbers ; stack<char> operations ; double number ; char symbol ; … } #include <iostream> #include <stack> We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
79
Stack Applications Evaluating arithmetic expressions
double read_and_evaluate(istream& ins) { … while (ins && ins.peek() != ‘\n’) if (isdigit(ins.peek()) || ins.peek() == DECIMAL)) { … } else if (strchr(“+-*/”, ins.peek()) != NULL) { … } else if (ins.peek() == RIGHT_PARENTHESIS) { …} else { … } } return numbers.top() ; We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
80
Stack Applications Evaluating arithmetic expressions
double read_and_evaluate(istream& ins) { … while (ins && ins.peek() != ‘\n’) if (isdigit(ins.peek()) || ins.peek() == DECIMAL)) { … } else if (strchr(“+-*/”, ins.peek()) != NULL) { … } else if (ins.peek() == RIGHT_PARENTHESIS) { …} else { … } } return numbers.top() ; #include <cctype> We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. #include <cstring>
81
Stack Applications Evaluating arithmetic expressions
double read_and_evaluate(istream& ins) { … while (ins && ins.peek() != ‘\n’) if (isdigit(ins.peek()) || ins.peek() == DECIMAL)) ins >> number ; numbers.push(number} ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
82
Stack Applications Evaluating arithmetic expressions
double read_and_evaluate(istream& ins) { … while (ins && ins.peek() != ‘\n’) else if (strchr(“+-*/”, ins.peek()) != NULL) ins >> symbol ; operations.push(symbol} ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
83
Stack Applications Evaluating arithmetic expressions
double read_and_evaluate(istream& ins) { … while (ins && ins.peek() != ‘\n’) else if (ins.peek()) == RIGHT_PARENTHESIS) ins.ignore( ) ; evaluate_stack (numbers, operations) ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
84
Stack Applications Evaluating arithmetic expressions
double read_and_evaluate(istream& ins) { … while (ins && ins.peek() != ‘\n’) else ins.ignore( ) ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
85
Stack Applications Evaluating arithmetic expressions
void evaluate_stack(stack<double>& numbers, stack<char>& operations) { double operand1, operand2 ; operand2 = numbers.top() ; numbers.pop() ; operand1 = numbers.top() ; switch ( operations.top() ) { … } operations.pop() ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
86
Stack Applications Evaluating arithmetic expressions
void evaluate_stack(stack<double>& numbers, stack<char>& operations) { … // continued switch ( operations.top() ) case ‘+’ : numbers.push(operand1 + operand2) ; break ; case ‘-’ : numbers.push(operand1 - operand2) ; break ; case ‘*’ : numbers.push(operand1 * operand2) ; break ; case ‘/’ : numbers.push(operand1 / operand2) ; break ; } operations.pop() ; We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
87
Implementations of the Stack Class
Array Implementation Stack template class Two member variables data[CAPACITY] array used - number of items We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
88
Implementations of the Stack Class
Array Implementation Stack template class Two member variables data[CAPACITY] array used - number of items template <class Item> class stack { private: Item data[CAPACITY] ; size_type used ; … } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
89
Implementations of the Stack Class
Array Implementation Constructor Push Pop Top Empty Size We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
90
Implementations of the Stack Class
Array Implementation Constructor template<class Item> stack<Item>::stack( ) { used = 0 } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
91
Implementations of the Stack Class
Array Implementation Push template <class Item> void stack<Item>::push(const Item& entry) { assert( size( ) < CAPACITY ) ; data[used] = entry ; ++used ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
92
Implementations of the Stack Class
Array Implementation Pop template <class Item> void stack<Item>::pop( ) { assert( !empty( ) ); - -used ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
93
Implementations of the Stack Class
Array Implementation Top template <class Item> Item stack<Item>::top( ) const { assert( !empty( ) ); return data[used-1] ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
94
Implementations of the Stack Class
Array Implementation Empty template <class Item> bool stack<Item>::empty( ) const { return used == 0 ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
95
Implementations of the Stack Class
Array Implementation Size template <class Item> size_type stack<Item>::size( ) const { return used; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
96
Implementations of the Stack Class
Linked List Implementation We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
97
Implementations of the Stack Class
Linked List Implementation Using a linked list implementation, a stack’s capacity can grow or shrink during execution. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
98
Implementations of the Stack Class
Linked List Implementation Using a linked list implementation, a stack’s capacity can grow or shrink during execution. The head of a list can be the top of a stack. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
99
Implementations of the Stack Class
Linked List Implementation Using a linked list implementation, a stack’s capacity can grow or shrink during execution. The head of a list can be the top of a stack. Entries can be inserted(pushed) into the head of the list. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
100
Implementations of the Stack Class
Linked List Implementation Using a linked list implementation, a stack’s capacity can grow or shrink during execution. The head of a list can be the top of a stack. Entries can be inserted(pushed) into the head of the list. Entries can be removed(popped) from the head of the list. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
101
Implementations of the Stack Class
Linked List Implementation: - Class Definition template <class Item> class stack { private: node<Item> *top_ptr ; … } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
102
Implementations of the Stack Class
Linked List Implementation: - Copy Constructor template <class Item> stack<Item>::stack(const stack<Item>& source) { node<Item>* tail_ptr ; list_copy(source.top_ptr, top_ptr, tail_ptr ) ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
103
Implementations of the Stack Class
Linked List Implementation: - Push template <class Item> void stack<Item>::push(const Item& entry) { list_head_insert(top_ptr, entry ) ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
104
Implementations of the Stack Class
Linked List Implementation: - Pop template <class Item> void stack<Item>::pop( ) { assert ( !empty() ) ; list_head_remove(top_ptr) ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
105
Implementations of the Stack Class
Linked List Implementation: - Empty template <class Item> bool stack<Item>::empty( ) const { return top_ptr == NULL ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
106
Implementations of the Stack Class
Linked List Implementation: - Top template <class Item> Item stack<Item>::top( ) const { assert ( !empty() ) ; return top_ptr->data() ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
107
Implementations of the Stack Class
Linked List Implementation: - Top template <class Item> Item stack<Item>::top( ) const { assert ( !empty() ) ; return top_ptr->data() ; } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
108
More Complex Stack Applications
Arithmetic Expressions: 1. Infix notation: 2. Prefix notation: 3. Postfix notation: We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
109
More Complex Stack Applications
Arithmetic Expressions: 1. Infix notation: - an operator symbol occurs between the operands Example: ( ) * 7 2. Prefix notation: 3. Postfix notation: We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
110
More Complex Stack Applications
Arithmetic Expressions: 1. Infix notation: - an operator symbol occurs between the operands Example: ( ) * 7 2. Prefix notation: - an operator symbol occurs before the operands Example: * 3. Postfix notation: We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
111
More Complex Stack Applications
Arithmetic Expressions: 1. Infix notation: - an operator symbol occurs between the operands Example: ( ) * 7 2. Prefix notation: - an operator symbol occurs before the operands Example: * 3. Postfix notation: - an operator symbol occurs after the operands Example: * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
112
More Complex Stack Applications
Evaluating Postfix Expressions: Example: * ( == (3 + 2) * 7 ) 1. Do the + operation with 3 and 2. 2. Save the result (5) somewhere. 3. Do the * operation with the saved result(5) and 7. We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
113
More Complex Stack Applications
Evaluating Postfix Expressions: Initialize a stack of double numbers. While there is more input to read We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
114
More Complex Stack Applications
Evaluating Postfix Expressions: Initialize a stack of double numbers. While there is more input to read if (the next input is a number) else if (the next input is an operator) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
115
More Complex Stack Applications
Evaluating Postfix Expressions: Initialize a stack of double numbers. While there is more input to read if (the next input is a number) read the input and push it onto the stack else if (the next input is an operator) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
116
More Complex Stack Applications
Evaluating Postfix Expressions: Initialize a stack of double numbers. While there is more input to read if (the next input is a number) read the input and push it onto the stack else if (the next input is an operator) read the operator We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
117
More Complex Stack Applications
Evaluating Postfix Expressions: Initialize a stack of double numbers. While there is more input to read if (the next input is a number) read the input and push it onto the stack else if (the next input is an operator) read the operator use top and pop to get the two numbers off the stack We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
118
More Complex Stack Applications
Evaluating Postfix Expressions: Initialize a stack of double numbers. While there is more input to read if (the next input is a number) read the input and push it onto the stack else if (the next input is an operator) read the operator use top and pop to get the two numbers off the stack compute the result with the operator and the numbers We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
119
More Complex Stack Applications
Evaluating Postfix Expressions: Initialize a stack of double numbers. While there is more input to read if (the next input is a number) read the input and push it onto the stack else if (the next input is an operator) read the operator use top and pop to get the two numbers off the stack compute the result with the operator and the numbers push the result onto the stack We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
120
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 5
121
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 3 5 5
122
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 2 3 5 5
123
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 2 3 5 5
124
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 3*2 5
125
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 6 5
126
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 5 + 6
127
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 4 11
128
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 4 11
129
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 11- 4
130
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 5 7
131
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 5 7
132
More Complex Stack Applications
Evaluating Postfix Expressions: Example : * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 7 + 5
133
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C ) ) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
134
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 1
135
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 2
136
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 3
137
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 4
138
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 5
139
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) A B C / * 2 D * – We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 1 2 3 4 5
140
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
141
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. (
142
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( (
143
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ( (
144
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ( Output: A (
145
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) + We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ( Output: A (
146
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) + We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( ( Output: A 7 (
147
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( Output: A 7 + (
148
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. * ( Output: A 7 + (
149
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) ( We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. * ( Output: A 7 + (
150
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) ( We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. * ( Output: A 7 + B (
151
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) / ( We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. * ( Output: A 7 + B (
152
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) / ( We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. * ( Output: A 7 + B C (
153
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. * ( Output: A 7 + B C / (
154
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: A 7 + B C / * (
155
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. – Output: A 7 + B C / * (
156
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( – Output: A 7 + B C / * (
157
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( – Output: A 7 + B C / * 2 (
158
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( – Output: A 7 + B C / * 2 (
159
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( – Output: A 7 + B C / * 2 D (
160
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. – Output: A 7 + B C / * 2 D (
161
More Complex Stack Applications
Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A ) * ( B / C )) – ( 2 * D ) ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: A 7 + B C / * 2 D * –
162
More Complex Stack Applications
Translating Infix to Postfix Notation: Fully parenthesized infix expressions: while there is more input to read if the next input is a left parenthesis or an operator else if the next input is a number or a variable else if the next input is a right parenthesis We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
163
More Complex Stack Applications
Translating Infix to Postfix Notation: Fully parenthesized infix expressions: while there is more input to read if the next input is a left parenthesis or an operator push it onto the stack else if the next input is a number or a variable else if the next input is a right parenthesis We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
164
More Complex Stack Applications
Translating Infix to Postfix Notation: Fully parenthesized infix expressions: while there is more input to read if the next input is a left parenthesis or an operator push it onto the stack else if the next input is a number or a variable write it to the output else if the next input is a right parenthesis We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
165
More Complex Stack Applications
Translating Infix to Postfix Notation: Fully parenthesized infix expressions: while there is more input to read if the next input is a left parenthesis or an operator push it onto the stack else if the next input is a number or a variable write it to the output else if the next input is a right parenthesis write the top item in the stack and pop it off the satck We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
166
More Complex Stack Applications
Translating Infix to Postfix Notation: Fully parenthesized infix expressions: while there is more input to read if the next input is a left parenthesis or an operator push it onto the stack else if the next input is a number or a variable write it to the output else if the next input is a right parenthesis write the top item in the stack and pop it off the satck pop the matching left parenthesis off the stack We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
167
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
168
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: - not every operation is parenthesized We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
169
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: - not every operation is parenthesized Example: * ( 5 – 2 ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
170
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: - not every operation is parenthesized Example: * ( 5 – 2 ) - still binary operators only We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
171
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: - not every operation is parenthesized Example: * ( 5 – 2 ) - still binary operators only must use precedence rules for operators Example: * and / have higher precedence than + and – We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
172
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 1: 3 + A – B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output:
173
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 1: 3 + A – B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3
174
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 1: 3 + A – B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 +
175
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 1: 3 + A – B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 A +
176
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 1: 3 + A – B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 A +
177
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 1: 3 + A – B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 A + –
178
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 1: 3 + A – B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 A + B –
179
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 1: 3 + A – B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 A + B –
180
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 2: 3 + A * B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output:
181
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 2: 3 + A * B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3
182
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 2: 3 + A * B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 +
183
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 2: 3 + A * B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 A +
184
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 2: 3 + A * B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. * Output: 3 A +
185
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 2: 3 + A * B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. * Output: 3 A B +
186
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 2: 3 + A * B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 A B * +
187
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 2: 3 + A * B We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 A B * +
188
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 3: 3 - ( A + B ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output:
189
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 3: 3 - ( A + B ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3
190
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 3: 3 - ( A + B ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 -
191
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 3: 3 - ( A + B ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( Output: 3 -
192
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 3: 3 - ( A + B ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( Output: 3 A -
193
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 3: 3 - ( A + B ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. + ( Output: 3 A -
194
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 3: 3 - ( A + B ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. + ( Output: 3 A B -
195
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 3: 3 - ( A + B ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( Output: 3 A B + -
196
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 3: 3 - ( A + B ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 A B + -
197
More Complex Stack Applications
Translating Infix to Postfix Notation: 2. More general expressions: Example 3: 3 - ( A + B ) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 A B + -
198
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read if the next input is a left parenthesis { … } else if the next input is a number or a variable { … } else if the next input is an operator { … } else if the next input is a right parenthesis { … } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
199
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read if the next input is a left parenthesis We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
200
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read if the next input is a left parenthesis { push it onto the stack } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
201
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read if the next input is a left parenthesis { push it onto the stack } else if the next input is a number or a variable We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
202
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read if the next input is a left parenthesis { push it onto the stack } else if the next input is a number or a variable { write it to the output } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
203
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read … else if the next input is an operator We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
204
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read … else if the next input is an operator { do print the top operator and pop it while none of these three conditions are true We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
205
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read … else if the next input is an operator { do print the top operator and pop it while none of these three conditions are true … (1) the stack becomes empty (2) the top item on the stack is a left parenthesis (3) the top item on the stack is an operator with lower precedence than the input operator We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Push the input operator onto the stack
206
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read … else if the next input is a right parenthesis We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
207
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read … else if the next input is a right parenthesis { write the top item(operator) and pop it off the stack We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
208
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read … else if the next input is a right parenthesis { write the top item(operator) and pop it off the stack keep printing and popping until a left parenthesis is the top item on the stack and then pop the ‘(‘ } We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
209
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: While there is more input to read … else if the next input is a right parenthesis { write the top item(operator) and pop it off the stack keep printing and popping until a left parenthesis is the top item on the stack and then pop the ‘(‘ } 2. Print and pop any remaining operators on the stack We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
210
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output:
211
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3
212
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 Output: 3 *
213
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 Output: 3 X *
214
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 Output: 3 X *
215
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z * popped because it has higher precedence than + We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 Output: 3 X *
216
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 Output: 3 X * +
217
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( Output: 3 X * Output: 3 +
218
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( Output: 3 X * Y Output: 3 +
219
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( Output: 3 X * Y Output: 3 +
220
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. – ( Output: 3 X * Y Output: 3 +
221
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. – ( Output: 3 X * Y 12 Output: 3 +
222
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( Output: 3 X * Y 12 – Output: 3 +
223
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 Output: 3 X * Y 12 – +
224
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z + popped because it has the same precedence as - We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 Output: 3 X * Y 12 – +
225
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 Output: 3 X * Y 12 – + –
226
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 Output: 3 X * Y 12 – + Z –
227
More Complex Stack Applications
Translating Infix to Postfix Notation: More general expressions: Example: 3 * X + ( Y – 12 ) – Z We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. Output: 3 Output: 3 X * Y 12 – + Z –
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.