DS Experiment No 04
DS Experiment No 04
Input Specification :
elements for Queue
Output Specification :
execution of fundamental operations on queue.
Theory :
A Queue is an ordered list in which all insertion takes place at one end and all
deletions take place at the opposite end. Since the first element removed is
the first element inserted, queues are also known as First In First Out (FIFO)
lists. The end at which insertions are taken place is called 'rear and the end at
which deletions take place is called 'front’ .
In a standard queue data structure re-buffering problem occurs for each
dequeue operation. That means, the queue tells it is full even though there are
empty locations. This problem is solved by joining the front and rear ends of
the queue to make the queue as a circular queue. When rear ==MaxSize-1 the
next element is entered at queue [0] in case that is empty .
Queues are frequently used in computer programming, and one example is the
creation of a job queue by an Operating System. If the OS does not use
priorities, the jobs a processed in the order they enter the system .
Alogorithm :
Step 1: Include all the header files which are used in the program and
constant Max with special value.
Step 2: declare all the user defined functions which one used in
queue implementation
Step 4: Define two integer variables front and rear and initilize both
with -1
Insertq ();
Steps 3 : if it is NOT fall then increment rear by one & and insert the
value.
Step 4: END
Printq () ;
Step 4: display value and Increment ''1' value by one ( i++) ,repeat
the same until value reach to rear (i<=rear )
Step 5 : end
PROFRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define mxsize 5
struct queue
{
int front,rear;
int arr[mxsize];
};
typedef struct queue QUE;
void initq(QUE *q)
{
q->front = 0;
q->rear = -1;
}
int isfullq(QUE q)
{
if(q.rear == mxsize-1)
return 1;
else
return 0;
}
int isemptyq(QUE q)
{
if(q.rear == -1) // if(q.rear<q.front)
return 1;
else
return 0;
}
void insertq(QUE *q)
{
int val;
if(isfullq(*q))
printf("No space to add element in queue.....Cannot add.\n");
else
{
printf("Enter value to be added : ");
scanf("%d",&val);
q->rear = q->rear + 1;
q->arr[q->rear] = val;
}
}
void removeq(QUE *q)
{
if(isemptyq(*q))
printf("Queue is Empty.....Cannot remove.\n");
else
{
printf("%d is removed from queue.\n",q->arr[q->front++]);
if(q->front==q->rear+1)
{
q->front = 0;
q->rear = -1;
}
}
}
void printdq(QUE q)
{
int i;
if(isemptyq(q))
printf("Queue is Empty.....\n");
else
{
printf("Elements of queue are.....\n");
for(i=q.front;i<=q.rear;i++)
printf("%d\t",q.arr[i]);
printf("\n");
}
}
int peekq(QUE q)
{
return q.arr[q.front];
}
#define mxsize 5
void desesort(QUE *q)
{
int i,j,temp;
for(i=0;i<q->rear;i++)
for(j=i+1;j<=q->rear;j++)
if(q->arr[i]<q->arr[j])
{
temp = q->arr[i];
q->arr[i] = q->arr[j];
q->arr[j] = temp;
}
}
void main()
{
QUE q;
int x,ch=1;
clrscr();
initq(&q);
printf("\n M E N U for operations on Queue......\n");
printf("1. Add element in an queue \n");
printf("2. Delete element from an queue \n");
printf("3. Print queue \n");
printf("4. Peek element\n");
printf("5. Quit\n");
while(ch)
{
printf("\nEnter your choice of operation : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : insertq(&q);
desesort(&q);
if(!isemptyq(q))
printdq(q);
break;
case 2 : removeq(&q);
if(!isemptyq(q))
printdq(q);
break;
case 3 : printdq(q);
break;
case 4 : if(!isemptyq(q))
{
x = peekq(q);
printf("%d is at the front of an queue.\n",x);
}
else
printf("Cannot peek as Queue is Empty.....\n");
break;
case 5 : exit(1);
default: printf("ERROR ! Entered wrong choice.....Try
again.....\n");
}
}
getch();
}
OUTPUT :