Sie sind auf Seite 1von 33

QUEUES

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.
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

Example :
empty queue
f=0,r=-1
0

queue with elements


0
1
10

20

30

Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in


http://kiransrinivas@wordpress.com

Different types of queues:

Queue (ordinary queue)

Circular queue

Double ended queue

Priority queue

Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in


http://kiransrinivas@wordpress.com

Queues(ordinary queues):
Insert at the rear end:
Before insert :
0
10

f
After insert :
0
10

20

30

40

r
1

20

30

40

50

r=r+1
q[r] = item.
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

// 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 Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

Queues(ordinary queues):
delete at the front end:
Before delete :
0
10

20

f
After delete :
0

30

40

r
1

20

30

40

50

printf ( the element deleted is %d, q[f++]);


Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

// 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 Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

Display queue contents:


0

1
20

2
30

3
40

4
50

The contents of the queue is always displayed from


front to rear.
for ( i=f; i<=r; i++ )
printf (%d\n, q[i]);

Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in


http://kiransrinivas@wordpress.com

// c function to display the contents of queue


void display ()
{
int i;
if(f>r)
{
printf (queue is empty);
return;
}
for (i=f; i<=r; i++)
{
printf (%d, q[i]);
}
}

Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in


http://kiransrinivas@wordpress.com

// complete c program to implement queue operations.


#include<stdio.h>
#include<process.h>
#define QUEUE_SIZE 5

int choice, item, f, r, q[20];


// include the above 3 functions
Void main()
{
f = 0;
r = -1;
for ( ; ; )
{
printf( 1. Insert 2. delete 3. display 4. exit);
printf( Enter the choice);
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
Scanf (%d,
&choice);
http://kiransrinivas@wordpress.com

Scanf(%d,&choice);
Switch(choice)
{
Case 1:
Printf(enter the item to be inserted );
Scanf(%d, &item);
insert_rear();
break;
Case 2:
delete_front();
break;
Case 3:
display();
break;
Default:
exit(0);
}
}
Kiran}Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

Double ended queue ( dequeue ) :


A dequeue is a special type of data structure in which
insertions are done from both ends and deletions are done at
both ends.
Operations performed on Queues :

* Insert on item from front end.


* Insert an item from rear end.
* Delete an item from rear end.
* Delete an item from front end.
* Display the contents of queue.
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

Insert at the front end:


Case1 :
Queue Empty : Here, an item can be inserted at
the front end first by incrementing r by 1 and then
insert an item.
Before insert
0
1
2
3
4

r=-1 f=0
0

After insert
1
2

10

f,r

if ( f == 0 && r == -1 )
{
q[++r] = item;
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
return ;
}
http://kiransrinivas@wordpress.com

Case 2: Some items are deleted :


Assume,
0
1
2
10

f
0

20

30

r
After deleting two elements
1
2
3
4
30

fr
Now, insert at front end,
0
1

20

30

f
r
if ( f != 0 )
{
q[--f] = item;

return;

Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in


http://kiransrinivas@wordpress.com

Case 3 : Some items are inserted (not deleted) :


0

10

20

30

Now, it is not possible to insert an item

printf (front insertion is not possible);

Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in


http://kiransrinivas@wordpress.com

// function to insert an item at the front end.


void insert_front ()
{
if ( f==0 && r==-1 )
// case 1
{
Q[++r] = item;
return;
}
if ( f != 0 )
// case 2
{
q[--f] = item;
return;
}
Printf( front insertion is not possible); //case 3.
}
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

Delete from the rear end :


Before deleting
0
1
2
10

f
0
10

20

30

r
After delete
1
2

20

Here if we keep on deleting we will arise condition that


f>r,
Then the queue is underflow.
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

//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 Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

Disadvantages of ordinary queues:


0

30

40

4
50

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.
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

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 representation of a circular queue &
its equivalent representation using an array is shown below.
0

10

f,r
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

After inserting 20 and 30


0
1
10

20

30

f
After inserting 40 and 50
0
1
10

20

f
After deleting 10 and 20
0
1

30

40

50

r
2

30

40

50

f
After inserting 60
0
60

2
30

Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in


http://kiransrinivas@wordpress.com

3
40

4
50

Operations on circular queue:

*Insert rear
*Delete front
*Display

Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in


http://kiransrinivas@wordpress.com

*To insert from the rear end :


Step1 :
Check for Overflow;
if (count == Queue_size)
{
printf(Queue is full);
return;
}
Step2 :
Insert Item :
This is achieved by incrementing r by 1 & then
inserting as shown below,
r = (r+1) % QUEUE _SIZE;
Q[r] = item;
Step 3:
Update count :
Count++;
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

//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 Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

*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 Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

//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 Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

*To display queue contents :


step1 : check for underflow:
if (count == 0)
{
printf(Queue is empty /n);
}
Step 2 : Display
for ( i=1; i<=count; i++)
{
printf(%d /n, q[f]);
f = (f+1) % QUEUE_SIZE;
}
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

//Function to display the contents of circular queue


void display (int q[], intf, int count )
{
int i;
if (count == 0)
{
printf(Q is empty /n);
return;
}
printf (contents of queue is /n);
for (i = 1; i <= count; i++)
{
printf(%d /n, q[f]);
f = (f+1) % QUEUE_SIZE;
}
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
}
http://kiransrinivas@wordpress.com

//C program to implement circular queue.


#include<stdio.h>
#include<process.h>
#define QUEUE_SIZE 5
/*include all the above three functions
void main()
{
int choice ,item, f, r , count ,q[20];
f = 0;
r = -1;
count = 0;
/*queue is empty*/
for ( ; ; )
{
printf(1.Insert 2. Delete 3.Display 4. Exit);
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
printf(enter
the choice);
http://kiransrinivas@wordpress.com

Scanf(%d, &choice);
Switch(choice)
{
Case1 : printf( enter the item to be inserted);
Scanf(%d, &item);
insert_rear(item,q, r, count);
break;
Case2 : delete_front(q,f,count);
break;
Case3 : display(q,f,count);
break;
default : exit(0);
}
}
}

Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in


http://kiransrinivas@wordpress.com

Priority Queues:
The priority queue is a special type of data structure in
which items can be inserted or deleted based on the priority.
Always an element with highest priority is processed
before processing any of the lower priority elements.

If the elements in the queue are of same priority, then


the elements, which is inserted first into the queue is processed.
Priority queues are used in job scheduling algorithms in
the design of operating system where the jobs with higher
priorities have to be processed first.
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
http://kiransrinivas@wordpress.com

The priority queues are classified into groups,


Ascending priority queue :
while deleting an element from the queue, only
the smallest element is removed first.

Descending priority queue :


while deleting an element from the queue, only
the largest element is deleted first.

Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in


http://kiransrinivas@wordpress.com

// Insert an item at the correct place in priority queue.


Void insert_item(int item, int q[],int r)
{
int j;
if ( r == QUEUE_SIZE-1 )
{
printf(queue is full);
return;
}
j = r;
while ( j >= 0 && item < q[j] )
{
Q[j+1] = q[j];
j--;
}
Q[j+1] = item;
r = r+1;
Kiran Sir Naresh IT vkiransrinivas@yahoo.co.in
}
http://kiransrinivas@wordpress.com

Das könnte Ihnen auch gefallen