Stacks Chapter 7 introduces the stack data type.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

The unorganized person’s data structure
Stacks Chapter 11.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Arithmetic Expressions
Infix, Postfix, Prefix.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Stacks  Introduction  Applications  Implementations  Complex Applications.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
CHP-3 STACKS.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
Stacks Stacks.
Infix to postfix conversion
Objectives In this lesson, you will learn to: Define stacks
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Stack: restricted variant of list
Stacks Chapter 4.
Data Structures – Week #3
Stack application: postponing data usage
Algorithms and Data Structures
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
More About Stacks: Stack Applications
Stacks.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Jordi Cortadella and Jordi Petit Department of Computer Science
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Data Structures – Week #3
More About Stacks: Stack Applications
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
5.3 Implementing a Stack Chapter 5 – The Stack.
LINEAR DATA STRUCTURES
Presentation transcript:

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 –