c语言队列实现
#include <stdio.h>
#include <malloc.h>
typedef enum {
false,
true
}Status;
typedef int QElemType;
typedef struct QNode
{
QElemType data;
struct QNode* next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front,rear;
}LinkQueue;
Status InitQueue(LinkQueue* Q)
{
QueuePtr p = (QNode*)malloc(sizeof(QNode));
Q->front = p;
Q->rear = p;
return true;
}
Status EnQueue(LinkQueue* Q,QElemType e)
{
QueuePtr p = (QNode*)malloc(sizeof(QNode));
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
return true;
}
Status DeQueue(LinkQueue* Q,QElemType* e)
{
QueuePtr p = NULL;
if(Q->front == Q->rear)
return false;
p = Q->front->next;
*e = p->data;
Q->front->next = p->next;
free(p);
if(Q->rear == p)
Q->rear = Q->front;
return true;
}
int main(void)
{
LinkQueue Q;
InitQueue(&Q);
QElemType e;
for(int i=0;i<10;i++){
e = i;
EnQueue(&Q,e);
printf("%d\n",e);
}
printf("-----------------\n");
for(int i=0;i<10;i++){
DeQueue(&Q,&e);
printf("%d\n",e);
}
return 0;
}