EX.NO: 4 ARRAY IMPLEMENTATION OF QUEUE
AIM:
To write a c program to implement the queue using array
ALGORITHM:
* Start the program.
* Define the queue size and declare the necessary function.
* Declare a queue fixed size and also declare its front and rear pointer.
* In main( ),display the options by calling diplay( ).
* Initialize the front and rear pointer to -1.
* Depending upon the choice call the appropriate function using switch case.
* Stop the program.
ENQUEUE:
* Increment the rear pointer.
* If front = qsize , then initialize front = 0.
* If front=1 and rear less than qsize or rear=front,then queue is full.
* Insert the data into the queue in the position point d by the car.
DEQUEUE:
* Increment the front pointer.
* If front=qsize, then initialize front=0.
* If front=rear then the queue is empty else return the data present in the
front position.
PRINT:
* If the queue is empty then display “queue empty”.
* Else display all the data present in the queue is from front to rear.
* Check for the status of the queue is full, then display “queue is full”.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define QSIZE 5
#define FULL 0
#define EMPTY 1
#define SOMEDATA 2
void enqueue(int queue[],int data);
int dequeue(int queue[]);
void display();
void print(int queue[]);
int front,rear,qst,q[QSIZE];
void main()
{
int c,d,e;
clrscr();
display();
rear=-1;
front=-1;
qst=EMPTY;
while(1)
{
printf("\nEnter the choice");
scanf("%d",&c);
switch(c)
{
case 1:
if(qst==FULL)
printf("\nQuene is full");
else
{
printf("Enter the element");
scanf("%d",&d);
enqueue(q,d);
print(q);
}
break;
case 2:
if(qst==EMPTY)
printf("\nQuene is empty");
else
{
printf("The deleted value is %d",dequeue(q));
print(q);
}
break;
case 3:
print(q);
break;
case 4:
printf("\nEnd the program");
exit(0);
break;
default:
printf("\nChoice is wrong");
}
}
}
void display()
{
printf("\n ARRAY IMPLEMENTATION OF QUEUE");
printf("\n\t 1.Enqueue");
printf("\n\t 2.Dequeue");
printf("\n\t 3.display");
printf("\n\t 4.Exit");
}
void enqueue(int queue[],int data)
{
qst=SOMEDATA;
rear++;
if(rear==QSIZE)
rear=0;
if(front==-1&&rear==(QSIZE-1)||rear==front)
qst=FULL;
queue[rear]=data;
}
int dequeue(int queue[])
{
front++;
if(front==QSIZE)
front=0;
if(front==rear)
qst==EMPTY;
else
qst=SOMEDATA;
return(queue[front]);
}
void print(int queue[])
{
int i;
if(qst==EMPTY)
printf("\nQueue is empty");
else
{
i=front;
printf("\n Queue contain..FRONT-->");
do
{
printf("%d-->",queue[i=(i+1%QSIZE)]);
}
while(i!=rear);
printf("REAR\n");
if(qst==FULL)
printf(" Queue is full");
}
}