队列的实现及应用(舞伴).docVIP

  • 20
  • 0
  • 约2.56千字
  • 约 5页
  • 2017-08-18 发布于重庆
  • 举报
队列的实现及应用(舞伴).doc

队列---------------------------------------------------------------------------------------------------------- 头文件(头文件名为text9) struct Queue{ ElemType *queue; int front,rear,len; int MaxSize; }; void InitQueue(Queue Q); //构造一个空队列 Q int EmptyQueue(Queue Q); //判断队列Q是否为空,若空返回1,否则返回0 void EnQueue(Queue Q, ElemType item); //元素 item 进队列Q ElemType OutQueue(Queue Q); //队头元素出队列Q,并返回其值 ElemType PeekQueue(Queue Q); //返回队头元素值 void ClearQueue(Queue Q); //清空队 void InitQueue(Queue Q) { Q.MaxSize=10; Q.len=0; Q.queue=new ElemType[Q.MaxSize]; Q.front=Q.rear=0; } int EmptyQueue (Queue Q) { return Q.front==Q.rear; } void EnQueue (Queue Q, ElemType item) { if((Q.rear+1)%Q.MaxSize==Q.front){ int k=sizeof(ElemType); Q.queue=(ElemType*)realloc(Q.queue,2*Q.MaxSize*k); if(Q.rear!=Q.MaxSize-1){ for(int i=0;i=Q.rear;i++) Q.queue[i+Q.MaxSize]=Q.queue[i]; Q.rear+=Q.MaxSize; } Q.MaxSize=2*Q.MaxSize; } Q.rear=(Q.rear+1)%Q.MaxSize; Q.queue[Q.rear]=item; Q.len++; } ElemType OutQueue (Queue Q) { if(Q.front==Q.rear){ cerrqueue is empty!endl; exit(1); } Q.front=(Q.front+1)%Q.MaxSize; Q.len--; return Q.queue[Q.front]; } ElemType PeekQueue (Queue Q) { if(Q.front==Q.rear){ cerrqueue is empty!endl; exit(1); } return Q.queue[(Q.front+1)%Q.MaxSize]; } void ClearQueue (Queue Q) { if(Q.queue!=NULL) delete []Q.queue; Q.front=Q.rear=0; Q.queue=NULL; Q.MaxSize=0; } CPP文件 #includeiostream.h #includestdio.h #includestdlib.h #includestring.h typedef struct{ char name[10]; char sex; }dancer; typedef dancer ElemType; #includetext9.h void partner() { Queue QM,QF; dancer t; char ch,oldch; int i; InitQueue(QM); InitQueue(QF); oldch= ; printf(请输入跳舞者的姓名和性别(以# #结束):\n); while((ch=getchar())!=#||oldch!=#) { i=0; while(ch!=\n) { if(ch!= ) t.name[i++]=ch; else{ ch=getchar(); t.sex=ch;

文档评论(0)

1亿VIP精品文档

相关文档