//此程序按照书上实现链式队列
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
typedef struct
{
int num;
char sex;
}QElemType;
typedef struct Qnode
{
QElemType data;
struct Qnode *next;
}Qnode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue *Q);
//构造一个空队列,即只包含一个空节点
int ClearQueue(LinkQueue *Q);
//将Q清空为空队列
int QueueEmpty(LinkQueue Q);
//若队列Q为空,则返回TRUE;否则返回FALSE
int QueueLength(LinkQueue Q);
//返回队列的长度,即元素个数
int GetHead(LinkQueue Q,QElemType *e);
//若队列不空,返回对头元素
int EnQueue(LinkQueue *Q,QElemType e);
//插入元素e为Q的新的队尾元素 即入队
int DeQueue(LinkQueue *Q,QElemType *e);
//若队列不空,则删除队头元素 即出队
int PrintQueue(LinkQueue Q);
//显示队列中所有元素
int main(void)
{
LinkQueue Q;
QElemType newelem,head;
InitQueue(&Q);
if( QueueEmpty(Q)==1 ) //测试初始化函数
printf("初始队列为空!/n");
newelem.num=111;
newelem.sex='a';
EnQueue(&Q,newelem);
newelem.num=222;
newelem.sex='b';
EnQueue(&Q,newelem);
newelem.num=333;
newelem.sex='c';
EnQueue(&Q,newelem);
PrintQueue(Q);
printf("length: %3d/n",QueueLength(Q));
GetHead(Q,&head);
printf("Gethead: %3d%3c/n/n",head.num,head.sex);
DeQueue(&Q,&head);
printf("出队: %3d%3c/n",head.num,head.sex);
PrintQueue(Q);
DeQueue(&Q,&head);
printf("/n出队: %3d%3c/n",head.num,head.sex);
PrintQueue(Q);
newelem.num=444;
newelem.sex='d';
EnQueue(&Q,newelem);
printf("/n入队:/n");
PrintQueue(Q);
ClearQueue(&Q); //将队列清空,测试各个函数
DeQueue(&Q,&head);
GetHead(Q,&head);
PrintQueue(Q);
if( QueueEmpty(Q)==1 )
printf("结果队列为空!/n");
system("pause");
return 0;
}
int InitQueue(LinkQueue *Q) //初始化队列,构造一个空头节点
{
Q->front=Q->rear=(QueuePtr)malloc(sizeof(Qnode));
if(Q->front==NULL)
exit(ERROR);
Q->front->next=NULL;
return OK;
}
int ClearQueue(LinkQueue *Q) //清空队列
{
QueuePtr p=Q->front->next,q;
Q->front->next=NULL; //将头结点指向清空
if( Q->front==Q->rear )
{
printf("队列已空,不用清空!/n");
return ERROR;
}
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
Q->rear=Q->front; //front和rear都指向头结点
return OK;
}
int QueueEmpty( LinkQueue Q ) //判断队列是否为空
{
if( Q.front==Q.rear )
return OK;
else
return ERROR;
}
int QueueLength(LinkQueue Q) //返回队列的长度,即元素个数
{
int length=0;
QueuePtr p=Q.front->next;
while(p!=NULL)
{
length++;
p=p->next;
}
return length;
}
int GetHead(LinkQueue Q,QElemType *e) //若队列不空,返回对头元素
{
if( Q.front==Q.rear )
{
printf("队列为空,无法获得队头元素!/n");
return ERROR;
}
*e=Q.front->next->data;
return OK;
}
int EnQueue(LinkQueue *Q,QElemType e) //插入元素e为Q的新的队尾元素 即入队
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(Qnode));
if(p==NULL)
exit(ERROR);
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return OK;
}
int DeQueue(LinkQueue *Q,QElemType *e) //若队列不空,则删除队头元素 即出队
{
QueuePtr p;
p=Q->front->next;
if(Q->front==Q->rear)
{
printf("队列为空,无法出队!/n");
return ERROR;
}
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front; //删除队列中唯一元素的情况
free(p);
return OK;
}
int PrintQueue(LinkQueue Q) //显示当前队列
{
QueuePtr p;
p=Q.front->next;
if(Q.front==Q.rear)
{
printf("队列为空,无法显示!/n");
return ERROR;
}
printf("------当前队列--------/n");
printf("num sex/n");
while(p!=NULL)
{
printf("%3d%3c/n",p->data.num,p->data.sex);
p=p->next;
}
//printf("/n");
return OK;
}
//请各位多多指教!