C program to perform array implementation of Queue ADT

0
569

C program to perform array implementation of Queue ADT

CONSTRUCTION:

  1. Start the program.
  2. Define the queue structure, read the size of the queue and choice of operation to be performed.
  3. Enqueue:
    1. Read the element, ‘x’ to be inserted.
    2. Check whether q.rear = q.size-1. If so print “queue full”.
    3. Else, increment the ‘rear’ & place the element, ‘x’ at the rear position.
  1. Dequeue:
    1. Initially front =0 and rear = -1. So check whether q.front > q.rear.     If so print “queue empty”.
    2. Delete the element in ‘front’ position.
    3. Increment the ‘front’ pointer.
  1. Display:
    1. If the queue is empty then print the message “queue is empty”
    2. Else, display all elements in the queue.
  2. Stop the program

PROGRAM:

#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#define MAX 5
int front,rear,count=0,item;
void insertion(int[],int);
void display(int[]);
void deletion(int[]);
void main()
{
 int queue[MAX],n,quit=0;
 front=0;
 rear=0;
 clrscr();
 do
{
 printf("OPERATION\n1==INSERT\n2==DELETE\n3==DISPLAY\n4==EXIT");
 printf("\nENTER THE CHOICE");
 scanf("%d",&n);
 switch(n)
 {
 case 1:
 printf("Enter the element to be inserted:");
 scanf("%d",&item);
 insertion(queue,item);
 break;
 case 2:
 deletion(queue);
 break;
 case 3:
 if(front==0)
 printf("queue is empty");
 else
 display(queue);
 break;
 case 4:
 quit=1;
 exit(0);
 break;
 default:
 printf("invalid operation");
 }
 }
 while(!quit);
 getch();
 }
 void insertion(queue,item)
 int queue[],item;
 {
 if(rear<MAX)
 {
 rear++;
 queue[rear]=item;
 if(!front)
 front=1;
 count++;
 printf("queue after insertion:\n");
 display(queue);
 }
 else
 printf("\nqueue overflow\n");
 }
 void deletion(queue)
 int queue[];
 {
 int item;
 if(front)
 {
 item=queue[front];
 count--;
 printf("\n the deleted element is %d \n",item);
 if(front==rear)
 {
 front=0;
 rear=0;
 printf("The queue is empty\n");
 }
 else
 front++;
 if(count)
 display(queue);
 }
 else
 printf("queue underflow\n");
 }
 void display(queue)
 int queue[];
 {
 int c;
 printf("the queue is ...");
 for(c=front;c<rear+1;c++)
 printf("%d\t",queue[c]);
 printf("\n"); }

 

Leave a Reply