Queue : queue means first in first out.(FIFO)
#include<stdio.h>
#define max 3
struct node
{
int a[max];
int first;
int last;
};
void init(struct node*);
void push(struct node*,int);
int pop(struct node*);
main()
{
struct node s;
int i;
init(&s);
push(&s,20);
push(&s,33);
push(&s,12);
push(&s,50);
i = pop(&s);
printf("the deleted number is %d\n",i);
}
void init(struct node *q)
{
q->first = -1;
q->last = -1;
}
void push(struct node *q,int num)
{
if(q->last == max-1)
{
printf("que is full\n");
}
else
{
q->last++;
q->a[q->last] = num;
}
if(q->first == -1)
q->first++;
}
int pop(struct node *q)
{
int n;
if(q->last == -1)
{
printf("que is empty\n");
}
else
{
n = q->a[q->first];
q->first++;
return n;
}
}
op :
que is full
the deleted number is 20
0 comments: