Data Structures Using C++ 2E

Slides:



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

Ceng-112 Data Structures I Chapter 5 Queues.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Data Structure Dr. Mohamed Khafagy.
Jerry Lebowitz.  Stacks  Queues 3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition.
DATA STRUCTURE & ALGORITHMS
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Chapter 18: Stacks and Queues
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition 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++
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
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)
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.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
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)
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
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.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
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
Review Array Array Elements Accessing array elements
C++ Programming:. Program Design Including
Data Structures Using C++ 2E
Data Structures Using C, 2e
Queues.
Queues Chapter 4.
QUEUES.
CC 215 Data Structures Queue ADT
Queues.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Queues Chapter 4.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
DATA STRUCTURE QUEUE.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks Data structure Elements added, removed from one end only
Visit for more Learning Resources
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Queues Definition of a Queue Examples of Queues
CSC 248 – fundamentals of Data structure
Getting queues right … finally (?)
Presentation transcript:

Data Structures Using C++ 2E Chapter 8 Queues Edited by Malak Abdullah Jordan University of Science and Technology 1

Introduction Queue data structure Elements added at one end (rear), deleted from other end (front) First In First Out (FIFO) Middle elements inaccessible

Queue Operations Two key operations Additional operations addQueue deleteQueue Additional operations initializeQueue, isEmptyQueue, isFullQueue, front, back queueFront, queueRear pointers Keep track of front and rear See code on pages 453-454

Implementation of Queues as Arrays Four member variables Array to store queue elements Variables queueFront, queueRear Variable maxQueueSize Using queueFront, queueRear to access queue elements queueFront: first queue element index queueRear: last queue element index queueFront changes after each deleteQueue operation queueRear changes after each addQueue operation

Empty Queue 0 1 2 3 4 5 6 ....... 19 list maxSize 20 front rear 19 0 1 2 3 4 5 6 ....... 19 list maxSize 20 front rear 19 count

addQueue(‘R’) 0 1 2 3 4 5 6 ....... 19 list maxSize 20 R front rear 0 1 2 3 4 5 6 ....... 19 list maxSize 20 R front rear count 1

addQueue(‘T’) 0 1 2 3 4 5 6 ....... 19 list maxSize 20 R T front rear 0 1 2 3 4 5 6 ....... 19 list maxSize 20 R T front rear 1 count 2

addQueue(‘P’) 0 1 2 3 4 5 6 ....... 19 list maxSize 20 R T P front 0 1 2 3 4 5 6 ....... 19 list maxSize 20 R T P front rear 2 count 3

deleteQueue() 0 1 2 3 4 5 6 ....... 19 list maxSize 20 R T P front 1 0 1 2 3 4 5 6 ....... 19 list maxSize 20 R T P front 1 rear 2 count 2

addQueue(‘M’) 0 1 2 3 4 5 6 ....... 19 list maxSize 20 R T P M front 1 0 1 2 3 4 5 6 ....... 19 list maxSize 20 R T P M front 1 rear 3 count 3

Implementation of Queues as Arrays (cont’d.) Execute operation addQueue(Queue,'A'); Execute addQueue(Queue,'B'); addQueue(Queue,'C'); deleteQueue(); Data Structures Using C++ 2E

FIGURE 8-3 Queue after the deleteQueue operation FIGURE 8-2 Queue after two more addQueue operations FIGURE 8-1 Queue after the first addQueue operation Data Structures Using C++ 2E

Implementation of Queues as Arrays (cont’d.) Consider the sequence of operations: AAADADADADADADADA... Eventually index queueRear points to last array position Looks like a full queue Reality: queue has two or three elements, array empty in the front FIGURE 8-4 Queue after the sequence of operations AAADADADADADA... Data Structures Using C++ 2E

Implementation of Queues as Arrays (cont’d.) First solution Upon queue overflow to the rear Check value of queueFront If room in front: slide all queue elements toward first array position Works if queue size very small Second solution: assume circular array FIGURE 8-5 Circular queue Data Structures Using C++ 2E

