1 / 20

Queues – Chapter 3

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 the other end called the front of the queue

marly
Download Presentation

Queues – Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queues – Chapter 3 • A queue is a data structure in which all additions are made at one end called the rear of the queueandall deletions are made from the other end called the front of the queue • Alternatively, in a queue the element deleted is the one that stayed in the queue the longest. This is also called first-in-first-out (FIFO) • Classical example of queues is a queue of people waiting to pay bills.

  2. Queue Concept and Example • A queue has a Front and a Rear • The insert operation is often called Enqueue • The delete operation is often called Dequeue Queue of people waiting to pay bills Rear of the queue Next person will join the queue from the rear Front of the queue Next person to be served

  3. Queue ADT • A queue is a data structure in which all additions are made at one end called the rear of the queueandall deletions are made from the other end called the front of the queue • Common queue operations: • Enqueue(item)– Add the item to the end of the Q • Dequeue() – Remove & return the item at the front • isEmpty() – Return true if the Q is empty • isFull() – Return true if the Q is full

  4. How do we implement queue ADT? • 2 ways to implement a queue • Using an array • Using a linked list

  5. Array Implementation of Queues: Operations • Use an array int Q[N] • front points to the front element of the queue, i.e., holds the index of the array Q that contains the front of the queue • rear points to next slot after the last element in the queue • noItems contains the current number of items in the queue. • An empty queue is one where noItems = 0 • A full queue is one where noItems = N

  6. 5 6 2 4 3 0 1 • Front and rear are equal to each other and noItems = 0 in an empty Queue An Empty Queue Q rear = 3 front = 3 noItems = 0 5 6 2 4 3 0 1 • Front points to the first element in the Queue • Rear points to the next slot after the last element in the Queue A partially-full Queue Q 7 8 2 15 9 4 6 noItems = 3 rear = 6 front = 3 5 6 2 4 3 0 1 • Front and rear are equal to each other and noItems = N=7 in a full Queue A Full Queue Q 7 8 2 15 9 4 6 front = 3 noItems = 7 rear = 3 Array Implementation of Queues • We can implement a queue of at most “N” elements with an array int Q[N] and 3 variables: int front, int rear, int noItems as follows

  7. Array Implementation of Queues 5 6 2 4 3 0 1 noItems = 3 Initial Queue Q 8 15 6 front = 3 rear = 6 5 6 2 4 3 0 1 noItems = 4 After 4 is inserted Q 8 15 4 6 rear = 0 front = 3 Conceptual view of the final queue: circular array 5 6 2 4 3 0 1 noItems = 5 After 20 is inserted Q 8 15 6 20 4 6 Front = 4 rear = 1 front = 3 5 4 0 Rear = 1 8 20 noItems = 4 5 6 2 4 3 0 1 After 15 is removed 1 6 4 Q 8 6 20 4 2 3 rear = 1 front = 4

  8. Queue Using Arrays: Declarations & Operations classQueue { private: static intN = 100; // size of the queue intQ[]; // Array holding the queue elements (ints) intfront; // front of the queue intrear; // rear of the queue intnoItems; // # of items in the queue public: Queue(); boolisEmpty(); boolisFull(); intEnqueue(int item); intDequeue(); };

  9. Queue Operations: Constructor, isEmpty, isFull // Constructor Queue(){ Q = new int[N]; front = rear = noOfItems = 0; } //end-Queue // Returns true if the Q is empty boolisEmpty(){ return noOfItems == 0; } //end-isEmpty // Returns true if the Q is full boolisFull(){ return noOfItems == N; } //end-isFull

  10. Queue Operations: Enqueue // Inserts a new item into the queue // Returns 0 on success, -1 on failure intEnqueue(intnewItem){ if (isFull()){ println(“Queue is full”); return -1; } //end-if Q[rear] = newItem; // Put the new item at the end rear++; if (rear == N) rear = 0; // Now move rear noItems++; // One more item in the queue return 0; } //end-EnQueue

  11. Queue Operations: Dequeue // Removes and returns the item at the front of the queue // If the queue is empty, returns -1 (an error) intDequeue(){ intidx = -1; if (isEmpty()){ println(“Queue is empty”); return -1; } //end-if idx = front; // This is where the first item is. front++; if (front == N) front = 0; // Move front noItems--; // One less item in the Queue return Q[idx]; // Return the item } //end-Dequeue

  12. Queue Usage Example main(){ Queue q = new Queue(); if (q.isEmpty()) println(“Queue is empty”); // Q empty now q.Enqueue(49); q.Enqueue(23); println(“Front of the Q is: ” + q.Dequeue()); // prints 49 q.Enqueue(44); q.Enqueue(22); println(“Front of the Q is: ” + q.Dequeue()); // prints 23 println(“Front of the Q is: ” + q.Dequeue()); // prints 44 println(“Front of the Q is: ” + q.Dequeue()); // prints 22. println(“Front of the Q is: ” + q.Dequeue()); // prints -1 if (q.isEmpty()) println(“Queue is empty”); // Q empty now } //end-main

  13. Array Implementation of Queues: Last Word • We can implement a queue of at most “N-1” elements with an array int Q[N] and 2 variables: int front, int rear • Think about how you would define • An empty queue • A full queue • Front and rear are equal to each other in an empty queue An Empty Queue Q rear = 3 front = 3 • There is one empty space between rear and front in a full queue A Full Queue Q 7 8 2 15 4 6 front = 3 rear = 2

  14. Linked-List implementation of Queues Front Rear Initial Queue Enqueue(3) Dequeue() Dequeue() Front Rear After 3 is enqueued 6 2 15 3 9 15 6 9 2 15 3 6 2 15 3 6 Front Rear After 9 is dequeued Front Rear After 2 is dequeued

  15. Queue using Linked List: Declarations & Operations structQueueNode { int item; QueueNodenext; QueueNode(int e){item=e; next=null;} }; classQueue{ private: QueueNodefront; // Ptr to the front of the Q QueueNoderear; // Ptr to the rear of the Q public: Queue(); boolisEmpty(); void Enqueue(int item); intDequeue(); };

  16. Queue Operations: Constructor, isEmpty // Constructor Queue(){ front = rear = null; } //end-Queue // Returns true if the Q is empty boolisEmpty(){ return front == null; } //end-isEmpty

  17. Queue Operations: Enqueue // Inserts a new item into the queue void Enqueue(intnewItem){ // Allocate a QueueNode for the item QueueNodenode = new QueueNode(newItem); if (front == NULL){ front = rear = node; } else { rear.next= node; rear = node; } //end-else } //end-EnQueue

  18. Queue Operations: Dequeue // Removes and returns the item at the front of the queue // If the queue is empty, returns -1 (an error) intDequeue(){ if (isEmpty()){ println(“Queue is empty”); return -1; } //end-if QueueNodetmp= front; // Keep a ptr to the front node front = front.next; // Remove the front node if (front == null) rear = NULL; // Empty Q? // Return the item return tmp.item; } //end-Dequeue

  19. Queue Usage Example main(){ Queue q = new Queue(); if (q.isEmpty()) println(“Queue is empty”); // Q empty now q.Enqueue(49); q.Enqueue(23); println(“Front of the Q is: ” + q.Dequeue()); // prints 49 q.Enqueue(44); q.Enqueue(22); println(“Front of the Q is: ” + q.Dequeue()); // prints 23 println(“Front of the Q is: ” + q.Dequeue()); // prints 44 println(“Front of the Q is: ” + q.Dequeue()); // prints 22. println(“Front of the Q is: ” + q.Dequeue()); // prints -1 if (q.isEmpty()) println(“Queue is empty”); // Q empty now } //end-main

  20. Applications of Queues • File servers: Users needing access to their files on a shared file server machine are given access on a FIFO basis • Printer Queue: Jobs submitted to a printer are printed in order of arrival • Phone calls made to customer service hotlinesare usually placed in a queue • Expected wait-time of real-life queues such as customers on phone lines may be too hard to solve analytically use queue for simulating real-life queues

More Related