Monday, 21 March 2016

Write an algorithm for the implementation of a Dequeue.

#include<stdio.h>
#include<conio.h>
#define max 10
struct queue
{
int arr[max];
int front, rear;
}q;
void init()
{
q.front=-1,q.rear=-1;
}
void add_front(int n)
{
if (q.front==0)
{
printf("Element can not be inserted at front");
return;
}
else
{
q.front--;
q.arr[q.front]=n;
if(q.front==-1)
q.front=0;
}
}
void del_front()
{
int a;
if(q.front==-1)
{
printf("queue is empty");
return;
}
else
a=q.front;
printf("\n\deleted number is =%d at position %d",q.arr[a],a);
if(q.front==q.rear)
q.front=q.rear-1;
else
q.front=q.front+1;
}
void add_rear(int n)
{
if (q.front==max-1)
{
printf("Element can not be inserted at rear");
return;
}
else
{
q.rear=q.rear+1;
q.arr[q.rear]=n;
if(q.rear==-1)
q.rear=0;
}
}
void del_rear()
{
int a;
if(q.rear==-1)
{
printf("queue is empty");
return;
}
else
{
a=q.front;
printf("\n\deleted number is =%d ",q.arr[a]);
if(q.front==q.rear)
q.front=q.rear-1;
else
{
q.rear=q.rear-1;
}
}
}
void disp()
{
int i;
printf("\n\nQueue as an array\n\n");
for(i=q.front;i<=q.rear;i++)
printf("--->%d",q.arr[i]);
}
main()
{
int ch,n;
char c;
init();
do
{
printf("\n1-add front\n");
printf("2-add rear\n");
printf("3-delete front\n");
printf("5-display rear\n");
printf("6- Exit\n");
printf("\n\n enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nenter the number");
scanf("%d",&n);
add_front(n);
break;
case 2:
printf("\nenter the number");
scanf("%d",&n);
add_rear(n);
break;
case 3:
del_front();
break;
case 4:
del_rear();
break;
case 5:
disp();
break;
case 6:
break;
default:
printf("sorry wrong choice");
   }
printf("\nD you want to continue(y/n)");
c=getch();
}while(c=='y');
getch();
}

No comments:

Post a Comment