實驗三 棧和佇列的基本操作實現及其應用
阿新 • • 發佈:2019-01-05
執行結果:#include using namespace std; struct Node{ int data; Node *next; }; class QueueOfCanteen{ private: Node *front,*rear; int total; //計算當前排隊人數,並作為入隊單號的參考 public: QueueOfCanteen(); ~QueueOfCanteen(); int getTotal(); void enQueue(int x); int deQueue(); int getQueue(); }; QueueOfCanteen::QueueOfCanteen(){ total=0; front=new Node; front->next=NULL; rear=front; } QueueOfCanteen::~QueueOfCanteen(){ Node *p; rear=NULL; while(front!=NULL){ p=front; front=p->next; delete p; } } int QueueOfCanteen::getTotal(){ return total; } void QueueOfCanteen::enQueue(int x){ Node *s=new Node; s->data=x; s->next=NULL; rear->next=s; rear=s; total++; } int QueueOfCanteen::deQueue(){ if(front==rear) throw"無\n"; Node *p=front->next; front->next=p->next; int x=p->data; if(p->next==NULL) rear=front; delete p; total--; return x; } int QueueOfCanteen::getQueue(){ if(front==rear) throw"無\n"; return front->next->data; } int queue(QueueOfCanteen q){ int queue(QueueOfCanteen q); cout<<"\n\t\t===============飯堂排號叫號管理================\n\n"; cout<<"\t\t\t當前佇列人數:"<>s; if(q.getTotal()!=0&&(s=='y'||s=='Y')){ //佇列人數不為0,下一個則隊頭出隊 try{ q.deQueue(); }catch(...){} system("cls"); queue(q); } if(s=='n'||s=='N') return 0; return 0; } int menu(QueueOfCanteen q){ int a; system("cls"); cout<<"\n\t\t===============飯堂排號叫號管理================\n\n"; cout<<"\t\t\t1.入隊列印單號\n"; cout<<"\t\t\t2.叫號\n"; cout<<"\t\t\t3.退出\n"; cout<<"\n\t\t\t請選擇:"; cin>>a; system("cls"); switch(a){ case 1: q.enQueue(q.getTotal()+1); //取得單號total+1入隊 cout<<"\n\t\t\t單號:"<>c; if(c=='y'||c=='Y'){ menu(q); } if(c=='n'||c=='N') return 0; return 0; } int main(){ try{ QueueOfCanteen q; menu(q); }catch(char *p){ cout<