"a.h"
#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#include"string.h"
#include"math.h"
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef int Status;
typedef struct QNode{
ElemType data;
struct QNode *next;
}QNode ,*QueuePtr;
typedef struct {
QueuePtr front ,rear;
}LinkQueue;
"b.h"
#include"b.c"
Status InitQueue(LinkQueue *q);
Status DestroyQueue(LinkQueue *q);
Status ClearQueue(LinkQueue *q);
Status QueueEmpty(LinkQueue q);
Status QueueLength(LinkQueue q);
Status GetHead(LinkQueue q,ElemType *e);
//取队头元素
Status InsertQueue(LinkQueue *q,ElemType e);
//添加元素,从队尾插入
Status DeleteQueue(LinkQueue *q,ElemType *e);
//删除元素,从队头删除
Status QueueTraverse(LinkQueue q,Status(*fun)(ElemType));
"b.c"
#include"a.h"
Status InitQueue(LinkQueue *q)
{
(*q).frOnt=(QNode*)malloc(sizeof(QNode));
if(!(*q).front)
return ERROR;
(*q).rear=(*q).front;
(*q).front->next=NULL;
return TRUE;
}
Status DestroyQueue(LinkQueue *q)
{
while((*q).front){
(*q).rear=(*q).front->next;
free((*q).front);
(*q).frOnt=(*q).rear;
}
return OK;
}
Status ClearQueue(LinkQueue *q)
{
QueuePtrp=(*q).front->next;
while(p){
(*q).rear=p->next;
free(p);
p=(*q).rear;
}
(*q).rear=(*q).frOnt=NULL;
return OK;
}
Status QueueEmpty(LinkQueue q)
{
if(q.front!=q.rear)
returnFALSE;
else
returnTRUE;
}
int QueueLength(LinkQueue q)
{
inti=0;
QueuePtr p=q.front;
while(p){
p=p->next;
i++;
}
returni;
}
Status GetHead(LinkQueue q,ElemType *e)
//取队头元素
{
if(q.frOnt==q.rear)
return ERROR;
(*e)=q.front->data;
returnTRUE;
}
Status InsertQueue(LinkQueue *q,ElemType e)
//添加元素,从队尾插入
{
QueuePtr p=(QNode *)malloc(sizeof(QNode));
if(!p)
returnERROR;
p->data=e;
p->next=NULL;
(*q).rear->next=p;
(*q).rear=p;
return OK;
}
Status DeleteQueue(LinkQueue *q,ElemType *e)
//删除元素,从队头删除
{
QueuePtr p=(*q).front->next;
if((*q).frOnt==(*q).rear)
returnERROR;
(*e)=p->data;
(*q).front->next=p->next;
free(p);
return OK;
}
Status QueueTraverse(LinkQueue q,Status(*fun)(ElemType))
{
QueuePtr p=q.front->next;
printf("队头");
while(p){
fun(p->data);
p=p->next;
}
printf("队尾\n");
return OK;
}
"main.c"
#include"b.h"
#define LEN 5
Status fun(ElemType e)
{
printf(" %d ",e);
return OK;
}
int main()
{
int i;
LinkQueue q;
ElemType e;
InitQueue(&q);
if(QueueEmpty(q))
printf("队列为空\n");
for(i=0;i
printf("输入元素并插入:");
scanf("%d",&e);
InsertQueue(&q,e);
}
QueueTraverse(q,fun);
for(i=0;i<3;i++){
DeleteQueue(&q,&e);
printf("被删除的元素为%d\n",e);
}
QueueTraverse(q,fun);
if(!QueueEmpty(q))
printf("队列不为空\n");
printf("队列的长度为 %d\n",QueueLength(q));
ClearQueue(&q);
printf("队列的长度为 %d\n",QueueLength(q));
DestroyQueue(&q);
return OK;
}