Presentation is loading. Please wait.

Presentation is loading. Please wait.

7- 1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists.

Similar presentations


Presentation on theme: "7- 1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists."— Presentation transcript:

1 7- 1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists.

2 7- 2 Linked Data Structures Linked data structures (or simply data structures), are structures represent objects linked in one of the following types: Linear data structures: –Linked lists: single, double, circuliar. –Stack (LIFO). –Queue (FIFO). Non-Linear data structures : –Tree: binary, multi-branch. –Graph.

3 7- 3 Queues Definition of a Queue: A queue is a linear list in which data is inserted at one end (the rear or tail), and from which data is removed from the other end (the front or head). FIFO. The length of a queue is the number of elements it contains. An empty queue has length zero. Queues are used to help divide subtasks.

4 7- 4 Priority Queues What if items in the queue have an order –Usually termed a priority queue –We must sort the items so that the highest (lowest ) priority item is removed first Items have some ordering relation –It doesn’t matter much what it is –As long as there’s some way to define order

5 7- 5 Queue concepts (1) A queue is a first-in-first-out sequence of elements. Elements can added only at one end (the rear of the queue) and removed only at the other end (the front of the queue).

6 7- 6 Queue concepts (2) Illustration (queue of cars):

7 7- 7 Queue applications Print server –maintains a queue of print jobs. Disk driver –maintains a queue of disk input/output requests. Scheduler (e.g., in an operating system) –maintains a queue of processes awaiting a slice of machine time. Services Queues, i.e. waiting in line for a bank teller

8 7- 8 Queue ADT: requirements Requirements: 1)It must be possible to make a queue empty. 2)It must be possible to test whether a queue is empty. 3)It must be possible to obtain the length of a queue. 4)It must be possible to add an element at the rear of a queue. 5)It must be possible to remove the front element from a queue. 6)It must be possible to access the front element in a queue without removing it.

9 7- 9 Queue ADT: contract (1) Possible contract, in C++: class Queue { // Each Queue object is a queue whose elements are / / objects. ///////////// Accessors /////////////// bool isEmpty (); // Return true if and only if this queue is empty. int size (); // Return this queue’s length. char getFirst (); // Return the element at the front of this queue.

10 7- 10 Queue ADT: contract (2) Possible contract (continued): /////////// Transformers //////////// void clear (); // Make this queue empty. void addLast (char elem); // Add elem as the rear element of this queue. char removeFirst (); // Remove and return the front element of this queue. }

11 7- 11 Invariant: 0frontrear–1maxlen–1 element Implementation of queues using arrays (1) Consider representing a bounded queue (length  maxlen) by: –a variable length, containing the current length –variables front and rear –an array elems of length maxlen, containing the queued elements in elems[front…rear–1]: Empty queue: 0maxlen–1front=rear unoccupied rear elementfront element

12 7- 12 Implementation using arrays (2) Animation (with maxlen = 6): Initially: 012345 0 front 0 rear elems 0 length 0 Hamed 1 Murad 2 Baher 3 Layla 45 0 front 4 rear elems 4 length After adding Hamed, Murad, Baher, Layla: 0 Hamed 1 Murad 2 Baher 3 Layla 4 Maged 5 0 front 5 rear elems 5 length After adding Maged: 01 Murad 2 Baher 3 Layla 4 Maged 5 1 front 5 rear elems 4 length After removing the front element: 012 Baher 3 Layla 4 Maged 5 2 front 5 rear elems 3 length After removing the front element: 012 Baher 3 Layla 4 Maged 5 Radi 2 front 0 rear elems 4 length After adding Radi:

13 7- 13 Implementation using arrays (3) If we must shift elements along the array, operations addLast and removeFirst will have time complexity O(n). We can avoid this if we use a “cyclic array” instead of an ordinary array.

14 7- 14 Implementation of queues using SLLs (1) Represent an (unbounded) queue by: –a SLL, whose first node contains the front element, and whose header contains links to the first node (front) and last node (rear). –a variable length (optional). Invariant: element front rear Empty queue: front rear Illustration: front rear Hamed Murad Baher Layla

15 7- 15 Implementation using SLLs (2) C++ implementation: / / Queue Node declaration struct QueNode{ char Data; // Node Information struct QueNode* Next; // Node Communication };

16 7- 16 Implementation using SLLs (2) C++ implementation (continued): // Queue ADT declaration class QueSLL { private: QueNode *Front, *Rear; int Length; public: //////////// Constructor //////////// QueSLL () { Front = Rear = NULL; Length = 0;} ;

17 7- 17 Implementation using SLLs (2) C++ implementation (continued): //////////// Accessors //////////// int Size () { return Length;}; void PrintQue(); int QueIsEmpty() { // return (Front==NULL); if (Front==NULL) { cout << "Queue is Empty" << endl; return TRUE; } else return FALSE; }

18 7- 18 Implementation using SLLs (2) C++ implementation (continued): //////////// Transformers //////////// // Add node to the the Que rear void Add(char c) { QueNode* p; // Create a node in memory p=new QueNode; // Fill node p->Data = c; p->Next = NULL; // Add node to Queue if (Front == NULL) Front = Rear = p; // If Que is empty else{ Rear->Next=p; Rear=Rear->Next; } Length++; }

19 7- 19 Implementation using SLLs (2) C++ implementation (continued): //////////// Transformers //////////// // Delete a node from the the Que front void Delete() { QueNode* p = Front; if(p){ Front=Front->Next; delete p; Length--; } else { cout << "Empty Queue..." << endl; } } }; // End of class QueSLL

20 7- 20 Implementation using SLLs (3) Possible implementation for main(): void main() { QueSLL Qlist; Qlist.Add('a'); Qlist.Add('b'); Qlist.Add('c'); Qlist.PrintQue(); cout << "Number of Nodes in Queue = " << Qlist.Size() << endl; Qlist.Add('a'); Qlist.Add('b'); Qlist.Add('c'); Qlist.Add('d'); Qlist.Add('e'); Qlist.Add('f'); cout << "Number of Nodes in Queue = " << Qlist.Size() << endl; while (!Qlist.QueIsEmpty()) cout << Qlist.Delete() << endl; Qlist.Delete(); } Que_SLL2.CPP

21 7- 21 Output a b c Number of Nodes in Queue = 3 a b c a b c d e f Number of Nodes in Queue = 9 Queue is Empty Number of Nodes in Queue = 0 Implementation using SLLs (8)

22 7- 22 Comparison of implementations We can similarly implement a priority queue using a sorted or an unsorted array. Time complexities of main operations: OperationUnsorted SLLUnsorted Array Add() O(1) Delete() O(n)O(n)O(n)O(n) PrintQue() O(n)O(n)O(n)O(n)


Download ppt "7- 1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists."

Similar presentations


Ads by Google