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

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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
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.
Chapter 7 Stacks Dr. Youssef Harrath
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.
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.
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
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using C++
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 Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
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: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
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.
1 Chapter 17: Stacks and Queues Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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.
Chapter 17: Stacks and Queues Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Data Structures Using C++ 2E
Data Structures Using C++ 2E
Stacks and Queues Chapter 4.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Data Structures Array Based Stacks.
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.
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 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a stack as a linked list Discover stack applications Learn how to use a stack to remove recursion

C++ Programming: Program Design Including Data Structures, Fourth Edition3 Objectives (continued) Learn about queues Examine various queue operations Learn how to implement a queue as an array Learn how to implement a queue as a linked list Discover queue applications

C++ Programming: Program Design Including Data Structures, Fourth Edition4 Stacks Stack: list 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 Edition5 Stacks (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition7 Stack Operations In the abstract class stackADT : − initializeStack − isEmptyStack − isFullStack − push − top − pop

C++ Programming: Program Design Including Data Structures, Fourth Edition9 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 Edition10 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 Edition13 Implementation of Stacks as Arrays (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition14 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 Edition15 Implementation of Stacks as Arrays (continued)

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

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

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

C++ Programming: Program Design Including Data Structures, Fourth Edition19 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 Edition20 Push (continued)

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

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

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

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

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

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

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

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

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

C++ Programming: Program Design Including Data Structures, Fourth Edition37 Linked Implementation of Stacks (This topic continues in file 02225_PPT_ch18-2.ppt )