1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

Slides:



Advertisements
Similar presentations
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.
Advertisements

ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Queue Overview Queue ADT Basic operations of queue
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.
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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 Stacks (Walls & Mirrors - Chapter 6). 2 Overview The ADT Stack Array Implementation of a Stack Linked-List Implementation of a Stack Application Domain:
Stacks and Queues. Sample PMT online… Browse 1120/sumII05/PMT/2004_1/
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.
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.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 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 -
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++
Data Structures Using C++
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
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.
CS Data Structures I Chapter 7 Queue I. 2 Topics Introduction Queue Application Implementation Linked List Array ADT List.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
1 Data Structures and Algorithms Stacks and Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queues Lecture 4. Lecture Objectives  Learn about queues  Examine various queue operations  Learn how to implement a queue as an array  Learn how.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Queue Section 3 6/12/ The Abstract Data Type Queue A queue is a list from which items are deleted from one end (front) and into which items are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
Chapter 7 Queues.
Data Abstraction & Problem Solving with C++
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
CS505 Data Structures and Algorithms
CC 215 Data Structures Queue ADT
CC 215 Data Structures Stack ADT
CENG 213 Data Structure Queue 7/2/2018.
The Abstract Data Type Queue
Stack and Queue APURBO DATTA.
Chapter 5 ADTs Stack and Queue.
Queues.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues Definition of a Queue Examples of Queues
Getting queues right … finally (?)
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Presentation transcript:

1 Queues (Walls & Mirrors - Chapter 7)

2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue

3 The ADT Queue A queue is a linear list, for which all insertions are made at one end of the list; all deletions are made at the other end. Queues exhibit a First In First Out (FIFO) property. This is like the queue (or line) at a movie theater, checkout counter at a store, bus stop, etc. When you arrive, you go to the back of the queue. People are served in the order that they arrive, starting at the front. OUTIN (back)(front)

4 The ADT Queue (Cont’d.) For an ADT Queue, the supported operations include: –Create an empty queue –Destroy a queue –Determine whether a queue is empty –Add a new item to the queue –Remove the item added earliest –Retrieve a copy of the item added earliest As long as these operations are supported, you don’t need to be concerned with whether the queue is implemented as an array, a linked list, or something else.

5 Comparing Stacks and Queues Recall, that a stack is a linear list, for which all insertions and deletions occur at the same end of the list, whereas insertions and deletions occur at opposite ends of a queue. Stacks exhibit a Last In First Out (LIFO) property; queues exhibit a First In First Out (FIFO) property: –In a stack, remove and retrieve act on the the item added most recently; –In a queue, remove and retrieve act on the item added earliest.

6 ADT Queue: Public Member Functions Queue( ); is the default constructor, which creates a new, empty queue. Queue( const Queue &oQueue ); is the copy constructor, which makes a copy of oQueue. ~Queue( ); is the destructor, which destroys a queue. bool isEmpty( ) const; returns true if the queue is empty, otherwise returns false.

7 ADT Queue: Public Member Functions bool enqueue( QueueItemType newItem ); adds newItem at the back of a queue. Returns true if enqueue succeeds, otherwise returns false. bool dequeue( ); removes the item at the front of a queue. Returns true if dequeue succeeds, otherwise returns false. bool dequeue( QueueItemType &queueFront ); removes the item at the front of a queue and returns it in queueFront. Returns true if dequeue succeeds, otherwise returns false.

8 ADT Queue: Public Member Functions bool getFront( QueueItemType &queueFront ) const; retrieves a copy of the item at the front of a queue and returns it in queueFront, leaving the queue unchanged. Returns true if getFront succeeds, otherwise returns false.

9 Application: Recognizing Palindromes Recall, that a palindrome is a string that reads the same from left to right as right to left. –NOON, DEED, RADAR, MADAM –ABLE WAS I ERE I SAW ELBA One way to recognize a palindrome is to copy the candidate string into both a stack and a queue. Remove the characters one at a time from the stack and queue and compare: –If a difference is found, the candidate string is not a palindrome. –If all characters match, the candidate string is a palindrome.

10 Recognizing Palindromes:“RADAR” Step 1: Copy “RADAR” into Queue and Stack:

11 Recognizing Palindromes:“RADAR” Step 2: Remove “RADAR” from Queue and Stack:

