C Program to Implement a Queue using an Array

Rumman Ansari   2017-10-30   Student   Data structure > Queue Using Array   1544 Share

Problem Description

In this program we are trying to implement the queue using an array. where we want to insert some element in the list, also want to delete some elements from the queue, and want to view the list elements.

Problem Solution

1. Use three functions for three operations like insert, delete and display.

void addElementsInQueue(); // this is for insertion 
void displayElementsInQueue();  // this is for Display elements
void deleteElementsFromQueue();  // this is for deleteing the elements

2. Use switch statement to access these functions.

3. Exit.

Program/Source Code

Here is source code of the C Program to implement a queue using array. The C program is successfully compiled and run on a Linux system as well as windows system. The program output is also shown below.



/*  
Programming of Queue:
C Program to Implement a Queue using an Array

*/


#include<stdio.h>
#include<stdlib.h>
void addElementsInQueue();
void displayElementsInQueue();
void deleteElementsFromQueue();
#define MAX 10
int sel;
int queue[MAX];
int front = -1;
int rear  = -1;
void main()
{

int loopIteration = 1; 

do{

printf("\n.............Your Choice.............\n");
printf("Enter 1: for Insert New Element \n");
printf("Enter 2: for Insert display Element \n");
printf("Enter 3: for Insert Delete Element \n");
printf("Enter 4: for Insert Exit Element \n");
printf("............................\n");
scanf("%d",&sel);   
  switch(sel)
  {
	case 1:
	printf("Enter a New Element \n");
	addElementsInQueue();
	break;

	case 2: 
	printf("Display all Elements in Queue \n");
	displayElementsInQueue();
	break;

	case 3:
	printf("Delete Elements from Queue \n");
	deleteElementsFromQueue();
	break;
       
	case 4: 
	exit(1);

	default:
	printf("Entered Wrong Choice");
	break;

  }
printf(" \n Enter 0 to exit \n");
scanf("%d", &loopIteration);


}while(loopIteration);

}


void addElementsInQueue(){


	if(rear==MAX-1)
	{
	printf("Queue is Overflow");
	}
	else if(front== -1 && rear ==  -1){
        front = 0;
	rear  = 0;

        printf("Enter New Element");
        scanf("%d",&queue[rear]);
	}
	else
	{
	rear = rear + 1;
        printf("Enter New Element");
        scanf("%d",&queue[rear]);
	}

}



void displayElementsInQueue(){
	int i; 
	if(front== -1 && rear == -1)
	{
	printf("No Element is present in Queue");
	}

	for(i=front; i<=rear; i++)
	{
        printf("%d \t",queue[i]);	
	}

}



void deleteElementsFromQueue()
{

	if(front== -1 && rear == -1)
	{
	printf("Queue is Underflow");
	}
	else if(front==rear && front != -1 )
	{
	printf(" \n %d is deleted from Queue", queue[front]);
        front = -1;
	rear = -1;
        }
	else
	{
	printf("%d is deleted from Queue", queue[front]);
	front = front + 1 ;

	}


}




Description

Description for void addElementsInQueue() function


void addElementsInQueue(){


	if(rear==MAX-1)
	{
	printf("Queue is Overflow");
	}
	else if(front== -1 && rear ==  -1){
        front = 0;
	rear  = 0;

        printf("Enter New Element");
        scanf("%d",&queue[rear]);
	}
	else
	{
	rear = rear + 1;
        printf("Enter New Element");
        scanf("%d",&queue[rear]);
	}

}

if(rear==MAX-1)

This if condition is a checking for Overflow condition when the list is full with the elements

else if(front== -1 && rear ==  -1)

This else if condition checks wheather the list is empty or not, if it empty then insert the element with the rear 0 position (This is actually the first element in the queue).

   else
	{
	    rear = rear + 1;
        printf("Enter New Element");
        scanf("%d",&queue[rear]);
	}

In this else condition it will insert a element by incrementiong rear position 1 i.e rear = rear + 1; Because we are inserting elements from the rear in case of queue.

Description for void displayElementsInQueue() function


void displayElementsInQueue(){
	int i; 
	if(front== -1 && rear == -1)
	{
	printf("No Element is present in Queue");
	}

	for(i=front; i<=rear; i++)
	{
        printf("%d \t",queue[i]);	
	}

}

if(front== -1 && rear == -1)

When no elements present in the the queue we should check that condiion.

   for(i=front; i<=rear; i++)
	{
        printf("%d \t",queue[i]);	
	}

This for loop prints all the elements present in the queue.

Description for deleteElementsFromQueue() function


void deleteElementsFromQueue()
{

	if(front== -1 && rear == -1)
	{
	printf("Queue is Underflow");
	}
	else if(front==rear && front != -1 )
	{
	printf(" \n %d is deleted from Queue", queue[front]);
        front = -1;
	rear = -1;
        }
	else
	{
	printf("%d is deleted from Queue", queue[front]);
	front = front + 1 ;

	}


}

if(front== -1 && rear == -1)

This if condition is used for the checking underflow condition that wheather the queue is empty or not, because if the queue if empty then it is not possible to delete form the queue.

else if(front==rear && front != -1 )

This condition is required because if there is only one element is present in the queue.

  else
	{
	printf("%d is deleted from Queue", queue[front]);
	front = front + 1 ;

	}

This else condition is used for the general case. It deleted form the front position and the front will be increase by one. front = front + 1 ;

Output


.............Your Choice.............
Enter 1: for Insert New Element
Enter 2: for Insert display Element
Enter 3: for Insert Delete Element
Enter 4: for Insert Exit Element
............................
3
.......Delete Elements from Queue.........
Queue is Underflow
 Enter 0 to exit, if not press other number:
9

.............Your Choice.............
Enter 1: for Insert New Element
Enter 2: for Insert display Element
Enter 3: for Insert Delete Element
Enter 4: for Insert Exit Element
............................
1
.......Insetion Operation......
 Enter New Element: 20

 Enter 0 to exit, if not press other number:
9

.............Your Choice.............
Enter 1: for Insert New Element
Enter 2: for Insert display Element
Enter 3: for Insert Delete Element
Enter 4: for Insert Exit Element
............................
1
.......Insetion Operation......
 Enter New Element21

 Enter 0 to exit, if not press other number:
9

.............Your Choice.............
Enter 1: for Insert New Element
Enter 2: for Insert display Element
Enter 3: for Insert Delete Element
Enter 4: for Insert Exit Element
............................
2
.........Display all Elements in Queue.......
20      21
 Enter 0 to exit, if not press other number:
9

.............Your Choice.............
Enter 1: for Insert New Element
Enter 2: for Insert display Element
Enter 3: for Insert Delete Element
Enter 4: for Insert Exit Element
............................
3
.......Delete Elements from Queue.........
20 is deleted from Queue
 Enter 0 to exit, if not press other number:
9

.............Your Choice.............
Enter 1: for Insert New Element
Enter 2: for Insert display Element
Enter 3: for Insert Delete Element
Enter 4: for Insert Exit Element
............................
2
.........Display all Elements in Queue.......
21
 Enter 0 to exit, if not press other number:
0
Press any key to continue . . .