33
“ A queue is defined as a special type of data structure where elements are inserted from one end and elements are deleted from the other end”. The end from where the elements are inserted is called rear end”. The end from where the elements are deleted is called front end”. “queue is also called as First In First Out (FIFO) data structure.” QUEUES Kiran Sir Naresh IT [email protected] http://[email protected]

6 Queues

Embed Size (px)

DESCRIPTION

DSA

Citation preview

A queue is defined as a special type of data structure where elements are inserted from one end and elements are deleted from the other end.

The end from where the elements are inserted is called rear end. The end from where the elements are deleted is called front end. queue is also called as First In First Out (FIFO) data structure. QUEUES Kiran SirNaresh [email protected]://[email protected] Example : empty queuef=0,r=-1 01234

queue with elements 0 1 23 4 fr 102030 Kiran SirNaresh [email protected]://[email protected] Different types of queues:- Queue (ordinary queue) Circular queue Double ended queue

Priority queue Kiran SirNaresh [email protected]://[email protected] Queues(ordinary queues):

Insert at therear end: Before insert : 0123 4 fr After insert : 012 3 4 fr r = r + 1 q[r] = item. 10203040 1020304050 Kiran SirNaresh [email protected]://[email protected] // c function to insert an item

void insert_rear () { if ( r == QUEUE_SIZE 1 ) { printf (queue overflow); return; } r = r+1;q[r] = item; } Kiran SirNaresh [email protected]://[email protected] Queues(ordinary queues):

delete at thefront end: Before delete : 0123 4 fr After delete : 012 3 4 fr printf ( the element deleted is %d, q[f++]); 10203040 20304050 Kiran SirNaresh [email protected]://[email protected] // c function to delete an item

Void delete_front () { if ( f > r) { printf (queue underflow); return; } printf ( the element deleted is %d, q[f++]); if ( f > r ) f = 0, r = -1 ; } Kiran SirNaresh [email protected]://[email protected] Display queue contents: 01 2 3 4 fr The contents of the queue is always displayed from front to rear. for ( i=f; ir) { printf (queue is empty); return; }

for (i=f; i r ,Then the queue is underflow. 102030 1020 Kiran SirNaresh [email protected]://[email protected] //C function to delete an item from the rear end. void delete_rear() { if (f > r) { printf(Queue Underflow); return; } printf(The element deleted is %d, q[r--] ); if ( f > r) { f = 0; r = -1; } } Kiran SirNaresh [email protected]://[email protected] Disadvantages of ordinary queues: 0 1 2 34 f r The above situation will arise when 5 elements are inserted & then deleting first two items. Now, if we try to insert an item we get the message Queue overflow. i.e., even if memory is available, we can not access these memory locations. 304050 Kiran SirNaresh [email protected]://[email protected] Circular Queues: In circular queue, the elements of a given queue can be stored efficiently in an array , so that end of the queue is followed by the front of the queue. The pictorial representationof a circular queue & its equivalent representation using an array is shown below. 0 1 23 4 f , r 10 Kiran SirNaresh [email protected]://[email protected] After inserting 20 and 30 01 2 34 fr After inserting 40 and 50 01 2 3 4 f r After deleting 10 and 20 0123 4 fr After inserting 60 0 123 4 r f 102030 1020304050 304050 60304050 Kiran SirNaresh [email protected]://[email protected] Operations on circular queue: *Insert rear *Delete front *Display Kiran SirNaresh [email protected]://[email protected] *To insert from the rear end: Step1 : Check forOverflow; if (count == Queue_size) { printf(Queue is full); return; } Step2 :Insert Item : This is achieved by incrementing r by 1 & theninserting as shown below, r = (r+1) % QUEUE _SIZE; Q[r] = item; Step 3: Update count : Count++; Kiran SirNaresh [email protected]://[email protected] //function to insert an item at the rear end. void insert_rear(int item, int q[],int r ,int count) { if (count == QUEUE_SIZE) { printf(Overflow of queue \n); return; } r = (r+1) % QUEUE_SIZE; Q[r] = item; count += 1; } Kiran SirNaresh [email protected]://[email protected] *TO delete from the front end : Step1 :Check for underflow : if (count == 0) { printf(queue is empty /n); return; } Step2 :Delete item: printf(The deleted elements is %d /n,q[f]); f = (f+1) % QUEUE_SIZE; Step 3: Update count: count--; Kiran SirNaresh [email protected]://[email protected] //function to delete an item from the front end of c queue. void delete_front(int q[] , int f , int count) { if(count == 0) { printf(underflow of queue /n); return ; } printf(the deleted elements is %d /n,q[f]); f = (f+1) % QUEUE_SIZE; count -= 1; } Kiran SirNaresh [email protected]://[email protected] *To display queue contents : step1 : check for underflow: if (count == 0) { printf(Queue is empty /n); } Step 2 : Display for ( i=1; i