Queue using structure
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 50
typedef struct queue
{
int ar[max];
int rear,front;
}qu;
void enqueue(qu *ps,int a);
int dequeue(qu *ps);
int isempty(qu *ps);
int isfull(qu *ps);
int peek(qu *ps);
void disp(qu *ps);
void init(qu *ps);
int main()
{
qu q;
int x,val;
init(&q);
while(1)
{
printf("press \n 1.enter\n2.delete\n3.Peek\n4.Display\n5.Exit\n");
scanf ("%d",&x);
switch(x)
{
case 1: if(!isfull(&q))
{
printf("enter value\n");
scanf("%d",&val);
enqueue(&q,val);
}
else
printf("no more space\n");
break;
case 2: if(!isempty(&q))
{
printf("Deleted value is \n%d",dequeue(&q));
}
else
printf("no more data\n");
break;
case 3: if(!isempty(&q))
printf("top value is \n%d",peek(&q));
else
printf("no data to display\n");
break;
case 4: if(!isempty(&q))
{
printf("values are\n");
disp(&q);
}
else
printf("no data t//insert a node after a particular nodeo display\n");
break;
case 5: exit(0);
default: printf("Wrong choice\n");
}
}
}
void enqueue(qu *p,int v)
{
if(p->front==-1&&p->rear==-1)
p->front=p->rear=0;
else if(p->rear==max-1)
{int i;
for(i=p->front;i<=p->rear;i++)
{
p->ar[i-p->front]=p->ar[i];
}
p->front=0;
p->rear=(p->rear-p->front)+1;
}
else
p->rear++;
p->ar[p->rear]=v;
}
int dequeue(qu *p)
{
int g;
g=p->ar[p->front];
p->front=p->front+1;
return g;
}
void init(qu *p)
{
p->front=p->rear=-1;
}
int peek(qu *ps)
{
return ps->ar[ps->front];
}
int isfull(qu *p)
{
if(p->front==0&&p->rear==max-1)
return 1;
else
return 0;
}
int isempty(qu *p)
{
if((p->rear==-1&&p->front==-1)||(p->front > p->rear) )
return 1;
else
return 0;
}
void disp(qu *p)
{
int i;
for(i=p->front;i<=p->rear;i++)
{
printf("%d \n",p->ar[i]);
}
}
I/O : detail description coming soon ...
#include<conio.h>
#include<stdlib.h>
#define max 50
typedef struct queue
{
int ar[max];
int rear,front;
}qu;
void enqueue(qu *ps,int a);
int dequeue(qu *ps);
int isempty(qu *ps);
int isfull(qu *ps);
int peek(qu *ps);
void disp(qu *ps);
void init(qu *ps);
int main()
{
qu q;
int x,val;
init(&q);
while(1)
{
printf("press \n 1.enter\n2.delete\n3.Peek\n4.Display\n5.Exit\n");
scanf ("%d",&x);
switch(x)
{
case 1: if(!isfull(&q))
{
printf("enter value\n");
scanf("%d",&val);
enqueue(&q,val);
}
else
printf("no more space\n");
break;
case 2: if(!isempty(&q))
{
printf("Deleted value is \n%d",dequeue(&q));
}
else
printf("no more data\n");
break;
case 3: if(!isempty(&q))
printf("top value is \n%d",peek(&q));
else
printf("no data to display\n");
break;
case 4: if(!isempty(&q))
{
printf("values are\n");
disp(&q);
}
else
printf("no data t//insert a node after a particular nodeo display\n");
break;
case 5: exit(0);
default: printf("Wrong choice\n");
}
}
}
void enqueue(qu *p,int v)
{
if(p->front==-1&&p->rear==-1)
p->front=p->rear=0;
else if(p->rear==max-1)
{int i;
for(i=p->front;i<=p->rear;i++)
{
p->ar[i-p->front]=p->ar[i];
}
p->front=0;
p->rear=(p->rear-p->front)+1;
}
else
p->rear++;
p->ar[p->rear]=v;
}
int dequeue(qu *p)
{
int g;
g=p->ar[p->front];
p->front=p->front+1;
return g;
}
void init(qu *p)
{
p->front=p->rear=-1;
}
int peek(qu *ps)
{
return ps->ar[ps->front];
}
int isfull(qu *p)
{
if(p->front==0&&p->rear==max-1)
return 1;
else
return 0;
}
int isempty(qu *p)
{
if((p->rear==-1&&p->front==-1)||(p->front > p->rear) )
return 1;
else
return 0;
}
void disp(qu *p)
{
int i;
for(i=p->front;i<=p->rear;i++)
{
printf("%d \n",p->ar[i]);
}
}
I/O : detail description coming soon ...
Comments
Post a Comment