Tuesday, April 6, 2010

ds - enqueue and dequeue implementation in circular queue

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
#define n 8
struct queue
{
int f;
int r;
int a[n];
int data;
};
typedef struct queue queue;
void enqueue(queue *,int);
void dequeue(queue *);
void display(queue);
void main()
{
queue q;
clrscr();
q.f=-1;
q.r=-1;
enqueue(&q,11);
enqueue(&q,22);
enqueue(&q,33);
enqueue(&q,44);
enqueue(&q,55);
enqueue(&q,66);
display(q);
dequeue(&q);
display(q);
getch();
}
void enqueue(queue *q,int d)
{
if(q->r==n-1&&q->f==0||q->r+1==q->f)
{printf("memory overflow");
exit(0); }
if(q->f==-1&&q->r==-1)
{q->r=0;
q->f=0;}
else{
if(q->r==n-1)
q->r=0;
else
q->r=q->r+1;
}
q->a[q->r]=d;
}
void display(queue q)
{
int i=q.f;
while(i {printf("%d ",q.a[i]);
i++;}
}
void dequeue(queue *q)
{
int x;
if(q->r==-1&&q->f==-1)
{printf("underflow");
exit(0);}
x=q->a[q->f];
q->a[q->f]=0;
if(q->f==q->r) {
q->r=-1;
q->f=-1;}
else{
if(q->f==n-1)
q->f=0;
else
q->f++;
}
printf("\n%d deleted\n",x);
}

No comments:

Post a Comment