We need to add ‘B’ !!???!! 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 R T P M S A F D front 16 rear 19 It seems full but it is not count 4 First Solution 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 S A F D B front rear 4 count 5 Imagine 1000000 elements????

We need to add ‘B’ !!???!! 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 R T P M S A F D front 16 rear 19 It seems full but it is not count 4 Second Solution 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 B T P M S A F D front 16 1 rear 19 count 5 18

How To implement this solution Second Solution 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 R T P M S A F D front 16 1 rear 19 19 count 5 18 If (rear ==19 && count < 20) then { rear= 0; } list(rear)= ‘B’ count++; rear= (rear +1 ) % 20 List [rear] =‘B’ count++;

Implementation of Queues as Arrays(cont’d.) queueRear = (queueRear + 1) % maxQueueSize; Advances queueRear (queueFront) to next array position FIGURE 8-6 Queue before and after the add operation Data Structures Using C++ 2E

Implementation of Queues as Arrays(cont’d.) If queueRear < maxQueueSize – 1 queueRear + 1 <= maxQueueSize – 1 (queueRear + 1) % maxQueueSize = queueRear + 1 If queueRear == maxQueueSize – 1 queueRear + 1 == maxQueueSize (queueRear + 1) % maxQueueSize = 0 queueRear set to zero First array position f r Data Structures Using C++ 2E

Implementation of Queues as Arrays (cont’d.) Two cases with identical queueFront, queueRear values Figure 8-7(b) represents an empty queue Figure 8-8(b) represents a full queue FIGURE 8-7 Queue before and after the delete operation FIGURE 8-8 Queue before and after the add operation Data Structures Using C++ 2E

See More 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 R T P M 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 R T P M front 3 rear 3 count 1 deleteQueue(); 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 R T P M front 4 As You see (empty Queue) front= rear +1 rear 3 count

See More 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 R T M F T A 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 R T M F T A S D Q U front 4 rear 2 count 19 addQueue(‘P’); 0 1 2 3 4 5 6 ....... 16 17 18 19 list maxSize 20 R T M P T A S D Q U front 4 rear 3 As You see (Full Queue) front= rear +1 count 20

Implementation of Queues as Arrays (cont’d.) First solution: use variable count Incremented when new element added Decremented when element removed Functions initializeQueue, destroyQueue initialize count to zero Data Structures Using C++ 2E

Implementation of Queues as Arrays (cont’d.) Second solution queueFront indicates index of array position preceding first element of the queue Assume queueRear indicates index of last element Empty queue if queueFront == queueRear Slot indicated by index queueFront is reserved Queue is full If next available space represents special reserved slot Data Structures Using C++ 2E

Implementation of Queues as Arrays (cont’d.) FIGURE 8-9 Array to store the queue elements with a reserved slot See code on pages 459-460 Uses first solution Data Structures Using C++ 2E

Empty Queue and Full Queue If count == 0 Full queue If count == maxQueueSize Data Structures Using C++ 2E

Initialize Queue Initializes queue to empty state First element added at the first array position Initialize queueFront to zero, queueRear to maxQueueSize - one, count to zero FIGURE 8-10 Empty queue Data Structures Using C++ 2E

Front Returns first queue element If the queue nonempty Otherwise Element indicated by index queueFront returned Otherwise Program terminates Data Structures Using C++ 2E

Back Returns last queue element If queue nonempty Otherwise Returns element indicated by index queueRear Otherwise Program terminates Data Structures Using C++ 2E

Add Queue Data Structures Using C++ 2E

Delete Queue Data Structures Using C++ 2E

Constructors and Destructors Data Structures Using C++ 2E

Constructors and Destructors (cont’d.) Array storing queue elements Created dynamically When queue object goes out of scope Destructor deallocates memory occupied by the array storing queue elements Data Structures Using C++ 2E

