//LinkQueue.cpp
#include"predefined.h" #include"LinkQueue.h" Status InitQueue(LinkQueue *Q) //构造一个新队列Q { (*Q).front=(*Q).rear=(QueuePtr)malloc(sizeof(QNode)); if(!(*Q).front) exit(OVERFLOW); (*(*Q).front).next=NULL; return OK; } Status DestroyQueue(LinkQueue *Q) //销毁队列Q,Q不再存在 { while((*Q).front) { (*Q).rear=(*(*Q).front).next; free((*Q).front); (*Q).front=(*Q).rear; } return OK; } Status ClearQueue(LinkQueue *Q) //将Q清为空队列 { QueuePtr Z; while((*Q).front!=(*Q).rear) { Z=(*(*Q).front).next; free((*Q).front); (*Q).front=Z; } return OK; } Status QueueEmpty(LinkQueue Q) //若队列为空队列,则返回TRUE,否则返回FALSE { if(Q.front==Q.rear) return TRUE; else return FALSE; } int QueueLength(LinkQueue Q) //返回Q的元素个数,即为队列的长度 { QueuePtr Z=Q.front; int count=0; while(Z!=Q.rear) { Z=(*Z).next; count++; } return count; } Status GetHead(LinkQueue Q,QElemType *e) //若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR。 { if(QueueEmpty(Q)) return ERROR; *e=(*(*Q.front).next).data; return OK; } Status EnQueue(LinkQueue *Q,QElemType e) //插入元素e为Q的新的队尾元素 { QueuePtr N; N=(QueuePtr)malloc(sizeof(QNode)); if(!N) exit(OVERFLOW); (*N).data=e; (*N).next=NULL; (*Q).rear=(*(*Q).rear).next=N; return OK; } Status DeQueue(LinkQueue *Q,QElemType *e) //若队列不为空,则删除Q的队头元素,用e返回其值,并返回OK; //否则返回ERROR { if(QueueEmpty(*Q)) return ERROR; QueuePtr N; N=(*(*Q).front).next; *e=(*N).data; (*(*Q).front).next=(*N).next; if((*Q).rear==N) (*Q).rear=(*Q).front; free(N); return OK; } void QueueTraverse(LinkQueue Q,void (*visit)(QElemType e)) //从队头到队尾,依次对Q的每个数据元素调用函数visit() { QueuePtr Z=(*Q.front).next; QElemType e; while(Z) { e=(*Z).data; visit(e); Z=(*Z).next; } } void PrintElem(QElemType e) { printf("%d ",e); }
#include"predefined.h" #include"LinkQueue.h" int main() { LinkQueue Q; Status s; int i; QElemType e; printf("Function 1\n★函数Status InitQueue(LinkQueue *Q)测试...\n"); s=InitQueue(&Q); printf("▲初始化链队列Q: %d (0:失败 1:成功)\n\n",s); printf("Function 2\n★函数Status QueueEmpty(LinkQueue Q)测试...\n"); QueueEmpty(Q)?printf("▲链队列Q为空!!!\n\n"):printf("▲链队列Q非空!!!\n\n"); printf("Function 3\n★函数Status EnQueue(LinkQueue *Q,QElemType e)测试...\n"); for(i=1;i<6;i++) { printf("▲元素\"%2d\"入队\n",2*i); EnQueue(&Q,2*i); printf("▲累计第%d元素\n",QueueLength(Q)); } printf("\n"); printf("Function 4\n★函数void QueueTraverse(LinkQueue Q,void (*visit)(QElemType e))测试...\n"); printf("▲队列Q中的元素为:Q={"); QueueTraverse(Q,PrintElem); printf("}\n\n"); printf("Function 5\n★函数Status DeQueue(LinkQueue *Q,QElemType *e)测试...\n"); DeQueue(&Q,&e); printf("▲队列Q队头元素\"%2d\"出队\n",e); printf("▲队列Q中的元素为:Q={"); QueueTraverse(Q,PrintElem); printf("}\n\n"); printf("Function 6\n★函数int QueueLength(LinkQueue Q)测试...\n"); i=QueueLength(Q); printf("▲队列Q的长度为%d\n\n",i); printf("Function 7\n★函数Status GetHead(LinkQueue Q,QElemType *e)测试...\n"); GetHead(Q,&e); printf("▲队列Q队头元素\"%2d\"\n\n",e); printf("Function 8\n★函数Status ClearQueue(LinkQueue *Q)测试...\n"); printf("▲置空前:"); QueueEmpty(Q)?printf("▲链队列Q为空!!!\n"):printf("▲链队列Q非空!!!\n"); ClearQueue(&Q); printf("▲置空后:"); QueueEmpty(Q)?printf("▲链队列Q为空!!!\n\n"):printf("▲链队列Q非空!!!\n\n"); printf("Function 9\n★函数Status DestroyQueue(LinkQueue *Q)测试...\n"); printf("▲销毁前:"); Q.front?printf("▲链队列Q存在!!!\n"):printf("▲链队列Q不存在!!!\n"); DestroyQueue(&Q); printf("▲销毁后:"); Q.front?printf("▲链队列Q存在!!!\n\n"):printf("▲链队列Q不存在!!!\n\n"); return 0; }