Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

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.
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.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
What is a Queue? A queue is a FIFO “first in, first out” structure.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Starting Out with C++, 3 rd Edition Chapter 18 Stacks and Queues.
Starting Out with C++, 3 rd Edition Chapter 18 – Stacks and Queues.
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.
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 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.
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.”
CS 204 Stacks and Queues (not in any book that we are using) From Starting out with C++, 7th edition, by Gaddis, Walters and Muganda (chapter 18, pp
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition 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,
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
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.
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
STACKS & QUEUES for CLASS XII ( C++).
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Chapter 12: Data Structures
Queue.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Queue, Deque, and Priority Queue Implementations
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 19: Stacks and Queues.
C++ Plus Data Structures
Chapter 18: Linked Lists.
(not in any book that we are using)
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
CSC 143 Queues [Chapter 7].
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.
Introduction to Programming
Stacks and Queues.
Queues Jyh-Shing Roger Jang (張智星)
C++ Plus Data Structures
Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures
Data Structures & Programming
Presentation transcript:

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved Introduction to the Stack ADT

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 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

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. A LIFO Structure

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 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

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Stack Operations - Example A stack that can hold char values: K E G K E E push('E');push('K');push('G');

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Stack Operations - Example A stack that can hold char values: E K E pop(); (remove G ) pop(); (remove K ) pop(); (remove E )

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. (See IntStack.cpp for the implementation.)

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved Dynamic Stacks

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Dynamic Stacks Grow and shrink as necessary Can't ever be full as long as memory is available Implemented as a linked list

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Implementing a Stack Programmers can program their own routines to implement stack functions See DynIntStack class in the book for an example. Can also use the implementation of stack available in the STL

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved The STL stack Container

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 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

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 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; When using a compiler that is older than C++ 11, be sure to put spaces between the angled brackets that appear next to each other. stack > cstack;

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved Introduction to the Queue ADT

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 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

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 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

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Queue Operations - Example A currently empty queue that can hold char values: enqueue('E'); E front rear

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Queue Operations - Example enqueue('K'); enqueue('G'); EK EKG front rear front rear

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Queue Operations - Example dequeue(); // remove E dequeue(); // remove K KG G front rear front rear

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 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)

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Contents of IntQueue.h 1 // Specification file for the IntQueue class 2 #ifndef INTQUEUE_H 3 #define INTQUEUE_H 4 5 class IntQueue 6 { 7 private: 8 int *queueArray; // Points to the queue array 9 int queueSize; // The queue size 10 int front; // Subscript of the queue front 11 int rear; // Subscript of the queue rear 12 int numItems; // Number of items in the queue

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 13 public: 14 // Constructor 15 IntQueue(int); // Copy constructor 18 IntQueue(const IntQueue &); // Destructor 21 ~IntQueue(); // Queue operations 24 void enqueue(int); 25 void dequeue(int &); 26 bool isEmpty() const; 27 bool isFull() const; 28 void clear(); 29 }; 30 #endif (See IntQueue.cpp for the implementation) Contents of IntQueue.h (Continued)

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved Dynamic Queues

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 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

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Implementing a Queue Programmers can program their own routines to implement queue operations See the DynIntQue class in the book for an example of a dynamic queue Can also use the implementation of queue and dequeue available in the STL

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved The STL deque and queue Containers

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 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 )

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. 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;