A simple loop queue

Source: Internet
Author: User

 

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.