#include usingnamespacestd;constintMAXSIZE=100;typedefintElemType;typedefstruct{ElemTypedata[MAXSIZE];intfront;intrear;}SeqQueue;voidInitQueue(SeqQueue&Q){Q.front=Q.rear=0;}voidEnQueue(SeqQueue&Q,ElemTypex){if((Q.rear+1)%MAXSIZE==Q.front){cout<<"队列已满"<exit(1); }Q.rear=(Q.rear+1)%MAXSIZE;Q.data[Q.rear]=x;}ElemTypeDeQueue(SeqQueue&Q){if(Q.front==Q.rear){cout<<"队列已空"<exit(1); }Q.front=(Q.front+1)%MAXSIZE;ElemTypex=Q.data[Q.front];returnx;}boolQueueEmpty(SeqQueue&Q){returnQ.front==Q.rear;}boolQueueFull(SeqQueue&Q){return(Q.rear+1)%MAXSIZE==Q.front;}intQueueLength(SeqQueue&Q){return(Q.rear-Q.front+MAXSIZE)%MAXSIZE;}ElemTypeQueueMax(SeqQueue&Q){inti;intf=(Q.front+1)%MAXSIZE;ElemTypemax=Q.data[f];for(i=1;iif(max max=Q.data[f+i]; returnmax;}intmain(){SeqQueueQ;inttime=0;ElemTypee;intx,flag,index;InitQueue(Q);cout<<"请输入打印队列,以-1结束:";cin>>e;while(e!=-1){EnQueue(Q,e);cin>>e;}cout<<"请输入需打印任务在队列中的位置:";cin>>x;if(x>QueueLength(Q)){cout<<"输入的位置有误!\n";return0;}flag=x;//设置需打印任务在队列中的位置while(!QueueEmpty(Q)){e=DeQueue(Q);flag--;if(e{ EnQueue(Q,e);if(flag==0)flag=QueueLength(Q);}else{time++;if(flag==0)break;}}cout<<"完成打印任务所需时间为:"<
相关分类