Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.

Similar presentations


Presentation on theme: "Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY."— Presentation transcript:

1 Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY

2 The Queue Operations  A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ Front Rear

3 The Queue Operations  New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. $ Front Rear

4 The Queue Operations  When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ Front Rear

5 The Queue Class  The C++ standard template library has a queue template class.  The template parameter is the type of the items that can be put in the queue. template class queue { public: queue( ); void push(const Item& entry); void pop( ); bool empty( ) const; Item front( ) const; … };

6 Array Implementation  A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... An array of integers to implement a queue of integers 48 6 We don't care what's in this part of the array.

7 Array Implementation  The easiest implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... 486 size 3 first 0 last 2

8 A Dequeue Operation  When an element leaves the queue, size is decremented, and first changes, too. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... 486 size 2 first 1 last 2

9 An Enqueue Operation  When an element enters the queue, size is incremented, and last changes, too. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... 2 86 size 3 first 1 last 3

10 At the End of the Array  There is special behavior at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 216 size 3 first 3 last 5

11 At the End of the Array  The new element goes at the front of the array (if that spot isn’t already used): [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 216 size 4 first 3 last 0 4

12 Array Implementation  Easy to implement  But it has a limited capacity with a fixed array  Or you must use a dynamic array for an unbounded capacity  Special behavior is needed when the rear reaches the end of the array. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... 486 size 3 first 0 last 2

13 Linked List Implementation 10 15 7 null 13  A queue can also be implemented with a linked list with both a head and a tail pointer. head_ptr tail_ptr

14 Linked List Implementation 10 15 7 null 13  Which end do you think is the front of the queue? Why? head_ptr tail_ptr

15 Linked List Implementation 10 15 7 null head_ptr 13  The head_ptr points to the front of the list.  Because it is harder to remove items from the tail of the list. tail_ptr Front Rear

16  Like stacks, queues have many applications.  Items enter a queue at the rear and leave a queue at the front.  Queues can be implemented using an array or using a linked list. Summary

17 LINKED QUEUE Linkq( Info,link,front,rear,avail.item) 1. If avail=null then write overflow and exit 2. Set new := avail and avail := link [avail] 3. Set info[new]=item and link[new]=null 4. If (front=null) then front = rear = new 5. Else set link [rear] = new and rear = new 6. Exit

18 Deleting linkq_del(info,link,rear,avail,item) 1. If ( front = null ) then underflow and exit 2. Set temp=front 3. Item=info[temp] 4. Front=link[temp] 5. Link[temp]=avail and avail = temp 6. Exit.

19 DEQUES: Double-ended queue  It is a linear list in which elements are added or removed at either end but not from the middle.  Its is maintained as a circular array queue by default  Two variation are : Input restricted and Output restricted.

20 Priority Queues  It’s a collection of elements such that each elements has been assigned a priority and such that elements will be inserted and deleted by following rules:  1. An element of higher priority is processed before any element of lower priority.  2. Two elements of same order are processed according to their arrival


Download ppt "Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY."

Similar presentations


Ads by Google