Saturday, August 9, 2014

Insertion and deletion of number into/from a queue

Simple C program with the deletion and insertion of elements into a queue. If you have any questions, please let me know.

#include<stdio.h>
void eq();
void dq();
void display();
int front=(-1),rear=(-1),q[50],n,i,d,x,a;
main()
{
int ch,c;
printf("enter the number of elements");
scanf("%d",&n);
do
{
printf("MENU \n1.enter \n2.delete \n3.display");
printf("enter choice");
scanf("%d",&ch);
switch(ch)
{
case 1: eq();
break;

case 2: dq();
break;

case 3: display();
break;

default: printf("worong chouice");
break;
}
printf("do you want to continue? \n1.yes \n2.no \n");
scanf("%d",&c);
}
while(c==1);
}
void eq()
{
printf("enter the number to be entered");
scanf("%d",&x);
if(rear==(n-1))
printf("full \n");
else if((front==(-1))&&(rear==(-1)))
{
front=0;
rear=rear+1;
q[rear]=x;
}
else
{
rear=rear+1;
q[rear]=x;
}
}
void dq()
{
if(front==(-1))
printf(" underflow ");
if((rear==(-1))&&(front==(-1)))
printf("its empty \n");
else if(front==rear)
{
d=q[front];
front=rear=(-1);
}
else
{
d=q[front];
front=front+1;
}
}
void display()
{
for(i=front;i<=rear;i++)
printf("%d ",q[i]);
printf(" \n");
}


No comments:

Post a Comment