Written by Sai Sandeep Thota on January 15, 2013 in C > Programming

C Program to Implement Queue using Arrays

GET ALERTS:

Get our Latest updates delivered to your mailbox!

A Queue can be defined as a list of items/elements in which additions can be done from two sides of the list know as front and rear, unlike stack in which additions can be done only from one direction i.e. Top. Queue follows the method of First in First out [FIFO].

C Program to implement Queue

#include<conio.h>
#include<stdio.h>
#define max 5
int queue[max],front=0,rear=0;
int menu();
void enqueue();
void dequeue();
void display();
void main()
{
int ch;
clrscr();
printf("\nProgrammingUnit.com - Free Source Codes\n");
printf("\nQueues using Arrays\n");
do
{
	ch=menu();
	switch(ch)
	{
	case 1: enqueue();
	break;
	case 2: dequeue();
	break;
	case 3: display();
	break;
	case 4: exit();
	break;
	default:printf("\n Please enter a valid choice!!");
	}
}while(1);
}

int menu()
	{
	int ch;
	printf("\n1.ENQUEUE \n2.DEQUEUE \n3.DISPLAY \n4.EXIT");
	printf("\nEnter your Choice:");
	scanf("%d",&ch);
	return ch;
	}

void enqueue()
{
	int element;
	if(rear==max)
	{
		printf("\nOverflow!!");
	}
	else
	{
		printf("\nEnter Element:");
		scanf("%d",&element);
		queue[rear++]=element;
		printf("\n %d Enqueued at %d",element,rear);
	}

}

void dequeue()
{
	if(rear==front)
	{
		printf("\nUnderflow!!");
	}
	else
	{
		front++;
		printf("\nElement is Dequeued from %d",front);
	}
}
void display()
{
	int i;
	if(front==rear)
	{
		printf("\nQueue is Empty!!!");
	}
	else
	{
		printf(" \n");
		for(i=front;i<max;i++)
		{
			printf(" | %d ",queue[i]);
		}
			printf("|");
	}
}

Output:
queue-using-arrays

queue-using-arrays1

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: