C program to implement queue using array

/*************** BY Marxtudor www.marxtudor.com/ ***************/

#include
#define MAX 5
void insert(int);
int del();
void display();
int queue[MAX];
int front=-1;
int rear=-1;

main()
{
int choice, num;
while(1)
{
printf("nEnter your choicen");
printf("1. Insertn");
printf("2. Deleten");
printf("3. Displayn");
printf("4. Exitn");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter a number to be inserted : ");
scanf("%d",&num);
insert(num);
break;
case 2:
num=del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Invalid choicen");
}
}
}

void insert(int element)
{

if(rear==MAX-1)
{
printf("nQueue is Fulln");
return;
}

if(front==-1)
{
front = 0;
}
rear=rear+1;
queue[rear]=element;
}

int del()
{
int element;
if(front==-1 || front==rear+1)
{
printf("Queue is Emptyn");
return;
}
element = queue[front];
front = front + 1;
printf("%d has been deletedn", element);
return element;
}

void display()
{
if(front==-1 || front==rear+1)
{
printf("Queue is Emptyn");
return;
}

int i;
for(i=front; i<=rear; i++)
{
printf("%d ",queue[i]);
}
}
Previous Post
Queue using array program blueprint
Next Post
Circular queue using array

Related Posts

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

alert('dsf'); console.log("dsdsdsd");