Queue implementation using array.


 

Queue implementation using array.

This tutorial demonstrate how to insert and delete element in the queue using array.

This tutorial demonstrate how to insert and delete element in the queue using array.

Description:

In this tutorial you will see how to implement queue using array and queue insert & delete operations.

Code:

#include <stdio.h>
#define MAX 5
#include <stdlib.h>

void insert(int queue[], int *rear, int value)
{
 if(*rear < MAX-1)
  {
  *rear= *rear +1;
  queue[*rear] = value;
  }
	 else
	 {
	  printf("The queue is full \n");
	  exit(1);
	 }
}

void deleteQ(int queue[], int *front, int rear, int *value)
{
	if(*front == rear)
	{
	printf("The queue is empty \n");
	exit(1);
	}
        *front = *front + 1;
	*value = queue[*front];
}

void main()
{
  int queue[MAX];
  int front,rear;
  int n,value;
  front=rear=(-1);
  do
	{
        do
	  {
         printf("Enter the element to be inserted in queue\n");
	 scanf("%d",&value);
	 insert(queue,&rear,value);
	 printf("Enter 1 to continue\n");
	 scanf("%d",&n);
	 } while(n == 1);
  printf("Enter 1 to delete an element from queue\n");
  scanf("%d",&n);
  	while( n == 1)
 	{
 deleteQ(queue,&front,rear,&value);
 printf("The value deleted is %d\n",value);
 printf("Enter 1 to delete an element from queue\n");
 scanf("%d",&n);
 }
  printf("Enter 1 to continue\n");
  scanf("%d",&n);
	} while(n == 1);
}

Output:

Download this code

Ads