12 Recognizing Palindromes bool isPal( str char[ ] ) { Queue charQueue; Stack charStack; // copy each character of str[ ] into both charQueue and charStack bool match; char queueFront, stackTop; // set match to true if characters removed from charQueue match // characters removed from charStack; otherwise set match to false return match; }

13 Recognizing Palindromes // copy each character of str[ ] into both charQueue and charStack for( int i = 0; i < strlen( str ) – 1; i++ ) { charQueue.enqueue( str[i] ); charStack.push( str[i] ); } // set match to true if characters removed from charQueue match // characters removed from charStack; otherwise set match to false for( match = true; match && !charQueue.isEmpty( ); ) { charQueue.dequeue( queueFront ); charStack.pop( stackTop ); if( queueFront != stackTop ) match = false; }

14 ADT Queue: Linked-List Implementation typedef int QueueItemType;// items in Queue are int’s struct QueueNode;// nodes in Queue are struct’s class Queue { public: // declarations of public member functions private: QueueNode *backPtr;// pointer to back of Queue }; struct QueueNode// place this in Implementation File: {// declaration of QueueNode for QueueItemType item;// circular, linked list QueueNode *next; };

15 ADT Queue: Member Function Definitions // default constructor, which creates a new empty Queue, // initializing backPtr to NULL Queue::Queue( ) : backPtr( NULL ) { }

16 Queue( const Queue &oQueue ) 5813 oQueue.backPtr

17 Queue( const Queue &oQueue ) backPtr 13 pnew oQueue.backPtrporig

18 ADT Queue: Member Function Definitions // copy constructor, which copies oQueue to a new Queue Queue::Queue( const Queue &oQueue ) { if( oQueue.backPtr = = NULL ) backPtr = NULL; else { // copy node at back of Queue backPtr = new QueueNode; assert( backPtr != NULL ); backPtr -> item = oQueue.backPtr -> item; // copy rest of Queue }

19 ADT Queue: Member Function Definitions // copy rest of Queue QueueNode *pnew = backPtr; for( QueueNode *porig = oQueue.backPtr -> next; porig != oQueue.backPtr ; porig = porig -> next ) { pnew -> next = new QueueNode; assert( pnew -> next != NULL ); pnew = pnew -> next; pnew -> item = porig -> item; } pnew -> next = backPtr;

20 ADT Queue: Member Function Definitions // destructor removes items from Queue until empty Queue::~Queue( ) { while( dequeue( ) ); } // returns true if Queue is empty, otherwise returns false bool Queue::isEmpty( ) const { return backPtr = = NULL; }

21 enqueue( QueueItemType newItem ) 5813 pnewnewItem 21 backPtr pnew 21 backPtr

22 ADT Queue: Member Function Definitions // adds newItem at the back of a Queue. Returns true if // enqueue succeeds, otherwise returns false. bool Queue::enqueue( QueueItemType newItem ) { QueueNode *pnew = new QueueNode; if( pnew = = NULL ) return false; pnew -> item = newItem; if( isEmpty( ) ) pnew -> next = pnew; else { pnew -> next = backPtr -> next; backPtr -> next = pnew; } backPtr = pnew; return true; }

23 dequeue( ) 21 frontPtr backPtr backPtrfrontPtr 5

24 ADT Queue: Member Function Definitions // removes the item at the front of a Queue. Returns true if // dequeue succeeds, otherwise returns false. bool Queue::dequeue( ) { if( isEmpty( ) ) return false; QueueNode *frontPtr = backPtr -> next; if( frontPtr = = backPtr ) backPtr = NULL; else backPtr -> next = frontPtr -> next; frontPtr -> next = NULL; delete frontPtr; return true; }

25 ADT Queue: Member Function Definitions // removes the item at the front of a Queue and returns it in // queueFront. Returns true if dequeue succeeds, otherwise // returns false. bool Queue::dequeue( QueueItemType &queueFront ) { if( isEmpty( ) ) return false; queueFront = (backPtr -> next) -> item; return dequeue( ); }

26 ADT Queue: Member Function Definitions // retrieves a copy of the item at the front of a Queue and returns // it in queueFront, leaving Queue unchanged. Returns true if // dequeue succeeds, otherwise returns false. bool Queue::getFront( QueueItemType &queueFront ) const { if( isEmpty( ) ) return false; queueFront = (backPtr -> next) -> item; return true; }

27 ADT Queue: Array Implementation Suppose that we chose the following, array implementation for our ADT Queue front 0 back 5 As usual, additions occur at the back of the queue, and deletions occur at the front.

28 ADT Queue: Array Implementation Here is what the array will look like after 4 deletions front 4 back 5 Although the array is nearly empty, only two positions (6 and 7) are available for new additions, since additions occur only at the back. This is called rightward drift.

29 ADT Queue: Array Implementation The problem of rightward drift can be solved by using the vacant positions at the front of the array when the positions at the back are filled. One way of representing this in a diagram is as follows front 5 back

30 ADT Queue: Array Implementation Now, after 4 deletions we have: And, after 4 subsequent additions we have: front 5 back front 1 back

31 ADT Queue: Array Implementation When the array is full we have: If we subsequently make 8 deletions we have: front 3 back front 3 back

32 Consequently, we need a way to distinguish between an empty array and a full array: –One way to solve this is to keep a count of the number of items in the array. –Another way is to keep a flag that indicates whether or not the array is full. (1 bit is sufficient.) –A third way is to reserve the the position immediately before the front of the array so that no data is stored there. (See Walls & Mirrors, chapter 7 for details.) We will keep a count of the number of items in the array. ADT Queue: Array Implementation

33 We also need a convenient way to find the next position in the circular array. If the array has space for MaxQueue items, then the position after position pos is given by (pos + 1) % MaxQueue where x % y gives the remainder when x is divided by y (modulus operator). Examples (assume MaxQueue = 8): (0 + 1) % 8 = 1(6 + 1) % 8 = 7 (1 + 1) % 8 = 2(7 + 1) % 8 = 0 ADT Queue: Array Implementation

34 ADT Queue: Array Implementation const int MaxQueue = 20;// maximum Queue size typedef int QueueItemType;// items in Queue are int’s class Queue { public: // declarations of public member functions - same as for // linked list private: QueueItemType items[ MaxQueue ]; int front;// index to front of items[ ] int back;// index to back of items[ ] int count;// number of elements in items[ ] };

35 ADT Queue: Member Function Definitions // default constructor, which creates a new empty Queue, // initializing front, back, and count Queue::Queue( ) : front( 0 ), back( MaxQueue – 1 ), count( 0 ) { }

36 ADT Queue: Member Function Definitions // copy constructor, which copies oQueue to a new Queue Queue::Queue( const Queue &oQueue ) : front( oQueue.front ), back( oQueue.back ), count( oQueue.count ) { int pos = oQueue.front; for( int i = 0; i < oQueue.count; i++ ) { items[ pos ] = oQueue.items[ pos ]; pos = (pos + 1) % MaxQueue; }

37 ADT Queue: Member Function Definitions // destructor - invokes the destructor of static arrays, // supplied by the compiler Queue::~Queue( ) { } // returns true if the Queue is empty, otherwise returns false bool Queue::isEmpty( ) const { return count = = 0; }

38 ADT Queue: Member Function Definitions // adds newItem at the back of a Queue. Returns true if // enqueue succeeds, otherwise returns false. bool Queue::enqueue( QueueItemType newItem ) { if( count = = MaxQueue ) return false; back = (back + 1) % MaxQueue; items[ back ] = newItem; count ++; return true; }

39 ADT Queue: Member Function Definitions // removes the item at the front of a Queue. Returns true if // dequeue succeeds, otherwise returns false. bool Queue::dequeue( ) { if( isEmpty( ) ) return false; front = (front + 1) % MaxQueue; count – –; return true; }

40 ADT Queue: Member Function Definitions // removes the item at the front of a Queue and returns it in // queueFront. Returns true if dequeue succeeds, otherwise // returns false. bool Queue::dequeue( QueueItemType &queueFront ) { if( isEmpty( ) ) return false; queueFront = items[ front ]; front = (front + 1) % MaxQueue; count – –; return true; }

41 ADT Queue: Member Function Definitions // retrieves a copy of the item at the front of a Queue and returns // it in queueFront, leaving Queue unchanged. Returns true if // dequeue succeeds, otherwise returns false. bool Queue::getFront( QueueItemType &queueFront ) const { if( isEmpty( ) ) return false; queueFront = items[ front ]; return true; }

42 Queueing Simulation Read this “case study’ in Chapter 7 of your text, Walls & Mirrors. This is a good illustration of how queues in a computer program can be used to predict the performance of queues in real life. Key concepts include –simulated vs. real time –time-driven vs. event-driven simulation –the use of event lists in event-driven simulations