1.statu.h
#ifndef STATUS_H
#define STATUS_H
//函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
//Status是函数的类型,其值是函数结果状态代码
typedef int Status;
typedef char SElemType;
typedef int ElemType;
typedef int QElemType;
#endif
2.DuiLieLian.h
#ifndef DUILIELIAN_H
#define DUILIELIAN_H
#include "status.h"
typedef struct QNode{
QElemType date;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front; //队头指针
QueuePtr rear;
}LinkQueue;
Status IniQueue(LinkQueue *Q);
Status EnQueue(LinkQueue *Q,QElemType e);
Status DeQueue(LinkQueue *Q,QElemType *e);
Status GetHead(LinkQueue Q,QElemType *e);
Status QueueEmpty(LinkQueue Q);
Status DestroyQueue(LinkQueue *Q);
//Status QueueTraverse(LinkQueue Q,visit());
int QueueLength(LinkQueue Q);
Status ClearQueue(LinkQueue *Q);
void Output(LinkQueue Q);
#endif
***3DuiLieLIan.c***
#include "DuiLieLian.h"
#include <stdio.h>
Status IniQueue(LinkQueue *Q){
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if( !Q->front)exit(OVERFLOW);
Q->front->next=NULL;
return OK;
}
Status EnQueue(LinkQueue *Q,QElemType e){
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->date=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e){
QueuePtr p;//QNode *p;
if(Q->front==Q->rear) return ERROR;
p=Q->front->next;
*e=p->date;
Q->front->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);
return OK;
}
Status GetHead(LinkQueue Q,QElemType *e){
if(QueueEmpty(Q)) exit(ERROR);
*e=Q.front->next->date;
return *e;
}
Status QueueEmpty(LinkQueue Q){
if(Q.front==Q.rear)return TRUE;
return FALSE;
}
Status DestroyQueue(LinkQueue *Q){
while(Q->front){
Q->rear=Q->front->next;
free(Q->front);
Q->front=Q->rear;
}
return OK;
}
void Output(LinkQueue Q){
QueuePtr p;
p=Q.front->next;
while(p!=Q.rear){
printf("%d ",p->date);
p=p->next;
}
printf("\n");
}
4.DuiLieLian_main.c
#include "DuiLieLian.h"
#include <stdio.h>
void main(){
LinkQueue Q;
QElemType m,*p,*e;
p=&m;
IniQueue(&Q);
EnQueue(&Q,2);
EnQueue(&Q,3);
EnQueue(&Q,5);
EnQueue(&Q,7);
printf("插入数据后的对列为:");
Output(Q);
DeQueue(&Q,p);
printf("出栈的元素为%d\n",*p);
printf("删除一个元素后的对列为:");
Output(Q);
printf("队列头元素为:%d\n",GetHead(Q,&e));
printf("队列是否为空:%d\n",QueueEmpty(Q));
return 0;
}