You are on page 1of 32

Queue

Kashiram Pokharel
Syllabus:
Queue: Introduction:
• A queue is an order collection of elements in which deletion can take
place only at one end, called front, and insertions can take place
only at the other end, called rear.
• The first element inserted into the queue is the first element to be
removed. For this reason a queue is sometimes called a FIFO (first-in
first-out) list as opposed to the stack, which is a LIFO (last-in first-out).
• Applications of queue:
• Task waiting for the printing
• Time sharing system for use of CPU
• For access to disk storage
• Task scheduling in operating system
Show that queue As an ADT
• Primary queue operations:
• MakeEmpty(q): To make q as an empty queue
• Enqueue(q, x): To insert an item x at the rear of the queue, this is
also called by names add, insert.
• Dequeue(q): To delete an item from the front of the queue q. this is
also known as Delete, Remove.
• IsFull(q): To check whether the queue q is full.
• IsEmpty(q): To check whether the queue q is empty
• Traverse (q): To read entire queue that is display the content of the
queue.
Queue Implementation :-
• array implementation
• linked list implementation
Array implementation
• A queue top two pointers front and rear pointing to the front and rear
element of the queue.
• When there is no place for adding elements in queue, then this is
called queue overflow.
• When there is no element for deleting from queue, then value of
front and rarer will be -1 or front will be greater than rarer.
an algorithm for creating an empty queue:
# define MAX 10 /* size of the queue items*/
struct queue
{
int front;
int rear;
int items[MAX];
};
typedef struct queue qt;
void makeEmpty (qt *q)
{
q->front=0;
q->rear=-1;
}
An algorithm for check empty condition in
queue:
int IsEmpty()
{
if(rear < front)
return 1;
else
return 0;
}
An algorithm for check overflow condition
int IsFull()
{
if(rear==MAX-1)
return 1;
else
return 0;
}
An algorithm for Enqueue operation
• Initially , Set front=0 and rear=-1
1. Check queue overflow condition
if rear==MAX-1
print “queue overflow” and return
2. Otherwise Increase rear by one
set rear=rear+1
3. Insert value at new rear position.
queue[rear]=item
4. end
An algorithm for Dequeue operation:
Check queue underflow condition.
if(rear < front)
{
printf(“queue is Underflow”);
exit(1);
}
print the element to be deleted
{
Deleted element is (items[front]);
Increase front by one
front++;
}
end
Traverse operation.
• Check queue underflow condition.
if(q->rear<q->front)
print Queue is empty and return;
• otherwise display item from front index to rear index.
for(i=q->front;i<=q->rear;i++)
{
printf("%d\t",q->item[i]);
}
Drawback of linear queue:

• If rear is at the last position of array and front is not at the zeroth
position then cannot add any element in the queue because the rear
is at the n-1 position.
• There are two spaces for adding the elements in queue. But we
cannot add any element in queue because rear is at the last position
of array. One way is to shift all the elements of array to left and
change the position of front and rear but it is not practically good
approach.
• To overcome this type of problem we use the concept of circular
queue.
Circular queue:
• A circular queue is one in which the insertion
of a new element is done at very first location
of the queue if the last location of the queue is
full.
• A circular queue overcomes the problem of unutilized space in linear
queue implementation as array.
• In circular queue we sacrifice one element of the array thus to insert n
elements in a circular queue we need an array of size n+1.(or we can
insert one less than the size of the array in circular queue).
Declaration and Initialization of a Circular
Queue:
Declaration of a Circular Queue:
# define MAX 50 /* size of the circular queue items*/
struct cqueue
{
int front;
int rear;
int items[MAX];
};
typedef struct cqueue cq;
Initialization operation of circular queue
void makeEmpty(cq *q)
{
q->rear=MAX-1;
q->front=MAX-1;
}
IsEmpty and isfull operation of Circular queue
1. IsEmpty operation of circular queue:
if (rear==front) [checking empty condition]
return 1;
else
return 0;

IsFull operation of circular queue:


