Saturday, November 5, 2011

Queue using pointer in C ( Dynamic Queue in C)

Queue using pointer in C ( Dynamic Queue in C)

#include<conio.h>
#include<stdio.h>
#define MAX 5
int queue[20];
void q_insert(int value,int *front,int *rear)
{
if((*front==0 && *rear==MAX) || (*front==*rear+1))
{
printf("\nOverflow.....\n");
return;
}
else if(*front==-1)
{
*front=0;
*rear=0;
}
else if(*rear==MAX)
{
*rear=0;
}
else
{
*rear=*rear+1;
}
queue[*rear]=value;
printf("\nValue has successfully inserted........");
}
void q_delete(int *value,int *front,int *rear)
{
if(*front==-1)
{
printf("\nUnderflow.....\n");
*value=-1;
return;
}
*value=queue[*front];
queue[*front]=0;
if(*front==MAX)
{
*front=0;
}
else if(*front==*rear)
{
*front=-1;
*rear=-1;
}
else
{
*front=*front+1;
}
}
void q_display()
{
int i;
for(i=0;i<=MAX;i++)
{
printf("\nValue %d Position %d",queue[i],i);
}
}
void main()
{
char ch;
int v;
int front=-1,rear=-1;
do
{
clrscr();
printf("1.Insert\n2.Delete\n3.Display\n4.Exit");
printf("\nEnter your choice:>>");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter value:");
scanf("%d",&v);
q_insert(v,&front,&rear);
getch();
break;
case 2:
q_delete(&v,&front,&rear);
printf("\n%d is Deleted....",v);
getch();
break;
case 3:
q_display();
getch();
break;
default:
printf("Invalid option has been selected...");
}
}while(ch!=4);
}

0 comments:

Post a Comment

You are most welcome for making comments here. I will consider your view and will reply you soon.