1. 程式人生 > 其它 >迴圈佇列(簡化版)

迴圈佇列(簡化版)

技術標籤:c++筆記c++佇列

#include "windows.h"//Windows環境下Sleep()函式需要此標頭檔案 
#include "iostream"
#include <stdlib.h>
#define MAXQSIZE 10
#define OK 1
#define ERROR 0 
typedef int Status;
typedef int QElemType; 
using namespace std;
//定義結構體
typedef struct SqQueue{
    QElemType *base=NULL
;/*賦值為NULL的目的是防止base為野指標 野指標不同於空指標,空指標是指一個指標的值為null, 而野指標的值並不為null,而且非空,野指標會指向一段實際的記憶體, 只是它指向哪裡我們並不知情,或者是它所指向的記憶體空間已經被釋放, 所以在實際使用的過程中,我們並不能通過指標判空去識別一個指標是否為野指標。*/ int front,rear; }SqQueue; //選單提示函式 void Prompt(){ cout<<
"---------------------------------"<<endl; cout<<"即將彈出選單:"; for(int i=0;i<13;i++){ cout<<"* "; Sleep(50);//windows下使用 } cout<<"\n1.初始化 2.遍歷 3.查空 4.入隊 5.出隊\n6.隊長 7.隊頭 8.清空 9.銷燬 10.退出"; cout<<"\n---------------------------------"
; cout<<"\n請輸入:"; } //進度條列印函式 void Progressbar(){ for(int i=0;i<13;i++){ cout<<"* "; Sleep(10);//windows下使用 } cout<<endl<<endl;//輸出一次換行 } //初始化函式 Status InitQueue(SqQueue &Q){ Q.base=new QElemType(MAXQSIZE);//將Q.base指標指向一片申請的空間,型別為int,大小為space if(!Q.base)//如果Q.base為NULL,即空間未申請成功 { cout<<"\n迴圈佇列初始化失敗!"<<endl<<endl; return ERROR; } else cout<<"迴圈佇列初始化成功!空間大小為:"<<MAXQSIZE<<endl<<endl; Q.front=Q.rear=0; return OK; } //遍歷佇列 Status QueueTraverse(SqQueue Q){ if(Q.front==Q.rear){ cout<<"迴圈佇列為空!"<<endl<<endl; return ERROR;//返回值為void可以使用這種返回方式終止函式執行 } int p=Q.front; while(p!=Q.rear){ cout<<Q.base[p]<<" "; p=(p+1)%MAXQSIZE; } cout<<endl<<endl; return OK; } //查空 int QueueEmpty(SqQueue Q){ if(Q.front==Q.rear)//隊頭和對尾的下標一致則說明為空隊,非空隊下標不可能相等 return 1; else return 0; } //入隊 Status EnQueue(SqQueue &Q,QElemType e){ if((Q.rear+1)%MAXQSIZE==Q.front){//判斷隊是否已滿,取模運算可以實現下標迴圈的效果 cout<<"迴圈佇列已滿!"<<endl<<endl; return ERROR; } Q.base[Q.rear]=e; Q.rear=(Q.rear+1)%MAXQSIZE; cout<<"入隊成功!"<<endl<<endl; return OK; } //出隊 Status DeQueue(SqQueue &Q,QElemType &e){ if(Q.rear==Q.front)//非空不可能相等 { cout<<"迴圈佇列為空!"<<endl; return ERROR; } e=Q.base[Q.front]; Q.front=(Q.front+1)%MAXQSIZE; return OK; } //求隊長 int QueueLength(SqQueue Q){ return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE ; } //取隊頭元素 QElemType GetHead(SqQueue Q){ return Q.base[Q.front]; } //清空佇列 void ClearQueue(SqQueue &Q){ Q.rear=Q.front=0;//下標回到初始狀態,入隊新的元素時,清空前的隊內元素會被覆蓋 } //銷燬佇列 void DestroyQueue(SqQueue &Q){ delete [] Q.base;//雙重保險 Q.base=NULL;//雙重保險 } Status InitQueue(SqQueue &Q);//宣告初始化函式 Status QueueTraverse(SqQueue Q);//宣告遍歷佇列函式 int QueueEmpty(SqQueue Q);//宣告佇列查空函式 Status EnQueue(SqQueue &Q,QElemType e);//宣告入隊函式 Status DeQueue(SqQueue &Q,QElemType &e);//宣告出隊函式 int QueueLength(SqQueue Q);//宣告隊長函式 QElemType GetHead(SqQueue Q);//宣告取隊頭函式 void ClearQueue(SqQueue &Q);//宣告清空佇列函式 void DestroyQueue(SqQueue &Q);//宣告銷燬佇列函式 void Progressbar();//宣告進度條列印函式 void Prompt();//宣告提示函式 //主函式 int main(){ SqQueue Q;//構造結構體SqQueu型別變數Q int choose,flag=0;//choose用於switch選擇分支,exit用於while的迴圈於退出 QElemType se; while(!flag){ Prompt();//呼叫選單提示函式 cin>>choose;//鍵盤獲取的值放入choose中 cout<<"---------------------------------"<<endl; switch (choose) { case 1: if(Q.base==NULL){//如果Q.base指標沒有指向申請的空間,則視為沒有初始化 cout<<"準備初始化操作:"; Progressbar(); InitQueue(Q); } else //已經初始化則不必再次初始化 cout<<"迴圈佇列已經初始化!"<<endl<<endl; break;//初始化 case 2: if(Q.base!=NULL){//如果Q.base不為NULL,則說明已經初始化過,即滿足if條件 cout<<"準備遍歷操作:"; Progressbar(); QueueTraverse(Q); } else//如果Q.base為NULL,則說明未初始化過,不滿足if條件執行else cout<<"\n迴圈佇列還未初始化!"<<endl<<endl; break;//遍歷 case 3: if(Q.base!=NULL){ cout<<"準備查空操作:"; Progressbar(); if(QueueEmpty(Q))//隊頭和對尾的下標一致則說明為空隊,非空隊下標不可能相等 cout<<"迴圈佇列為空!"<<endl<<endl; else cout<<"迴圈佇列非空!"<<endl<<endl; } else cout<<"\n迴圈佇列還未初始化!"<<endl<<endl; break;//查空 case 4: if(Q.base!=NULL){ cout<<"請輸入您想入隊的元素:"; cin>>se; cout<<"準備入隊操作:"; Progressbar(); EnQueue(Q,se); } else cout<<"\n迴圈佇列還未初始化!"<<endl<<endl; break;//入隊 case 5: if(Q.base!=NULL){ cout<<"準備出隊操作:"; Progressbar(); if(DeQueue(Q,se)) cout<<"出隊元素為:"<<se<<endl<<endl; } else cout<<"\n迴圈佇列還未初始化!"<<endl<<endl; break;//出隊 case 6: if(Q.base!=NULL){ cout<<"正在查詢隊長:"; Progressbar(); cout<<"迴圈佇列空間長度為:"<<MAXQSIZE<<endl; cout<<"迴圈佇列長度為:"<<QueueLength(Q)<<endl<<endl; } else cout<<"\n迴圈佇列還未初始化!"<<endl<<endl; break;//求隊長 case 7: if(Q.base!=NULL){ cout<<"準備取隊頭元素:"; Progressbar(); if(Q.front!=Q.rear) cout<<"隊頭元素為:"<<GetHead(Q)<<endl<<endl; else cout<<"迴圈佇列為空!"<<endl<<endl; } else cout<<"\n迴圈佇列還未初始化!"<<endl<<endl; break;//取隊頭元素 case 8: if(Q.base!=NULL){ cout<<"準備清空佇列:"; Progressbar(); ClearQueue(Q); cout<<"佇列已清空!"<<endl<<endl; } else cout<<"\n迴圈佇列還未初始化!"<<endl<<endl; break;//清空佇列 case 9: if(Q.base!=NULL){ cout<<"準備銷燬操作:"; Progressbar(); DestroyQueue(Q); cout<<"迴圈佇列已銷燬!"<<endl<<endl; } else cout<<"\n迴圈佇列還未初始化!"<<endl<<endl; break;//銷燬佇列 case 10: cout<<"程式正在退出:"; Progressbar(); flag=1; break;//退出 default:cout<<"您輸入的選項有誤!請重新輸入!"<<endl;//輸入的數字不合法時呼叫default } } return 0; }