///dabbysunshine@qq.com /** 《数据结构》严蔚敏.吴伟民P63-65.循环队列 **/ ///如有BUG,请发邮件联系 #include "stdio.h" #include "stdlib.h" #include "string.h" //#include "malloc.h" //#include <iostream> //using namespace std; ///============================================================================= struct StudentRecord { char* Name; int Number; }; void InitRecord(StudentRecord &record) { record.Name = NULL; record.Number = 0; } void SetRecord(StudentRecord &record,char* name,int number) { if(record.Name != NULL) { free(record.Name); record.Name = NULL; } record.Name = (char*)malloc(strlen(name) + 1); strcpy(record.Name,name); record.Number = number; } void DestoryRecord(StudentRecord &record) { if(record.Name != NULL) { free(record.Name); record.Name = NULL; } record.Number = 0; } void PrintRecord(StudentRecord &record) { printf("%s,%d/n",record.Name,record.Number); } ///============================================================================= #define MAXQSIZE 6 struct SqQueue { StudentRecord *base; int front; int rear; }; void InitQueue(struct SqQueue &Q) { ///构造一个空队列Q Q.base = (StudentRecord*)malloc(MAXQSIZE*sizeof(StudentRecord)); if(!Q.base) { puts("储存分配失败!"); exit(0); } Q.rear = Q.front = 0; } bool EnQueue(struct SqQueue &Q,StudentRecord record) { ///插入新的队尾元素record if((Q.rear + 1)%MAXQSIZE == Q.front) { puts("队已满."); return false; } SetRecord(Q.base[Q.rear],record.Number); Q.rear = (Q.rear+1)%MAXQSIZE; return true; } bool DeQueue(struct SqQueue &Q,StudentRecord &record) { ///队列非空条件下,删除队头元素 if(Q.front == Q.rear) { puts("队列为空."); return false; } SetRecord(record,Q.base[Q.front].Name,Q.base[Q.front].Number); Q.front = (Q.front+1)%MAXQSIZE; return true; } /**调用指针函数输出队列**/ typedef void(*Func)(StudentRecord &record); /**指针实现**/ /*void QueueTraverse(SqQueue &Q,Func visit) { int cur = Q.front; StudentRecord *p = Q.base; if(Q.front == Q.rear) { puts("队列为空."); } else { while((Q.rear)%MAXQSIZE != cur) { visit(*p); p++; cur = (cur + 1)%MAXQSIZE; } } }*/ /**非指针实现**/ void QueueTraverse(struct SqQueue &Q,Func visit) { int cur = Q.front; if(Q.front == Q.rear) { puts("队列为空."); } else { while(cur != (Q.rear)%MAXQSIZE) { visit(Q.base[cur]); cur = (cur + 1)%MAXQSIZE; } } } void DestoryQueue(struct SqQueue &Q) { QueueTraverse( Q,DestoryRecord); /**调用指针函数摧毁队列中元素**/ free(Q.base); Q.base = NULL; Q.front = Q.rear = 0; } int main(void) { StudentRecord record; StudentRecord record0; StudentRecord record1; StudentRecord record2; StudentRecord record3; InitRecord(record); InitRecord(record0); InitRecord(record1); InitRecord(record2); InitRecord(record3); SetRecord(record0,"NYJ",100); SetRecord(record1,"CLF",101); SetRecord(record2,"LQJ",102); SetRecord(record3,"ZCY",103); struct SqQueue Q; InitQueue(Q); printf("/nQueue part 1:/n"); EnQueue(Q,record0); EnQueue(Q,record1); EnQueue(Q,record2); EnQueue(Q,record3); puts("生成的队列为:"); QueueTraverse(Q,PrintRecord); printf("/nQueue part 2:/n"); DeQueue( Q,record); puts("删除的队头元素为:"); PrintRecord(record); puts("删除队头元素后的队列为:"); QueueTraverse(Q,PrintRecord); printf("/nQueue part 3:/n"); puts("摧毁队列:"); DestoryQueue(Q); QueueTraverse(Q,PrintRecord); return 0; }
代码如有问题,欢迎发邮件dabbysunshine@qq.com咨询