/*****************************************
C实现链式队列
*****************************************/
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int QueueData;
typedef struct node
{
QueueData data; //结点数据
struct node * next; //结点链指针
} QueueNode;
typedef struct LinkQueue
{
QueueNode *rear, *front;
} LinkQueue;
LinkQueue* InitQueue (void)
{
LinkQueue *Q=( LinkQueue * ) malloc( sizeof ( LinkQueue ) );
Q->rear=Q->front=NULL;
return Q;
}
int QueueEmpty ( LinkQueue *Q )
{
return Q->front == NULL;
}
int GetFront ( LinkQueue *Q, QueueData *x )
{
if ( QueueEmpty (Q) ) return 0;
(*x) = Q->front->data; return 1;
}
int EnQueue ( LinkQueue **Q, QueueData x )
{
QueueNode *p = ( QueueNode * ) malloc( sizeof ( QueueNode ) );
p->data = x;
p->next = NULL;
if ( (*Q)->front == NULL )
//空,创建第一个结点
(*Q)->front = (*Q)->rear = p;
else (*Q)->rear = (*Q)->rear->next = p;
return 1;
}
int DeQueue ( LinkQueue **Q, QueueData *x)
{
//删去队头结点,并返回队头元素的值
if ( QueueEmpty (*Q) ) return 0; //判队空
QueueNode *p = (*Q)->front;
(*x) = p->data; //保存队头的值
(*Q)->front = (*Q)->front->next; //新队头
if ((*Q)->front == NULL) (*Q)->rear = NULL;
free (p);
return 1;
}
int main()
{
int x=0,i=0;
LinkQueue *Q;
Q=InitQueue();
for(i=0;i<10;i++)
{
EnQueue(&Q,i);
}
for(i=0;i<10;i++)
{
DeQueue(&Q,&x);
printf("%d\n",x);
}
return 0;
}
C实现链式队列
最新推荐文章于 2023-04-24 19:50:55 发布