Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,

Slides:



Advertisements
Similar presentations
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
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.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
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.
Queue Overview Queue ADT Basic operations of queue
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.
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. 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, Queues, and Deques
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.”
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
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,
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.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda 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.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
 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.
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Queues Rem Collier Room A1.02
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
Pointers and Dynamic Variables
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 19: Stacks and Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
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.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
CSE 373 Data Structures Lecture 6
Queues Jyh-Shing Roger Jang (張智星)
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
Data Structures & Programming
Presentation transcript:

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 18.7 Eliminating Recursion 18-2

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18.1 Introduction to the Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria serving area –return addresses for function calls 18-3

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Stack Basics Stack is usually implemented as a list, with additions and removals taking place at one end of the list The active end of the list implementing the stack is the top of the stack Stack types: –Static – fixed size, often implemented using an array –Dynamic – size varies as needed, often implemented using a linked list 18-4

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Stack Operations and Functions Operations: –push: add a value at the top of the stack –pop: remove a value from the top of the stack Functions: –isEmpty: true if the stack currently contains no elements –isFull: true if the stack is full; only useful for static stacks 18-5

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Static Stack Implementation Uses an array of a fixed size Bottom of stack is at index 0. A variable called top tracks the current top of the stack const int STACK_SIZE = 3; char s[STACK_SIZE]; int top = 0; top is where the next item will be added 18-6

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Implementation Example This stack has max capacity 3, initially top = 0 and stack is empty. K E G K E 18-7 E push('E');push('K');push('G'); top is 1top is 2top is 3

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Stack Operations Example After three pops, top is 0 and the stack is empty E 18-8 K E pop(); (remove G ) pop(); (remove K ) pop(); (remove E )

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Implementation char s[STACK_SIZE]; int top=0; To check if stack is empty: bool isEmpty() { if (top == 0) return true; else return false; } 18-9

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Implementation char s[STACK_SIZE]; int top=0; To check if stack is full: bool isFull() { if (top == STACK_SIZE) return true; else return false; } 18-10

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Implementation To add an item to the stack void push(char x) { if (isFull()) {error(); exit(1);} // or could throw an exception s[top] = x; top++; } 18-11

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Implementation To remove an item from the stack void pop(char &x) { if (isEmpty()) {error(); exit(1);} // or could throw an exception top--; x = s[top]; } 18-12

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Implementation class STACK { private: char *s; int capacity, top; public: void push(char x); char pop(); bool isFull(); bool isEmpty(); STACK(int stackSize); ~STACK() }; 18-13

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Exceptions from Stack Operations Exception classes can be added to the stack object definition to handle cases where an attempt is made to push onto a full stack (overflow) or to pop from an empty stack (underflow) Programs that use push and pop operations should do so from within a try block. catch block(s) should follow the try block, interpret what occurred, and inform the user

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18.2 Dynamic Stacks Implemented as a linked list Can grow and shrink as necessary Can't ever be full as long as memory is available 18-15

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Linked List Implementation Define a class for a dynamic linked list Within the class, define a private member class for dynamic nodes in the list Define a pointer to the beginning of the linked list, which will serve as the top of the stack 18-16

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Linked List Implementation A linked stack after three push operations: push('a'); push('b'); push('c'); NULL top abc

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Operations on a Linked Stack Check if stack is empty: bool isEmpty() { if (top == NULL) return true; else return false; } 18-18

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Operations on a Linked Stack Add a new item to the stack void push(char x) { top = new LNode(x, top); } 18-19

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Operations on a Linked Stack Remove an item from the stack char pop( ) { if (isEmpty()) { // throw error } char x = top->value; LNode *oldTop = top; top = top->next; delete oldTop; return x; } 18-20

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18.3 The STL stack Container Stack template can be implemented as a vector, 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 (must be used with pop to remove and retrieve top element) 18-21

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defining an STL-based Stack Defining a stack of char, named cstack, implemented using a vector : stack > cstack; Implemented using a list: stack > cstack; Implemented using a deque (default): stack cstack; Spaces are required between consecutive > > symbols to distinguish from stream extraction 18-22

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18.4 Introduction to the Queue ADT Queue: a FIFO (first in, first out) data structure. Examples: –people in line at the theatre box office –print requests sent by users to a network printer Implementation: –static: fixed size, implemented as array –dynamic: variable size, implemented as linked list 18-23

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 18-24

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Implementation of Queue An empty queue that can hold char values: enqueue('E'); E frontrear front,rear

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Queue Operations - Example enqueue('K'); enqueue('G'); EK EKG frontrear front rear

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Queue Operations - Example dequeue(); // remove E dequeue(); // remove K KG G frontrear frontrear

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Implementation Issues In the preceding example, Front never moves. Whenever dequeue is called, all remaining queue entries move up one position. This takes time. Alternate approach: –Circular array: front and rear both move when items are added and removed. Both can ‘wrap around’ from the end of the array to the front if warranted. Other conventions are possible 18-28

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array Implementation Issues Variables needed –const int QSIZE = 100; –char q[QSIZE]; –int front = -1; –int rear = -1; –int number = 0; //how many in queue Could make these members of a queue class, and queue operations would be member functions 18-29

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley isEmpty Member Function Check if queue is empty bool isEmpty() { if (number > 0) return false; else return true; } 18-30

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley isFull Member Function Check if queue is full bool isFull() { if (number < QSIZE) return false; else return true; } 18-31

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley enqueue and dequeue To enqueue, we need to add an item x to the rear of the queue Queue convention says q[rear] is already occupied. Execute if(!isFull) { rear = (rear + 1) % QSIZE; // mod operator for wrap-around q[rear] = x; number ++; } 18-32

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley enqueue and dequeue To dequeue, we need to remove an item x from the front of the queue Queue convention says q[front] has already been removed. Execute if(!isEmpty) { front = (front + 1) % QSIZE; x = q[front]; number--; } 18-33

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley enqueue and dequeue enqueue moves rear to the right as it fills positions in the array dequeue moves front to the right as it empties positions in the array When enqueue gets to the end, it wraps around to the beginning to use those positions that have been emptied When dequeue gets to the end, it wraps around to the beginning use those positions that have been filled 18-34

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley enqueue and dequeue Enqueue wraps around by executing rear = (rear + 1) % QSIZE; Dequeue wraps around by executing front = (front + 1) % QSIZE; 18-35

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Exception Handling in Static Queues As presented, the static queue class will encounter an error if an attempt is made to enqueue an element to a full queue, or to dequeue an element from an empty queue A better design is to throw an underflow or an overflow exception and allow the programmer to determine how to proceed Remember to throw exceptions from within a try block, and to follow the try block with a catch block 18-36

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18.5 Dynamic Queues Like a stack, a queue can be implemented using a linked list This allows dynamic sizing and avoids the issue of wrapping indices front rear NULL

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Queue Implementation Data Structures Define a class for the dynamic queue Within the dynamic queue, define a private member class for a dynamic node in the queue Define pointers to the front and rear of the queue 18-38

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley isEmpty Member Function To check if queue is empty: bool isEmpty() { if (front == NULL) return true; else return false; } 18-39

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley enqueue Member Function Details To add item at rear of queue void enqueue (char x) { if (isEmpty()) { front = new QNode(x); rear = front; } else { rear->next = new QNode(x); rear = rear->next; } 18-40

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley dequeue Member Function To remove item from front of queue char dequeue () { if (isEmpty()) { error(); exit(1); } char x = front->value; QNode *oldfront = front; front = front->next; delete oldfront; } 18-41

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18.6 The STL deque and queue Containers deque : a double-ended queue (DEC). Has member functions to enqueue ( push_back ) and dequeue ( pop_front ) queue : container ADT that can be used to provide a queue based on a vector, list, or deque. Has member functions to enqueue ( push ) and dequeue ( pop ) 18-42

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defining a Queue Defining a queue of char, named cQueue, based on a deque : deque cQueue; Defining a queue with the default base container queue cQueue; Defining a queue based on a list : queue > cQueue; Spaces are required between consecutive > > symbols to distinguish from stream extraction 18-43

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18.7 Eliminating Recursion Recursive solutions to problems are often elegant but inefficient A solution that does not use recursion is more efficient for larger sizes of inputs Eliminating the recursion: re-writing a recursive algorithm so that it uses other programming constructs (stacks, loops) rather than recursive calls 18-44

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues