C PROGRAMMINGDATA STRUCTURES

Queue program in c using array with menu

Queue program in c using array with menu

 A queue is a linear data structure where elements are inserted from one end and deleted from other end. Element are inserted for rare and deleted from front of the queue.

There are two types of operations performed on queues 
Enqueue                     Dequeue
#include <stdio.h>
#define MAX 10

int queue[MAX];
int front = -1, rear = -1;

void insert()
{
int num;
printf("n Enter the number to be inserted in the queue :n");
scanf("%d", &num);
if(rear == MAX-1)
printf("n OVERFLOW");
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}

int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf("n UNDERFLOW");
return -1;
}
else
{
val = queue[front];
front++;
if(front > rear)
front = rear = -1;
return val;
}
}

int peek()
{
if(front==-1 || front>rear)
{
printf("n QUEUE IS EMPTY");
return -1;
}
else
{
return queue[front];
}
}


void display()
{
int i;
if(front == -1 || front > rear)
printf("n QUEUE IS EMPTYn");
else
{
for(i = front;i <= rear;i++)
printf("%d t", queue[i]);
}
}


void main()
{
int option, val;
do
{
printf("n***** MAIN MENU *****");
printf("n 1. Insert an element");
printf("n 2. Delete an element");
printf("n 3. Peek");
printf("n 4. Display the queue");
printf("n 5. EXIT");
printf("n Enter your option : n");
scanf("%d", &option);
switch(option)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if (val != -1)
printf("n The number deleted is : %dn", val);
break;
case 3:
val = peek();
if (val != -1)
printf("n The first value in queue is : %dn", val);
break;
case 4:
    display();
break;
}
}while(option != 5);
getch();
}

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button