Saturday, December 19, 2015

Write A Program to implement operation INSERTION & DELETION of linear queue using array.

Solution:-
 
#include<stdio.h>
#include<conio.h>
#include<process.h>
# define maxsize 5
int queue[maxsize];
int rear=-1;
int front=-1;
void enque();
void deque();
void display();
void main()
{
  int choice;
  clrscr();
  do
  {
            printf("\n 1.ENQUEUW");
            printf("\n 2.DEQUEUE");
            printf("\n 3.DISPLAY");
            printf("\n 4.EXIT");
            printf("\n Enter your choice: \t");
            scanf("%d",&choice);
            switch(choice)
            {
                case 1: enque();
                        break;
                case 2: deque();
                        break;
                case 3: display();
                        break;
                case 4: exit(0);
                        break;
            }
  }
  while(choice!=4);
  getch();
}
void enque()
{
  int item;
  if(rear==maxsize-1)
  printf("\n QUEUE OVERFLOW");
  else
  {
       printf("\n Enter item: \t");
       scanf("%d",&item);
       if(rear==-1)
       {
            rear=0;
            front=0;
       }
       else
       rear=rear+1;
       queue[rear]=item;
       printf("\n %d is inserted successfully",queue[rear]);
  }
}
void deque()
{
  int item;
  if(front==-1)
  printf("\n QUEUE UNDERFLOW");
  else
  {
      item=queue[front];
      if(rear==front)
      {
         front=-1;
         rear=-1;
      }
      else
      front=front+1;
      printf("\n %d is deleted successfully",item);
  }
}
void display()
{
  int i;
  if(rear==-1)
  printf("\n NO ITEM");
  else
  {
     for(i=front;i<=rear;i++)
     printf("%d \t",queue[i]);
  }
}