Chapter 19: Stacks and Queues.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

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.
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.
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 © 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.
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)
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
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,
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++).
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Queues.
Chapter 12: Data Structures
Queue.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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 (張智星)
Stacks CS-240 Dick Steflik.
C++ Plus Data Structures
Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures
Data Structures & Programming
Presentation transcript:

Chapter 19: Stacks and Queues

Introduction to the Stack ADT 19.1 Introduction to the Stack ADT

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

A LIFO Structure

Stack Operations and Functions 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

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

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

(See IntStack.cpp for the implementation.)

19.2 Dynamic Stacks

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

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

The STL stack Container 19.3 The STL stack Container

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

Defining a stack Defining a stack of chars, named cstack, implemented using a vector: stack< char, vector<char>> cstack; implemented using a list: stack< char, list<char>> cstack; implemented using a deque: stack< char > 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< char, vector<char> > cstack;

Introduction to the Queue ADT 19.4 Introduction to the Queue ADT

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

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

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

Queue Operations - Example enqueue('K'); enqueue('G'); front E K rear front E K G rear

Queue Operations - Example dequeue(); // remove E dequeue(); // remove K front K G rear front G rear

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)

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

Contents of IntQueue.h (Continued) 13 public: 14 // Constructor 15 IntQueue(int); 16 17 // Copy constructor 18 IntQueue(const IntQueue &); 19 20 // Destructor 21 ~IntQueue(); 22 23 // 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)

19.5 Dynamic Queues

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

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

The STL deque and queue Containers 19.6 The STL deque and queue Containers

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)

Defining a queue Defining a queue of chars, named cQueue, implemented using a deque: deque<char> cQueue; implemented using a queue: queue<char> cQueue; implemented using a list: queue<char, list<char>> cQueue;