/*
队列 链式存储结构
*/
/*
队列 链式存储结构
*/
#include <stdlib.h>
#include <stdio.h>
#define ERROR -1
#define OK 0
typedef int ElemType;
typedef struct Node
{
ElemType value;
struct Node *next;
}Node;
typedef struct LinkQueue
{
Node *front;
Node *rear;
int count;
}LinkQueue;
int initQueue(LinkQueue *q)
{
Node *p = (Node *)malloc(sizeof(Node));
p->value = 0;
p->next = NULL;
q->rear = q->front = p;
if (q->front == NULL)
{
return ERROR;
}
q->count = 0;
return OK;
}
int inQueue(LinkQueue *q, ElemType value)
{
Node *p = (Node *)malloc(sizeof(Node));
if (!p)
{
return ERROR;
}
p->value = value;
p->next = NULL;
q->rear->next = p;
q->rear = p;
q->count += 1;
printf("inQueue count=%d\n", q->count);
return OK;
}
int outQueue(LinkQueue *q,ElemType *value)
{
Node *del = q->front->next;
if (!del)
{
return ERROR;
}
*value = del->value;
q->front->next = del->next;
if (del == q->rear)
{
q->rear = q->front;
}
free(del);
q->count -= 1;
printf("outQueue count=%d\n", q->count);
return OK;
}
int main()
{
LinkQueue *q = (LinkQueue*)malloc(sizeof(LinkQueue));
initQueue(q);
for (int i = 1; i <= 10; i++)
{
inQueue(q,i);
}
for (int i = 0;i<=10;i++)
{
int data = 0;
int ret = outQueue(q, &data);
printf("outQueue i=%d ret=%d data=%d\n", i, ret, data);
}
free(q);
return 0;
}
队列是只允许在一端插入另外一端删除操作的线性表。。。。
代码经过vs2017测试过,有啥问题欢迎交流