C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Fundamentals of Python: From First Programs Through Data Structures
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Jerry Lebowitz.  Stacks  Queues 3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition.
DATA STRUCTURE & ALGORITHMS
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Data Structures & Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Summary of lectures (1 to 11)
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 18: Stacks and Queues
Chapter 18: Stacks and Queues
Data Structures Using C++ 2E
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 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
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 Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
Chapter 18: Stacks and Queues
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Data Structures Using C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
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.
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
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.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Introduction Of Queue. Introduction A queue is a non-primitive linear data structure. It is an homogeneous collection of elements in which new elements.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 17: Stacks and Queues Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 16: Linked Lists.
Data Structures Using C++ 2E
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
C++ Programming:. Program Design Including
Data Structures Using C++ 2E
Data Structures Using C, 2e
Queues Chapter 4.
QUEUES.
CC 215 Data Structures Queue ADT
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Queues Chapter 4.
CMSC 341 Lecture 5 Stacks, Queues
DATA STRUCTURE QUEUE.
CSC 248 – fundamentals of Data structure
CSCS-200 Data Structure and Algorithms
Data Structures Using C++ 2E
Presentation transcript:

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)

C++ Programming: Program Design Including Data Structures, Fourth Edition 2 Queues Queue: list of homogeneous elements Elements are: −Added at one end (the back or rear) −Deleted from the other end (the front) First In First Out (FIFO) data structure −Middle elements are inaccessible Example: −Waiting line in a bank

C++ Programming: Program Design Including Data Structures, Fourth Edition 3 Queue Operations Some of the queue operations are: − initializeQueue − isEmptyQueue − isFullQueue − front − back − addQueue − deleteQueue Abstract class queueADT defines these operations

C++ Programming: Program Design Including Data Structures, Fourth Edition 6 Implementation of Queues as Arrays You need at least four (member) variables: −An array to store the queue elements − queueFront and queueRear To keep track of first and last elements − maxQueueSize To specify the maximum size of the queue

C++ Programming: Program Design Including Data Structures, Fourth Edition 7 Implementation of Queues as Arrays (continued) To add an element to the queue: −Advance queueRear to next array position −Add element to position pointed by queueRear Example: array size is 100; originally empty

C++ Programming: Program Design Including Data Structures, Fourth Edition 8 Implementation of Queues as Arrays (continued) To delete an element from the queue: −Retrieve element pointed to by queueFront −Advance queueFront to next queue element

C++ Programming: Program Design Including Data Structures, Fourth Edition 9 Implementation of Queues as Arrays (continued) Will this queue design work? −Suppose A stands for adding an element to the queue −And D stands for deleting an element from the queue −Consider the following sequence of operations: AAADADADADADADADA...

C++ Programming: Program Design Including Data Structures, Fourth Edition 10 Implementation of Queues as Arrays (continued) The sequence AAADADADADADADADA... would eventually set queueRear to point to the last array position −Giving the impression that the queue is full

C++ Programming: Program Design Including Data Structures, Fourth Edition 11 Implementation of Queues as Arrays (continued) Solution 1: −When the queue overflows to the rear (i.e., queueRear points to the last array position): Check value of queueFront If value of queueFront indicates that there is room in the front of the array, slide all of the queue elements toward the first array position Problem: too slow for large queues Solution 2: assume that the array is circular

C++ Programming: Program Design Including Data Structures, Fourth Edition 12 Implementation of Queues as Arrays (continued) To advance the index in a (logically) circular array:

