C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.

Slides:



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

Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
DATA STRUCTURE & ALGORITHMS
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.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)
Data Structures & Algorithms
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 18: Stacks and Queues
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Chapter 18: Stacks and Queues
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
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.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 7 Stacks Dr. Youssef Harrath
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Chapter 18: Stacks and Queues
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
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)
Stacks And Queues Chapter 18.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
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.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Data Structures Evolution of algorithms for an array-based stack, queue, and linked-list. Evolved data structures used to evolve solutions to problems.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
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.
Click to edit Master text styles Stacks Data Structure.
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C++ 2E
Queues.
Stacks and Queues Chapter 4.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Pointers and Linked Lists
Stacks Data structure Elements added, removed from one end only
Stacks CS-240 Dick Steflik.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Stack Implementations
Presentation transcript:

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues

C++ Programming: Program Design Including Data Structures, Fourth Edition2 Stacks Stack: group of homogenous elements −Addition and deletion occur only at one end, called the top of the stack Example: in a cafeteria, the second tray can be removed only if first tray has been removed −Last in first out (LIFO) data structure Operations: −Push: to add an element onto the stack −Pop: to remove an element from the stack

C++ Programming: Program Design Including Data Structures, Fourth Edition3 Stacks (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition4 Implementation of Stacks as Arrays First element can go in first array position, the second in the second position, etc. The top of the stack is the index of the last element added to the stack Stack elements are stored in an array Stack element is accessed only through top To keep track of the top position, use a variable called stackTop

C++ Programming: Program Design Including Data Structures, Fourth Edition5 Implementation of Stacks as Arrays (continued) Because stack is homogeneous −You can use an array to implement a stack Can dynamically allocate array −Enables user to specify size of the array The class stackType implements the functions of the abstract class stackADT

C++ Programming: Program Design Including Data Structures, Fourth Edition8 Implementation of Stacks as Arrays (continued) C++ arrays begin with the index 0 −Must distinguish between: The value of stackTop The array position indicated by stackTop If stackTop is 0, the stack is empty If stackTop is nonzero, the stack is not empty −The top element is given by stackTop - 1

C++ Programming: Program Design Including Data Structures, Fourth Edition9 Implementation of Stacks as Arrays (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition10 Initialize Stack

C++ Programming: Program Design Including Data Structures, Fourth Edition11 Empty Stack If stackTop is 0, the stack is empty

C++ Programming: Program Design Including Data Structures, Fourth Edition12 Full Stack The stack is full if stackTop is equal to maxStackSize

C++ Programming: Program Design Including Data Structures, Fourth Edition13 Push Store the newItem in the array component indicated by stackTop Increment stackTop Must avoid an overflow

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

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

C++ Programming: Program Design Including Data Structures, Fourth Edition16 Pop Simply decrement stackTop by 1 Must check for underflow condition

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

C++ Programming: Program Design Including Data Structures, Fourth Edition19 Constructor and Destructor

C++ Programming: Program Design Including Data Structures, Fourth Edition20 Constructor and Destructor (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition21 Copy Constructor

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

C++ Programming: Program Design Including Data Structures, Fourth Edition23 Stack Header File Place definitions of class and functions (stack operations) together in a file

C++ Programming: Program Design Including Data Structures, Fourth Edition25 Programming Example: Highest GPA Input: program reads an input file with each student’s GPA and name 3.5 Bill 3.6 John 2.7 Lisa 3.9 Kathy 3.4 Jason 3.9 David 3.4 Jack Output: the highest GPA and all the names associated with the highest GPA

C++ Programming: Program Design Including Data Structures, Fourth Edition26 Problem Analysis and Algorithm Design Read the first GPA and name of the student −This is the highest GPA so far Read the second GPA and student name −Compare this GPA with highest GPA so far New GPA is greater than highest GPA so far Update highest GPA, initialize stack, add to stack New GPA is equal to the highest GPA so far Add name to stack New GPA is smaller than the highest GPA Discard