if(q->front==(q->rear+1)%MAX) //(front==0 &&rear==max-1)||(front==rear+1)
return 1;
else
return 0;
.
Enqueue operation of circular queue using
array.
This algorithm is assume that rear and front are initially set to MAZSIZE-1.
1. Check queue overflow condition
if (front==(rear+1)%MAX)
print Queue is full and exit
2. Increase rear by one.
3. Set rear=(rear+1)%MAX; [increment rear by 1]
4. Insert data at new rear position.
cqueue[rear]=item;
5. end
Dequeue operation of circular queue:
This algorithm is assume that rear and front are initially set to MAZSIZE-1.
1. check for underflow condition
if (rear==front)
print Queue is empty and exit
2. Increase front pointer
front=(front+1)%MAX; [increment front by 1]
3. set deleted value to a variable.
item=cqueue[front];
4. Display the element
Print item;
5. end.
Traverse operation of circular queue:
1. Check underflow condition
if(rear==front)
printf("Queue is empty\n");
2. Print items of queue
for(i=(q->front+1)%SIZE ; i!=q->rear; i=(i+1)%SIZE)
{
printf("%d\t",q->item[i]);
}
printf("%d\t",q->item[q->rear]);
}}
Priority queue
• Priority Queue is an extension of queue with following properties.
• Every item has a priority associated with it.
• An element with high priority is deleted before an element with low priority.
• If two elements have the same priority, they are served according to their order in in
which they were added to the queue.
• In stack and queue, the elements orders are based on the ordered in which
they have been inserted, but a queue in which it is possible to insert or remove
an item from any position depending on some priority is called priority queue.
• A priority queue is a collection of elements such that each element has been
assigned a priority and the order in which elements are deleted and processed
according to their higher priority i.e.
• An element of higher priority is processed before any element of lower priority.
• If two elements has same priority then they are processed according to the order in which
they were added to the queue.
Applications
• Patients in an emergency room
• Operating system scheduler
• Routing
• A* search
• Simulation
• The best application of priority queue is observed in CPU scheduling, in
which the jobs which have higher priority are processed first.
• If the priority of two jobs is same this jobs are processed according to their
position in queue.
• A short job is given higher priority over the longer one.
Type of Priority queue:
• There are two kinds of priority queues:
• An ascending priority queue
• An ascending priority queue is a collection of items into which items can be
inserted arbitrarily and from which only the smallest item can be removed.
• Descending priority queue.
• A descending priority queue is similar to ascending priority queue but allows
deletion of only the largest item.
The priority queue as ADT
• A ascending priority queue of elements of type T is a finite sequence
of elements of T together with the operations:
• MakeEmpty(p): Create an empty priority queue p
• Empty(p): Determine if the priority queue p is empty or not
• Insert(p,x): Add element x on the priority queue p
• DeleteMin(p): If the priority queue p is not empty, remove the
minimum element of the queue and return it.
• FindMin(p): Retrieve the minimum element of the priority queue p
Array implementation of priority queue
1. Unordered array implementation
• To insert an item, insert it at the rear end of the queue.
• To delete an item, find the position of the minimum element and
• Either mark it as deleted (lazy deletion) or
• shift all elements past the deleted element by on position and then
decrement rear
Declaration of priority queue:
Queue data type of priority queue is the same as the non priority
queue.
struct pqueue
{
int front;
int rear;
int items[MAXQUEUE];
};
struct pqueue *pq;
Insert function:
1. Check overflow conditions
if(rear==MAX-1)
printf("Queue is Overflow");
2. Increase rear by 1
rear++;
3. Enter data to be inserted
scanf("%d",&x);
cqueue[rear]=x;
4. End
Delete Function 2. Find the lowest priority value
{
x=q->items[q->front];
1. check queue underflow for(i=q->front+1; i<=q->rear; i++)
condition. {
void delet( struct pqueue *q) if(q->items[i] < x)
{ {
x=q->items[i];
int i, temp=0, x; temp=i;
if(q->rear < q->front) }
{ }
printf("Queue is emptyn"); 3. Shift one position left from minimum value
position to rear position.
return 0;
for(i=temp;i< q->rear;i++)
} {
else q->items[i]=q->items[i+1];
}
4. Decrease the rear position by one
q->rear--;
Printf(" Deleted item is %d ",x);
}}
Traverse
1. Check queue underflow condition.
void display(pq *q)
{
int i;
if(q->rear < q->front)
printf("Queue is emptyn");
else
{
2, Display item from front to rear
printf("Items of queue are:n");
for(i=q->front; i<=q->rear;i++)
{
printf("%dt", q->item[i]);
}
}
}
Ordered array implementation
• In this implementation, the front is the
position of the smallest element and
the rear is the position of the largest
element.
• To insert an element, locate the proper
position of the new element and shift
succeeding elements by one position.
• To delete the minimum element, delete
element at the front position and shift
all elements one position towards front
and decrement rear.
3. Find the proper position for insertion.
for(i=q->front; i<=q->rear; i++)
Insert function: {
if(x < q->item[i])
{
1. Check queue overflow condition.
break;
void insert(struct pqueue *q)
}}
{
4. Move all the element one position right from
int x,i,j; the obtained position
if(q->rear == MAX -1) for(j=q->rear;j>=i;j--)
printf("n Queue is fulln"); q->item[j+1]= q->item[j];
else 5. Increase rear index by one
{ q->rear = q->rear+1;
2. Enter data to be inserted 6. Insert a value at its proper place
printf ("nEnter data to be insertedt"); q->item[i] = x;
scanf("%d",&x); }
}
Delete function:

1. Check queue underflow condition 4. Shift element one position left from
void delet(struct pqueue *q) deleted element to rear index.
{ for(i=q->front;i<q->rear;i++)
int i, x; {
if(q->rear < q->front) q->item[i]=q->item[i+1];
{ }
printf("nQueue is emptyn"); 5. Decrease rear index by one
} q->rear--;
else printf("n Deleted item is %d", x);
{ }}
2. Select item to be delete
x=q->item[q->front];
********************END***********************

You might also like