队列的定义及基本操作
记住先进先出就OK
#include <stdio.h>
#include <malloc.h>
struct node {
int data;
node *next;
};
struct queue{
node *front;
node *rear;
};
queue* initQueue(){
queue * q=(queue *)malloc(sizeof(queue));
q->front=q->rear=NULL;
return q;
}
int isEmpty(queue *q)
{
return q->front ==NULL;
}
void insertQueue(queue *q, int a)
{
node *n=(node* )malloc(sizeof(node ));
n->data=a;
n->next=NULL;
if(q->front ==NULL)
{
q->front=n;
q->rear=n;
}
else
{
q->rear->next=n;
q->rear=n;
}
}
int deleteQueue(queue *q)
{
int d;
node *p;
if(q->front==NULL)
{
return NULL;
}
else
{
d=q->front->data;
p=q->front;
q->front=p->next;
free(p);
}
return d;
}
int queuesize(queue *q)
{
int num=0;
node *p;
p=q->front;
while (p!=NULL)
{
num++;
p=p->next;
}
return num;
}
void Display(queue *q)
{
int i=0;
node *p;
p=q->front;
while(p!=NULL)
{
printf("第%d个数据是%d\n",i,p->data);
p=p->next;
i++;
}
}
int main()
{
queue *q=initQueue();
if(isEmpty(q));
printf("q是个空队列\n");
insertQueue(q,1);
insertQueue(q,2);
insertQueue(q,3);
insertQueue(q,4);
insertQueue(q,5);
int num=queuesize(q);
printf("队列中有%d个元素\n",num);
Display(q);
deleteQueue(q);
Display(q);
}