Professional Documents
Culture Documents
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;
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***********************