2/23/2009

C Program to Perform Insert(), Delete() And Display() Operation On Linear Queue


/* C Program to Perform Insert(), Delete() And Display() Operation On Linear Queue */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define queue_size 3
void insertion();
void deletion();
void display();
int element,q[10],f=0,r=-1;
void main()
{
 int choice;
 clrscr();
 for(;;)
 {
  printf("1:Insert 2:Delete 3:Display 4:Exit\n");
  printf("Enter your choice\n");
   scanf("%d",&choice);
  switch(choice)
  {
   case 1:
    printf("Enter the element to be inserted\n");
     scanf("%d",&element);
    insertion();
    break;
   case 2:
    deletion();
    break;
   case 3:
    display();
    break;
   default:
    exit(0);
  }
 getch();
 }
}

void insertion()
{
 if(r==queue_size-1)
  {
   printf("Overflow\n");
   return;
  }
 r=r+1;
 q[r]=element;
}
void deletion()
{
 if(f>r)
  {
   printf("Underflow\n");
   return;
  }
 printf("Deleted element is=%d\n",q[f]);
 f=f+1;
 if(f>r)
  {
    f=0;
    r=-1;
  }
}
void display()
{
 int i;
 if(f>r)
  {
   printf("Queue is empty\n");
   return;
  }
 printf("contents of Queue are\n");
 for(i=f;i<=r;i++)
  printf("%d\n",q[i]);
}
Out Put:
C Program to Perform Insert(), Delete() And Display() Operation On Linear Queue

No comments:

Post a Comment