Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Chapter 9: Data Structures I
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 12: Data Structures
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 16 Stack and Queues part2
Stack and Queue.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Chapter 12: Data Structures
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 19: Stacks and Queues.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Stacks and Queues CSE 373 Data Structures.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Data Structures & Programming
Presentation transcript:

Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues

Chapter 18 slide 2 Topics 18.1 Introduction to the Stack ADT 18.2 Dynamic Stacks 18.3 The STL stack container 18.4 Introduction to the Queue ADT 18.5 Dynamic Queues 18.6 The STL deque and queue Containers

Chapter 18 slide Introduction to the Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for function calls Implementation: –static: fixed size, implemented as array –dynamic: variable size, implemented as linked list

Chapter 18 slide 4 Stack Operations and Functions Operations: –push: add a value onto the top of the stack –pop: remove a value from the top of the stack Functions: –isFull : true if the stack is currently full, i.e., has no more space to hold additional elements –isEmpty: true if the stack currently contains no elements

Chapter 18 slide 5 Stack Operations - Example A stack that can hold char values: K E G K EE push('E');push('K');push('G');

Chapter 18 slide 6 Stack Operations - Example A stack that can hold char values: E K E pop(); (remove G ) pop(); (remove K ) pop(); (remove E )

Chapter 18 slide Dynamic Stacks Grow and shrink as necessary Can't ever be full as long as memory is available Implemented as a linked list

Chapter 18 slide 8 Implementing a Stack Programmers can program their own routines to implement stack functions Can also use the implementation of stack available in the STL

Chapter 18 slide The STL stack container Stack template can be implemented as a vector, a linked list, or a deque Implements push, pop, and empty member functions Implements other member functions: –size : number of elements on the stack –top : reference to element on top of the stack

Chapter 18 slide 10 Defining a Stack Defining a stack of char s, named cstack, implemented using a vector : stack > cstack; implemented using a list: stack > cstack; implemented using a deque : stack cstack; Spaces are required between consecutive >>, << symbols

Chapter 18 slide Introduction to the Queue ADT Queue: a FIFO (first in, first out) data structure. Examples: –people in line at the theatre box office –print jobs sent to a printer Implementation: –static: fixed size, implemented as array –dynamic: variable size, implemented as linked list

Chapter 18 slide 12 Queue Locations and Operations rear: position where elements are added front: position from which elements are removed enqueue: add an element to the rear of the queue dequeue: remove an element from the front of a queue

Chapter 18 slide 13 Queue Operations - Example A currently empty queue that can hold char values: enqueue('E'); E front rear

Chapter 18 slide 14 Queue Operations - Example enqueue('K'); enqueue('G'); EK EKG front rear front rear

Chapter 18 slide 15 Queue Operations - Example dequeue(); // remove E dequeue(); // remove K KG G front rear front rear

Chapter 18 slide 16 dequeue Issue, Solutions When removing an element from a queue, remaining elements must shift to front Solutions: –Let front index move as elements are removed (works as long as rear index is not at end of array) –Use above solution, and also let rear index "wrap around" to front of array, treating array as circular instead of linear (more complex enqueue, dequeue code)

Chapter 18 slide Dynamic Queues Like a stack, a queue can be implemented using a linked list Allows dynamic sizing, avoids issue of shifting elements or wrapping indices front rear NULL

Chapter 18 slide 18 Implementing a Queue Programmers can program their own routines to implement queue operations Can also use the implementation of queue and dequeue available in the STL

Chapter 18 slide The STL deque and queue Containers deque : a double-ended queue. Has member functions to enqueue ( push_back ) and dequeue ( pop_front ) queue : container ADT that can be used to provide queue as a vector, list, or deque. Has member functions to enque ( push ) and dequeue ( pop )

Chapter 18 slide 20 Defining a Queue Defining a queue of char s, named cQueue, implemented using a deque: deque cQueue; implemented using a queue: queue cQueue; implemented using a list : queue > cQueue; Spaces are required between consecutive >>, << symbols

Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues