C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
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)
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Topic 15 Implementing and Using Stacks
Data Structures & Algorithms
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
Infix, Postfix, Prefix.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Topic 15 Implementing and Using Stacks
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 18: Stacks and Queues
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Chapter 18: Stacks and Queues
Data Structures Using C++ 2E
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Chapter 17: Stacks and Queues
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
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.
Chapter 7 Stacks Dr. Youssef Harrath
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
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.
CHAPTER 61 STACK, QUEUES, RECURSION. Introduction when one wants to restrict insertion and deletion so that they can take place only at the beginning.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Chapter 18: Stacks and Queues
CIS 068 Welcome to CIS 068 ! Lesson 11: Data Structures 2.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
Stack Any Other Data Structure Array Linked List
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
CHP-3 STACKS.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
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.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
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.
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 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Chapter 17: Stacks and Queues Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 16: Linked Lists.
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Data Structures Using C++ 2E
Data Structures Array Based Stacks.
STACKS.
Stacks Chapter 4.
Algorithms and Data Structures
PART II STACK APPLICATIONS
Stacks, Queues, and Deques
Stacks Data structure Elements added, removed from one end only
Topic 15 Implementing and Using Stacks
Jordi Cortadella and Jordi Petit Department of Computer Science
Stack.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 Linked Implementation of Stacks Array only allows fixed number of elements If number of elements to be pushed exceeds array size −Program may terminate Linked lists can dynamically organize data In a linked representation, stackTop is pointer to top element in stack

C++ Programming: From Problem Analysis to Program Design, Fourth Edition7 Default Constructor Initializes the stack to an empty state when a stack object is declared −Sets stackTop to NULL

C++ Programming: From Problem Analysis to Program Design, Fourth Edition8 Empty Stack and Full Stack In the linked implementation of stacks, the function isFullStack does not apply −Logically, the stack is never full

C++ Programming: From Problem Analysis to Program Design, Fourth Edition9 Initialize Stack

C++ Programming: From Problem Analysis to Program Design, Fourth Edition10 Push The newElement is added at the beginning of the linked list pointed to by stackTop

C++ Programming: From Problem Analysis to Program Design, Fourth Edition11 Push (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition12 Push (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition13 Push (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition14 Push (continued) We do not need to check whether the stack is full before we push an element onto the stack

C++ Programming: From Problem Analysis to Program Design, Fourth Edition15 Return the Top Element

C++ Programming: From Problem Analysis to Program Design, Fourth Edition16. Pop Node pointed to by stackTop is removed

C++ Programming: From Problem Analysis to Program Design, Fourth Edition18 Pop (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition19 Copy Stack

C++ Programming: From Problem Analysis to Program Design, Fourth Edition20 Copy Stack (continued) Notice that this function is similar to the definition of copyList for linked lists

C++ Programming: From Problem Analysis to Program Design, Fourth Edition21 Constructors and Destructors

C++ Programming: From Problem Analysis to Program Design, Fourth Edition22 Overloading the Assignment Operator (=)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition23 Stack as Derived from the class unorderedLinkedList Our implementation of push is similar to insertFirst (discussed for general lists) −Other functions are similar too: initializeStack and initializeList isEmptyList and isEmptyStack linkedStackType can be derived from linkedListType − class linkedListType is abstract Must implement pop as described earlier

C++ Programming: From Problem Analysis to Program Design, Fourth Edition24 Stack as Derived from the class unorderedLinkedList (cont.) unorderedLinkedListType is derived from linkedListType −Provides the definitions of the abstract functions of the class linkedListType We can derive the linkedStackType from unorderedLinkedListType

C++ Programming: From Problem Analysis to Program Design, Fourth Edition25 Application of Stacks: Postfix Expressions Calculator Infix notation: usual notation for writing arithmetic expressions −The operator is written between the operands −Example: a + b −The operators have precedence Parentheses can be used to override precedence

C++ Programming: From Problem Analysis to Program Design, Fourth Edition26 Application of Stacks: Postfix Expressions Calculator (continued) Prefix (Polish) notation: the operators are written before the operands −Introduced by the Polish mathematician Jan Lukasiewicz Early 1920s −The parentheses can be omitted −Example: + a b

C++ Programming: From Problem Analysis to Program Design, Fourth Edition27 Application of Stacks: Postfix Expressions Calculator (continued) Reverse Polish notation: the operators follow the operands (postfix operators) −Proposed by the Australian philosopher and early computer scientist Charles L. Hamblin Late 1950's −Advantage: the operators appear in the order required for computation −Example: a + b * c In a postfix expression: a b c * +

C++ Programming: From Problem Analysis to Program Design, Fourth Edition28 Application of Stacks: Postfix Expressions Calculator (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition29 Application of Stacks: Postfix Expressions Calculator (continued) Postfix notation has important applications in computer science −Many compilers first translate arithmetic expressions into postfix notation and then translate this expression into machine code Evaluation algorithm: −Scan expression from left to right −When an operator is found, back up to get the operands, perform the operation, and continue

C++ Programming: From Problem Analysis to Program Design, Fourth Edition30 Application of Stacks: Postfix Expressions Calculator (continued) Example: * = −Read first symbol 6 is a number  push it onto the stack −Read next symbol 3 is a number  push it onto the stack

C++ Programming: From Problem Analysis to Program Design, Fourth Edition31 Application of Stacks: Postfix Expressions Calculator (continued) Example: * = −Read next symbol + is an operator (two operands)  pop stack twice, perform operation, put result back onto stack

C++ Programming: From Problem Analysis to Program Design, Fourth Edition32 Example: * = −Read next symbol 2 is a number  push it onto the stack Application of Stacks: Postfix Expressions Calculator (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition33 Example: * = −Read next symbol * is an operator  pop stack twice, perform operation, put result back onto stack Application of Stacks: Postfix Expressions Calculator (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition34 Example: * = −Read next symbol = is an operator  indicates end of expression Print the result (pop stack first) Application of Stacks: Postfix Expressions Calculator (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition35 Application of Stacks: Postfix Expressions Calculator (continued) Symbols can be numbers or anything else: −+, -, * and / are operators Pop stack twice and evaluate expression If stack has less than two elements  error −If symbol is =, the expression ends Pop and print answer from stack If stack has more than one element  error −If symbol is anything else Expression contains an illegal operator

C++ Programming: From Problem Analysis to Program Design, Fourth Edition36 Application of Stacks: Postfix Expressions Calculator (continued) Examples: ; 6 - = ; is an illegal operator * = Does not have enough operands for = Error: stack will have two elements when we encounter equal ( = ) sign

C++ Programming: From Problem Analysis to Program Design, Fourth Edition37 Application of Stacks: Postfix Expressions Calculator (continued) We assume that the postfix expressions are in the following form: #6 #3 + #2 * = −If symbol scanned is #, next input is a number −If the symbol scanned is not #, then it is: An operator (may be illegal) or An equal sign (end of expression) We assume expressions contain only +, -, *, and / operators

C++ Programming: From Problem Analysis to Program Design, Fourth Edition38 Main Algorithm Pseudocode: We will write four functions: − evaluateExpression, evaluateOpr, discardExp, and printResult

C++ Programming: From Problem Analysis to Program Design, Fourth Edition39 Function evaluateExpression

C++ Programming: From Problem Analysis to Program Design, Fourth Edition40 Function evaluateOpr

C++ Programming: From Problem Analysis to Program Design, Fourth Edition41

C++ Programming: From Problem Analysis to Program Design, Fourth Edition42 Function discardExp This function is called whenever an error is discovered in the expression

C++ Programming: From Problem Analysis to Program Design, Fourth Edition43 Function printResult If the postfix expression contains no errors, the function printResult prints the result −Otherwise, it outputs an appropriate message The result of the expression is in the stack and the output is sent to a file

C++ Programming: From Problem Analysis to Program Design, Fourth Edition44

C++ Programming: From Problem Analysis to Program Design, Fourth Edition45 Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward To print the list backward, first we need to get to the last node of the list −Problem: how do we get back to previous node? Links go in only one direction −Solution: save a pointer to each of the nodes with info 5, 10, and 15 Use a stack (LIFO)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition46

C++ Programming: From Problem Analysis to Program Design, Fourth Edition47

C++ Programming: From Problem Analysis to Program Design, Fourth Edition48 Let us now execute the following statements: Output: Removing Recursion (continued) Linked Implementation of Stacks

C++ Programming: From Problem Analysis to Program Design, Fourth Edition49 Queues (This topic continues in file 02096_PPT_ch19-3.ppt )