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++
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.
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.
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
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
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
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
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
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
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
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
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
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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
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.
Stack Applications Evaluating Arithmetic Expressions Example: ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
Stack Applications Evaluating Arithmetic Expressions Assumptions: - The input is a fully parenthesized numeric expression. Example: ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
Stack Applications Evaluating Arithmetic Expressions Assumptions: - The input is a fully parenthesized numeric expression. Examples: ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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 ) + 3 ) + ( 6 - 4 )) (( 6 + ( 9 + 3 )) + ( 6 - 4 ))
Stack Applications Evaluating Arithmetic Expressions Assumptions: - The input is a fully parenthesized numeric expression. - +, -, *, and / are the operators Examples: ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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 ) + 3 ) + ( 6 - 4 )) (( 6 + ( 9 + 3 )) + ( 6 - 4 ))
Stack Applications Evaluating Arithmetic Expressions The order of the evaluation: Examples: ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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 ) + 3 ) + ( 6 - 4 )) (( 6 + ( 9 + 3 )) + ( 6 - 4 ))
Stack Applications Evaluating Arithmetic Expressions The order of the evaluation: Examples: (( 15 / 3 ) * ( 6 - 4 )) 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 ) + ( 6 - 4 )) (( 6 + 12 ) + ( 6 - 4 ))
Stack Applications Evaluating Arithmetic Expressions The order of the evaluation: Examples: (( 15 / 3 ) * ( 6 - 4 )) 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 ) + ( 6 - 4 )) (( 6 + 12 ) + ( 6 - 4 ))
Stack Applications Evaluating arithmetic expressions The order of the evaluation: Examples: ( 5 * ( 6 - 4 )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( 18 + ( 6 - 4 )) ( 18 + ( 6 - 4 ))
Stack Applications Evaluating arithmetic expressions The order of the evaluation: Examples: ( 5 * ( 6 - 4 )) We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. ( 18 + ( 6 - 4 )) ( 18 + ( 6 - 4 ))
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. ( 18 + 2 ) ( 18 + 2 )
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. ( 18 + 2 ) ( 18 + 2 )
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.
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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
Stack Applications Evaluating arithmetic expressions will use two stacks: one for storing operands and the other for storing operators Example : ((( 6 + 9 ) / 3 ) * ( 6 - 4 )) 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
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.
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.
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.
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.
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.
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.
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.
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.
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>
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
More Complex Stack Applications Arithmetic Expressions: 1. Infix notation: - an operator symbol occurs between the operands Example: ( 3 + 4 ) * 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.
More Complex Stack Applications Arithmetic Expressions: 1. Infix notation: - an operator symbol occurs between the operands Example: ( 3 + 4 ) * 7 2. Prefix notation: - an operator symbol occurs before the operands Example: * + 3 4 7 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.
More Complex Stack Applications Arithmetic Expressions: 1. Infix notation: - an operator symbol occurs between the operands Example: ( 3 + 4 ) * 7 2. Prefix notation: - an operator symbol occurs before the operands Example: * + 3 4 7 3. Postfix notation: - an operator symbol occurs after the operands Example: 3 4 + 7 * We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board.
More Complex Stack Applications Evaluating Postfix Expressions: Example: 3 2 + 7 * ( == (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.
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.
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.
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.
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.
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.
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.
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.
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + We'll start with a description of a problem which involves a bunch of queens from a chess game, and a chess board. 5
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Evaluating Postfix Expressions: Example : 5 3 2 * + 4 - 5 + 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
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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.
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( B / C )) – ( 2 * D ) ) A 7 + 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
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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.
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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. (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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. ( (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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. ( ( (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 + (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 + (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 + (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 / (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 / * (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 / * (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 / * (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 (
More Complex Stack Applications Translating Infix to Postfix Notation: Simplifying Assumption: All operators are binary 1. Fully parenthesized Infix expressions: Example: ( ( ( A + 7 ) * ( 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 * –
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.
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.
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.
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.
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.
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.
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.
More Complex Stack Applications Translating Infix to Postfix Notation: 2. More general expressions: - not every operation is parenthesized Example: 3 + 4 * ( 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.
More Complex Stack Applications Translating Infix to Postfix Notation: 2. More general expressions: - not every operation is parenthesized Example: 3 + 4 * ( 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.
More Complex Stack Applications Translating Infix to Postfix Notation: 2. More general expressions: - not every operation is parenthesized Example: 3 + 4 * ( 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.
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:
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
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 +
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 +
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 +
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 + –
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 –
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 –
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:
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
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 +
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 +
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 +
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 +
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 * +
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 * +
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:
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
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 -
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 -
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 -
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 -
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 -
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 + -
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 + -
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 + -
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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:
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
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 *
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 *
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 *
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 *
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 * +
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 +
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 +
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 +
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 +
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 +
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 +
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 – +
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 – +
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 – + –
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 –
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 –