Linked Implementation of Queues Array implementation issues Fixed array size Finite number of queue elements Requires special array treatment with the values of the indices queueFront, queueRear Linked implementation of a queue Simplifies special cases of the array implementation Queue never full See code on pages 464-465 Data Structures Using C++ 2E

Linked Queue front rear front 5 22 16 rear

addQueu(9) front 5 22 16 rear nodeType *N; Ninfo= 9; Nlink=NULL; rearlink=N; rear= N; front 5 22 16 9 rear

deleteQueu() front 5 22 16 rear nodeType *p; p= front; front= frontlink; Delete p; front 22 16 rear

Empty and Full Queue Empty queue if queueFront is NULL Memory allocated dynamically Queue never full Function implementing isFullQueue operation returns the value false Data Structures Using C++ 2E

Initialize Queue Initializes queue to an empty state Empty if no elements in the queue Data Structures Using C++ 2E

addQueue, front, back, and deleteQueue Operations addQueue operation adds a new element at end of the queue Access the pointer queueRear Data Structures Using C++ 2E

addQueue, front, back, and deleteQueue Operations (cont’d.) If queue nonempty Operation front returns first element Element indicated queueFront returned If queue empty: front terminates the program Data Structures Using C++ 2E

addQueue, front, back, and deleteQueue Operations (cont’d.) If queue nonempty Operation back returns last element Element indicated by queueRear returned If queue empty: back terminates the program Data Structures Using C++ 2E

addQueue, front, back, and deleteQueue Operations (cont’d.) If queue nonempty Operation deleteQueue removes first element Access pointer queueFront Data Structures Using C++ 2E Edited by Malak Abdullah Jordan University of Science and Technology

addQueue, front, back, and deleteQueue Operations (cont’d.) Default constructor When queue object goes out of scope Destructor destroys the queue Deallocates memory occupied by the queue elements Function definition similar to function initializeQueue Data Structures Using C++ 2E

Queue Derived from the class unorderedLinkedListType Linked queue implementation Similar to forward manner linked list implementation Similar operations add Queue, insertFirst initializeQueue , initializeList isEmptyQueue, isEmptyList deleteQueue operation implemented as before Same pointers queueFront and first, queueRear and last Data Structures Using C++ 2E

Queue Derived from the class unorderedLinkedListType (cont’d.) Linked queue implementation (cont’d.) Can derive the class to implement the queue from the class linkedListType class linkedListType An abstract Does not implement all operations class unorderedLinkedListType Derived from class linkedListType Provides definitions of the abstract functions of the class linkedListType Data Structures Using C++ 2E

STL class queue (Queue Container Adapter) Standard Template Library (STL) Provides a class to implement queues in a program Queue Name of class defining the queue Name of header defining class queue Data Structures Using C++ 2E

STL class queue (cont’d.) Queue container class Provides relational operators comparing two queues See Example 8-2 TABLE 8-1 Operations on a queue object Data Structures Using C++ 2E

Priority Queues Queue structure ensures items processed in the order received Priority queues Customers (jobs) with higher priority pushed to the front of the queue Implementation Ordinary linked list Keeps items in order from the highest to lowest priority Treelike structure Very effective Chapter 10 Data Structures Using C++ 2E

Application of Queues: Simulation Queuing systems Computer simulations Queues represent the basic data structure Queues of objects Waiting to be served by various servers Consist of servers and queues of objects waiting to be served Data Structures Using C++ 2E

Designing a Queuing System Server Object that provides the service Customer Object receiving the service Transaction time (service time) Time required to serve a customer Queuing system consists of servers, queue of waiting objects Model system consisting of a list of servers; waiting queue holding the customers to be served Data Structures Using C++ 2E

Summary Queue Standard Template Library (STL) Priority Queue First In First Out (FIFO) data structure Implemented as array or linked list Linked lists: queue never full Standard Template Library (STL) Provides a class to implement a queue in a program Priority Queue Customers with higher priority pushed to the front Simulation Common application for queues Data Structures Using C++ 2E