一、實驗要求
<1>初始化一個隊列。
<2>判斷是否隊空。
<3>判斷是否隊滿。
<4>入隊
<5>出隊
<6>取隊頭元素
<7>求目前隊列中元素個數
<8>編寫算法實作
①初始化空循環隊列;
②當鍵盤輸入奇數時,此奇數入隊;
③當鍵盤輸入偶數時,隊頭出隊;
④當鍵盤輸入0時,算法退出;
⑤每當鍵盤輸入後,輸出目前隊列中的所有元素。
二、資料結構設計
template<class ElementType>
class Queue{
public:
Queue(); //初始化隊列
~Queue(); //銷毀隊列
bool empty(); //判斷是否為空
bool full(); //判斷棧是否為滿
void enter(ElementType x); //入隊
void out(); //出隊
void getFront(ElementType &x);//取隊頭元素
int getLength(); //求目前隊列中元素個數
void print(); //周遊隊列
private:
ElementType * data; //資料域
int front; //首指針
int rear; //尾指針
};
三、代碼實作
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include <iostream>
#define MAXLEN 100
using namespace std;
template<class ElementType>
class Queue{
public:
Queue(); //初始化隊列
~Queue(); //銷毀隊列
bool empty(); //判斷是否為空
bool full(); //判斷棧是否為滿
void enter(ElementType x); //入隊
void out(); //出隊
void getFront(ElementType &x); //取隊頭元素
int getLength(); //求目前隊列中元素個數
void print(); //周遊隊列
private:
ElementType * data;
int front;
int rear;
};
//初始化隊列
template<class ElementType>
Queue<ElementType>::Queue()
{
data=new ElementType[MAXLEN];
front=rear=0;
}
//銷毀隊列
template<class ElementType>
Queue<ElementType>::~Queue()
{
delete [] data;
}
//判斷是否為空
template<class ElementType>
bool Queue<ElementType>::empty()
{
if( front==rear)
{
return true;
}
else
{
return false;
}
}
//判斷棧是否為滿
template<class ElementType>
bool Queue<ElementType>::full()
{
if((rear+1)%MAXLEN==front)
{
return true; //隊滿
}
else{
return false;
}
}
//入隊
template<class ElementType>
void Queue<ElementType>::enter(ElementType x)
{
if(full())
{
cout<<"隊滿,不能入隊!"<<endl;
}
else{
rear=(rear+1)%MAXLEN;
data[rear]=x;
}
}
//出隊
template<class ElementType>
void Queue<ElementType>::out()
{
if(front==rear)
{
cout<<"隊空,不能出隊!"<<endl;
}
else{
front=(front+1)%MAXLEN; //指針後移
}
}
//取隊頭元素
template<class ElementType>
void Queue<ElementType>::getFront(ElementType &x)
{
if(empty())
{
cout<<"隊空,不能取元素!"<<endl;
}
else{
x=data[(front+1)%MAXLEN];
}
}
//求目前隊列中元素個數
template<class ElementType>
int Queue<ElementType>::getLength()
{
return (rear-front+MAXLEN)%MAXLEN;
}
//周遊隊列
template<class ElementType>
void Queue<ElementType>::print()
{
int p=front;
while(rear!=p)
{
p=(p+1)%MAXLEN;
cout<<data[p]<<" ";
}
}
#endif // _QUEUE_H_
#include <iostream>
#include "queue.h"
#include <Cstdlib>
using namespace std;
void algorithm()
{
Queue<int> q;
int i;
cout<<"請輸入一個整數:";
cin>>i;
while(i!=0)
{
if(i%2==0)
{
q.out();
cout<<"此時隊列為:";
q.print();
cout<<endl;
}
else{
q.enter(i);
cout<<"此時隊列為:";
q.print();
cout<<endl;
}
cout<<"請輸入一個整數:";
cin>>i;
}
}
int main()
{
int i,x;
Queue<int> q;
cout<<"*******************************************"<<endl;
cout<<"0退出程式!"<<endl;
cout<<"1判斷是否隊空"<<endl;
cout<<"2判斷是否隊滿"<<endl;
cout<<"3入隊"<<endl;
cout<<"4出隊"<<endl;
cout<<"5取隊頭元素"<<endl;
cout<<"6求目前隊列中元素個數"<<endl;
cout<<"7輸入奇數入隊,偶數出隊"<<endl;
cout<<"8輸出隊列"<<endl;
cout<<"*******************************************"<<endl;
cout<<"請輸入要執行算法的序号:";
cin>>i;
while(i!=0)
{
switch(i)
{
case 1:
if(q.empty())
{
cout<<"隊列為空!"<<endl;
}
else{
cout<<"隊列不為空!"<<endl;
}
break;
case 2:
if(q.full())
{
cout<<"隊列為滿!"<<endl;
}
else{
cout<<"隊列不為滿!"<<endl;
}
break;
case 3:
cout<<"請輸入入隊元素:";
cin>>x;
q.enter(x);
break;
case 4:
q.out();
break;
case 5:
q.getFront(x);
cout<<"隊頭元素為:"<<x<<endl;
break;
case 6:
cout<<"隊列中元素個數為:"<<q.getLength()<<endl;
break;
case 7:
algorithm();
break;
case 8:
q.print();
break;
}
system("PAUSE");
system("CLS");
cout<<"*******************************************"<<endl;
cout<<"0退出程式!"<<endl;
cout<<"1判斷是否隊空"<<endl;
cout<<"2判斷是否隊滿"<<endl;
cout<<"3入隊"<<endl;
cout<<"4出隊"<<endl;
cout<<"5取隊頭元素"<<endl;
cout<<"6求目前隊列中元素個數"<<endl;
cout<<"7輸入奇數入隊,偶數出隊"<<endl;
cout<<"8輸出隊列"<<endl;
cout<<"*******************************************"<<endl;
cout<<"請輸入要執行算法的序号:";
cin>>i;
}
return 0;
}
四、實驗截圖
