#include <stdio.h>
#include <malloc.h>
#define MAX 10
typedef struct NODE
{
int Queue[max];
int front,rear;
}q,*queue;
void init (Queue P)
{
p->front=-1;
p->rear=-1;
}
int empty (queue P)
{
if (p->front==p->rear)
{
printf ("Queue is empty \ n");
return 1;
}
Else
{
return 0;
}
}
int Insert (queue p,int x)
{
if ((p->rear+1)%max==p->front)
{
printf ("Queue is full \ n");
}
Else
{
P->rear= (p->rear+1)%max;
p->queue[p->rear]=x;
}
return 1;
}
int del (Queue p,int *n)
{
if (p->front==p->rear)
{
printf ("Queue is empty \ n");
}
Else
{
P->front= (p->front+1)%max;
*n=p->queue[p->front];
printf ("Deleted element:%d", *n);
printf ("\ n");
}
return 1;
}
void tra (Queue p)
{
for (int i=p->front+1;i<p->rear+1;i++)//loop queue front with rear not equal to-1; (Circle from the beginning)
{
printf ("%d", P->queue[i%max]);
}
}
int main ()
{//dynamic memory, so pointers to array memory are not executed immediately allocated
Queue L;int N;
L= (queue) malloc (sizeof (q));
Init (l);
Insert (l,1);
Insert (l,2);
Insert (l,3);
Insert (l,4);
Insert (l,5);
TRA (l);
printf ("\ n");
Empty (L);
Del (L,&N);
TRA (l);
}
A simple loop queue