C program to implement Linear Queue using array
//C program to implement Linear Queue using array
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
struct Queue{
int a[MAX];
int front;
int rear;
}q;
void enqueue();
void dequeue();
void display();
int isFull();
int isEmpty();
int main()
{
int ch;
q.front=0; //Initially Queue is Empty
q.rear=-1;
printf("\n\t WELCOME TO QUEUE OPERATIONS\n");
printf("\n\t Press 1 to ENQUEUE\nPress 2 to DEQUEUE\nPress 3 to
DISPLAY\nPress 4 to EXIT\n");
while(1){
printf("\n\t Enter your choice:");
scanf("%d",&ch);
switch(ch){
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("INVALID CHOICE CODE\n");
}
}
return 0;
}
int isFull(){ //Checks whether queue is full or not
if(q.rear==MAX-1)
return 1;
else
return 0;
}
int isEmpty(){ //Checks whether queue is empty or not
if(q.front>q.rear)
return 1;
else
return 0;
}
void enqueue(){
/*Increment Rear By 1 and Assign it with value num*/
int num;
if(!isFull()){
printf("Enter any element : ");
scanf("%d",&num);
q.a[++q.rear]=num;
}
else{
printf("QUEUE IS FULL !!!\n");
}
}
void dequeue(){
/*Left Shift All the Elements*/
int i;
if(!isEmpty()){
printf("\n\t The element deleted is=%d", q.a[q.front]);
for(i=0;i<q.rear;i++) //Left shifting
{q.a[i]=q.a[i+1];}
q.rear--;
}
else{
printf("QUEUE IS EMPTY!!!\n");
}
}
void display(){
int i;
if(isEmpty())
printf("\n\t The linear queue is Empty.");
else
{
for(i=q.front;i<=q.rear;i++)
printf("%d \t ",q.a[i]);
}
}
No comments: