Write a C Program to Implementation of Linear Queue operations like enQueue(), deQueue() and displayQ() using structure pointers
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct queue{
int value[MAX];
int front;
int rear;
};
void enQueue(int x,struct queue *q) {
if(q->rear == MAX-1){
printf("Queue is full");
return;
}
if(q->rear == -1 && q->front == -1){
q->front = q->rear = 0;
}
else
q->rear++;
q->value[q->rear] = x;
}
int deQueue(struct queue *q) {
int x;
if(q->front == -1 || q->front > q->rear) {
printf(" Queue is empty");
return -1;
}
x = q->value[q->front++];
return x;
}
void displayQ(struct queue *q) {
int i;
for(i=q->front; i<= q->rear; i++)
printf("%d ", q->value[i]);
}
int main() {
int ch, p;
struct queue q;
q.rear=q.front=-1;
while(1){
printf("\n 1-> INSERT");
printf("\n 2->DELETE ");
printf("\n 3->DISPLAY");
printf("\n 4->EXIT ");
scanf("%d",&ch);
switch(ch){
case 1: printf("\n Enter an element to insert: ");
scanf("%d",&p);
enQueue(p,&q);
break;
case 2: p = deQueue(&q);
if((q.front == -1 || q.front > q.rear) && p==-1)
break;
else
printf("\n The deleted element is %d",p);
break;
case 3: displayQ(&q);
break;
case 4: exit(0);
}
}
}
OUTPUT :
Post a Comment
0 Comments