您好,登錄后才能下訂單哦!
循環(huán)隊(duì)列:
1.循環(huán)隊(duì)列中判斷隊(duì)空的方法是判斷front==rear,隊(duì)滿的方法是判斷front=(rear+1)%maxSize。(我曾經(jīng)想過(guò)為什么不用一個(gè)length表示隊(duì)長(zhǎng),當(dāng)length==maxSize時(shí)隊(duì)滿)原因就是,在頻繁的隊(duì)列操作中,多出一個(gè)變量會(huì)大量的增加執(zhí)行時(shí)間,所以不如浪費(fèi)一個(gè)數(shù)組空間來(lái)得劃算。
2.用單鏈表表示的鏈?zhǔn)疥?duì)列特別適合于數(shù)據(jù)元素變動(dòng)較大的情形,而且不存在溢出的情況。
template<class T> class SeqQueue{ protected: T *element; int front,rear; int maxSize; public: SeqQueue(int sz=10){ front=rear=0; maxSize=sz; element=new T[maxSize]; } ~SeqQueue(){ delete[] element; } bool EnQueue(const T& x){//入隊(duì) if(isFull()) return false; element[rear]=x; rear=(rear+1)%maxSize; return true; } bool DeQueue(T& x){//出隊(duì) if(isEmpty()) return false; x=element[front]; front=(front+1)%maxSize; return true; } bool getFront(T& x){//獲取隊(duì)首元素 if(isEmpty()) return false; x=element[front]; return true; } void makeEmpty(){//隊(duì)列置空 front=rear=0; } bool isEmpty()const{//判斷隊(duì)列是否為空 return (rear==front)?true:false; } bool isFull()const{//隊(duì)列是否為滿 return ((rear+1)%maxSize==front)?true:false; } int getSize()const{ return (rear-front+maxSize)%maxSize; } };
測(cè)試代碼如下:
void menu(){ cout<<"1.入隊(duì)"<<endl; cout<<"2.獲取隊(duì)首元素"<<endl; cout<<"3.出隊(duì)"<<endl; cout<<"4.隊(duì)列置空"<<endl; cout<<"5.獲取隊(duì)中元素?cái)?shù)量"<<endl; cout<<"6.退出"<<endl; } void function(int num,SeqQueue<int> *sq){ switch(num){ int x; case 1: cin>>x; sq->EnQueue(x); break; case 2: sq->getFront(x); cout<<x<<endl; break; case 3: sq->DeQueue(x); break; case 4: sq->makeEmpty(); break; case 5: x=sq->getSize(); cout<<x<<endl; break; default: exit(1); } } int main(int argc, char** argv) { SeqQueue<int> *sq=new SeqQueue<int>; int num; while(true){ menu(); cin>>num; function(num,sq); } delete sq; return 0; }
之后是鏈?zhǔn)疥?duì)列,實(shí)現(xiàn)類代碼和測(cè)試代碼如下:
#include <iostream> using namespace std; template<class T> struct LinkNode{ T data; LinkNode<T> *link; LinkNode(T& x,LinkNode<T> *l=NULL){ data=x; link=l; } }; template<class T> class LinkedQueue{ protected: LinkNode<T> *front,*rear; public: LinkedQueue(){ front=rear=NULL; } ~LinkedQueue(){ makeEmpty(); } bool enQueue(T& x){ if(front==NULL) front=rear=new LinkNode<T>(x); else{ rear=rear->link=new LinkNode<T>(x); } return true; } bool deQueue(T& x){ if(isEmpty()) return false; LinkNode<T> *p=front; x=front->data; front=front->link; delete p; return true; } bool getFront(T& x)const{ if(isEmpty()) return false; x=front->data; return true; } void makeEmpty(){ LinkNode<T> *p; while(front!=NULL){ p=front; front=front->link; delete p; } } bool isEmpty()const{ return (front==NULL)?true:false; } int getSize()const{ LinkNode<T> *p; int count=0; p=front; while(p!=NULL){ count++; p=p->link; } return count; } }; void menu(){ cout<<"1.入隊(duì)"<<endl; cout<<"2.獲取隊(duì)首元素"<<endl; cout<<"3.出隊(duì)"<<endl; cout<<"4.隊(duì)列置空"<<endl; cout<<"5.獲取隊(duì)中元素?cái)?shù)量"<<endl; cout<<"6.退出"<<endl; } void function(int num,LinkedQueue<int> *lq){ switch(num){ int x; case 1: cin>>x; lq->enQueue(x); break; case 2: lq->getFront(x); cout<<x<<endl; break; case 3: lq->deQueue(x); break; case 4: lq->makeEmpty(); break; case 5: x=lq->getSize(); cout<<x<<endl; break; default: exit(1); } } int main(int argc, char** argv) { LinkedQueue<int> *lq=new LinkedQueue<int>; int num; while(true){ menu(); cin>>num; function(num,lq); } delete lq; return 0; }
以上這篇C++實(shí)現(xiàn)循環(huán)隊(duì)列和鏈?zhǔn)疥?duì)列的示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。