C++ Programming: Program Design Including Data Structures, Fourth Edition 13 Implementation of Queues as Arrays (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition 14 Implementation of Queues as Arrays (continued) Case 1:

C++ Programming: Program Design Including Data Structures, Fourth Edition 15 Implementation of Queues as Arrays (continued) Case 2:

C++ Programming: Program Design Including Data Structures, Fourth Edition 16 Implementation of Queues as Arrays (continued) Problem: −Figures and have identical values for queueFront and queueRear −However, the former represents an empty queue, whereas the latter shows a full queue Solution?

C++ Programming: Program Design Including Data Structures, Fourth Edition 17 Implementation of Queues as Arrays (continued) Solution 1: keep a count −Incremented when a new element is added to the queue −Decremented when an element is removed −Initially, set to 0 −Very useful if user (of queue) frequently needs to know the number of elements in the queue We will implement this solution

C++ Programming: Program Design Including Data Structures, Fourth Edition 18 Implementation of Queues as Arrays (continued) Solution 2: let queueFront indicate index of the array position preceding the first element − queueRear still indicates index of last one −Queue empty if: queueFront == queueRear −Slot indicated by queueFront is reserved Queue can hold 99 (not 100) elements −Queue full if the next available space is the reserved slot indicated by queueFront

C++ Programming: Program Design Including Data Structures, Fourth Edition 19 Implementation of Queues as Arrays (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition 22 Empty Queue and Full Queue

C++ Programming: Program Design Including Data Structures, Fourth Edition 23 Initialize Queue

C++ Programming: Program Design Including Data Structures, Fourth Edition 24 Front Returns the first element of the queue

C++ Programming: Program Design Including Data Structures, Fourth Edition 25 Back Returns the last element of the queue

C++ Programming: Program Design Including Data Structures, Fourth Edition 26 addQueue

C++ Programming: Program Design Including Data Structures, Fourth Edition 27 deleteQueue

C++ Programming: Program Design Including Data Structures, Fourth Edition 28 Constructors and Destructors

C++ Programming: Program Design Including Data Structures, Fourth Edition 29 Constructors and Destructors (continued) The array to store the queue elements is created dynamically −When the queue object goes out of scope, the destructor simply deallocates the memory occupied by the array

C++ Programming: Program Design Including Data Structures, Fourth Edition 30 Linked Implementation of Queues Array size is fixed: only a finite number of queue elements can be stored in it The array implementation of the queue requires array to be treated in a special way −Together with queueFront and queueRear The linked implementation of a queue simplifies many of the special cases of the array implementation −In addition, the queue is never full

C++ Programming: Program Design Including Data Structures, Fourth Edition 31 Linked Implementation of Queues (continued) Elements are added at one end and removed from the other −We need to know the front of the queue and the rear of the queue Two pointers: queueFront and queueRear

C++ Programming: Program Design Including Data Structures, Fourth Edition 34 Empty and Full Queue The queue is empty if queueFront is NULL The queue is never full

C++ Programming: Program Design Including Data Structures, Fourth Edition 35 Initialize Queue Initializes queue to an empty state −Must remove all the elements, if any

C++ Programming: Program Design Including Data Structures, Fourth Edition 36 addQueue

C++ Programming: Program Design Including Data Structures, Fourth Edition 37 front and back Operations

C++ Programming: Program Design Including Data Structures, Fourth Edition 38 deleteQueue

C++ Programming: Program Design Including Data Structures, Fourth Edition 39 Default Constructor

C++ Programming: Program Design Including Data Structures, Fourth Edition 41 Queue Derived from the class unorderedLinkedListType The linked implementation of a queue is similar to the implementation of a linked list created in a forward manner − addQueue is similar to insertFirst − initializeQueue is like initializeList − isEmptyQueue is similar to isEmptyList − deleteQueue can be implemented as before − queueFront is the same as first − queueRear is the same as last

C++ Programming: Program Design Including Data Structures, Fourth Edition 42 Queue Derived from unordered LinkedListType (continued) We can derive the class to implement the queue from linkedListType −Abstract class: does not implement all the operations However, unorderedLinkedListType is derived from linkedListType −Provides the definitions of the abstract functions of the linkedListType −Therefore, we can derive linkedQueueType from unorderedLinkedListType

C++ Programming: Program Design Including Data Structures, Fourth Edition 43 Application of Queues: Simulation Simulation: a technique in which one system models the behavior of another system Computer simulations using queues as the data structure are called queuing systems

C++ Programming: Program Design Including Data Structures, Fourth Edition 44 Designing a Queuing System Server: the object that provides the service Customer: the object receiving the service Transaction time: service time, or the time it takes to serve a customer Model: system that consists of a list of servers and a waiting queue holding the customers to be served −Customer at front of queue waits for the next available server

C++ Programming: Program Design Including Data Structures, Fourth Edition 45 Designing a Queuing System (continued) We need to know: −Number of servers −Expected arrival time of a customer −Time between the arrivals of customers −Number of events affecting the system Performance of system depends on: −How many servers are available −How long it takes to serve a customer −How often a customer arrives

C++ Programming: Program Design Including Data Structures, Fourth Edition 46 Designing a Queuing System (continued) If it takes too long to serve a customer and customers arrive frequently, then more servers are needed System can be modeled as a time-driven simulation Time-driven simulation: the clock is a counter −The passage of, say, one minute can be implemented by incrementing the counter by 1 −Simulation is run for a fixed amount of time

C++ Programming: Program Design Including Data Structures, Fourth Edition 47 Customer

C++ Programming: Program Design Including Data Structures, Fourth Edition 48 Server

C++ Programming: Program Design Including Data Structures, Fourth Edition 49 Server List A server list is a set of servers −At a given time, a server is either free or busy

C++ Programming: Program Design Including Data Structures, Fourth Edition 50 Waiting Customers Queue When a customer arrives, he/she goes to the end of the queue When a server becomes available, the customer at front of queue leaves to conduct the transaction After each time unit, the waiting time of each customer in the queue is incremented by 1 We can use queueType but must add the operation of incrementing the waiting time

C++ Programming: Program Design Including Data Structures, Fourth Edition 51 Waiting Customers Queue (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition 52 Main Program Algorithm: −Declare and initialize the variables −Main loop (see next slide) −Print results

C++ Programming: Program Design Including Data Structures, Fourth Edition 53 Main Program (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition 54 Summary Stack: items are added/deleted from one end −Last In First Out (LIFO) data structure −Operations: push, pop, initialize, destroy, check for empty/full stack −Can be implemented as array or linked list −Middle elements should not be accessed Postfix notation: operators are written after the operands (no parentheses needed)

C++ Programming: Program Design Including Data Structures, Fourth Edition 55 Summary (continued) Queue: items are added at one end and removed from the other end −First In First Out (FIFO) data structure −Operations: add, remove, initialize, destroy, check if queue is empty/full −Can be implemented as array or linked list −Middle elements should not be accessed −Restricted versions of arrays and linked lists