C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: 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)
Data Structures and Algorithms (60-254)
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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
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
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
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.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
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.
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.
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.
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.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: 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.
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 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.
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.
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
Stacks Data structure Elements added, removed from one end only
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: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)

C++ Programming: Program Design Including Data Structures, Fourth Edition 2 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: Program Design Including Data Structures, Fourth Edition 7 Default Constructor Initializes the stack to an empty state when a stack object is declared −Sets stackTop to NULL

C++ Programming: Program Design Including Data Structures, Fourth Edition 8 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: Program Design Including Data Structures, Fourth Edition 9 Initialize Stack

C++ Programming: Program Design Including Data Structures, Fourth Edition 10 Push The newElement is added at the beginning of the linked list pointed to by stackTop

C++ Programming: Program Design Including Data Structures, Fourth Edition 11 Push (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition 12 Push (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition 13 Push (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition 14 Push (continued) We do not need to check whether the stack is full before we push an element onto the stack

C++ Programming: Program Design Including Data Structures, Fourth Edition 15 Return the Top Element

C++ Programming: Program Design Including Data Structures, Fourth Edition 16. Pop Node pointed to by stackTop is removed

C++ Programming: Program Design Including Data Structures, Fourth Edition 18 Pop (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition 19 Copy Stack

C++ Programming: Program Design Including Data Structures, Fourth Edition 20 Copy Stack (continued) Notice that this function is similar to the definition of copyList for linked lists

C++ Programming: Program Design Including Data Structures, Fourth Edition 21 Constructors and Destructors

C++ Programming: Program Design Including Data Structures, Fourth Edition 22 Overloading the Assignment Operator (=)

C++ Programming: Program Design Including Data Structures, Fourth Edition 23 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: Program Design Including Data Structures, Fourth Edition 24 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: Program Design Including Data Structures, Fourth Edition 25 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: Program Design Including Data Structures, Fourth Edition 26 Application of Stacks: Postfix Expressions Calculator (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition 27 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: Program Design Including Data Structures, Fourth Edition 28 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: Program Design Including Data Structures, Fourth Edition 29 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: Program Design Including Data Structures, Fourth Edition 30 Example: * = −Read next symbol 2 is a number  push it onto the stack Application of Stacks: Postfix Expressions Calculator (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition 31 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: Program Design Including Data Structures, Fourth Edition 32 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: Program Design Including Data Structures, Fourth Edition 33 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: Program Design Including Data Structures, Fourth Edition 34 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: Program Design Including Data Structures, Fourth Edition 35 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: Program Design Including Data Structures, Fourth Edition 36 Main Algorithm Pseudocode: We will write four functions: − evaluateExpression, evaluateOpr, discardExp, and printResult

C++ Programming: Program Design Including Data Structures, Fourth Edition 37 Function evaluateExpression

C++ Programming: Program Design Including Data Structures, Fourth Edition 38 Function evaluateOpr

39

C++ Programming: Program Design Including Data Structures, Fourth Edition 40 Function discardExp This function is called whenever an error is discovered in the expression

C++ Programming: Program Design Including Data Structures, Fourth Edition 41 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

42

C++ Programming: Program Design Including Data Structures, Fourth Edition 43 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)

44

45

C++ Programming: Program Design Including Data Structures, Fourth Edition 46 Let us now execute the following statements: Output: Removing Recursion (continued)