2 2queue
2 2queue
6-2
Conceptual View of a Queue
Removing an element
Element is removed
from the front of the
queue
6-3
Operations on Queue
int f=-1,r=-1;
Insertion
void insert(int item ) 0 1 2 3 4
{
if(r==SIZE-1)
{ Initially
r=-1
f=-1
Here SIZE = 5
Insertion 0 1 2 3 4
100
void insert(int item )
r
{ f
if(r==SIZE-1)
{ printf("Queue is full\n");
else {
if (f == -1) {
f = 0; }
r++;
q[r] = item;
printf("Inserted %d\n", item); } }
Insertion
void insert(int item )
0 1 2 3 4
{
100
if(r==SIZE-1) f=0 r=1
{ printf("Queue is full\n");
else {
if (f == -1) {{
f = 0; }
r++;
q[r] = item;
printf("Inserted %d\n", item); } }
Insertion
Insert(200);
0 1 2 3 4
100 200
r =2
f=0
Insertion
Insert(300);
0 1 2 3 4
100 200 300
r =3
f=0
Insertion
Insert(400);
0 1 2 3 4
100 200 300 400
r =4
f=0
Insertion
Insert(500);
0 1 2 3 4
100 200 300 400 500
r =5
f=0
Insertion
Insert(600);
0 1 2 3 4
100 200 300 400 500
r =5
f=0
QUEUE IS FULL
void dequeue() {
if (f == -1 || f > r) {
printf("Queue is empty\n");
}
Deletion
else {
printf("Deleted %d\n", q[f]);
f++;
}
}
Deletion
0 1 2 3 4
100 200 300 400 500
r =5
f=0
else
{
printf("Deleted %d\n", q[f]);
Deletion
0 1 2 3 4
200 300 400 500
f=1 r =5
else
{
printf("Deleted %d\n", q[f]);
++f;
}
Deletion
0 1 2 3 4
300 400 500
f=2 r =5
Deletion();
Display
Void display()
{
if(f==-1)
printf("\nQUEUE IS EMPTY“);
else
{ printf("\n Elements in the Queue Are:: “);
for ( int i=f;i<=r;i++)
printf(“%d”,q[i]);
}}
Display
else
{
printf("\n Elements in the Queue Are:: “);
for ( int i=f;i<=r;i++)
printf(“%d”,q[i]);
}
Display
0 1 2 3 4
300 400 500
f=2 r =5
i=f;i<=r;i++) i=2
%d”,q[i]);
300
Display
0 1 2 3 4
300 400 500
f=2 r =5
for ( int i=f;i<=r;i++) i=3
printf(“%d”,q[i]);
300 400
Display
0 1 2 3 4
300 400 500
f=2 r =5
for ( int i=f;i<=r;i++) i=4
printf(“%d”,q[i]);
Restricted queue
Circular Queue
In a normal queue when we go on deleting the
elements the deleted locations become
unused, this wastage of memory space can be
avoided by using circular queue.
Circular Queue
Incircular queue is same as normal queue we
reset f and r when the queue underflow
occurs.
Basic Conditions:
ADD
DELETE
CREATE
int arr[n],R=0,F=0;
Add Operation in Circular Queue
Void enqueue(int item)
{ R=(R+1)mod n;
if(F==R)
{ printf("Queue is full");
if(R==0)
R=n-1;
else
R=R-1;
return;}
else {
arr[R]=item;
return; }
Delete Operation in Circular Queue
Void dequeue( )
{ if(F==R)
{
printf("Queue is empty"); return -1;
}
else
{
F=(F+1)%n;
printf("Number Deleted = %d",arr[F]);
}
Priority Queue
Priority Queue is the special case of Queue.
In priority queue deletion and insertion base on
Priority P1 P2 pi Pn
Priority Queue operations
Enqueue(): Adding a new item to the Queue
Data Structure, in other words Enqueue new
item to Queue If Queue is full, then it is said to
be in an overflow condition.
Dequeue(): Delete a item from a Queue,
queue.
This removal creates an empty slot, which will