#include<iostream>
using namespace std;
const int N=10;
int q[N];
int rear,front;
void CreatCQ() //建立一個環狀佇列(Circular Queue)
{
rear=front=N-1; //將rear指標與front指標指在同一個起始位子
}
bool IsEmptyCQ()//檢查佇列裡是不是空的(亦即沒有資料)
{
return (rear==front);//rear與front在同一位子上則為為真
}
bool IsFullCQ()//檢查佇列是不是滿的
{
return (rear+1)%N==front; //因為為環狀佇列....請自行畫圖 PS:此種做法會造成無法分辨空或滿!!!!
}
void AddCQ(int item)//加入新資料
{
if(IsFullCQ())cout<<"Queue is full!!"<<endl;
rear=(rear+1)%N; //將rear往前一位
q[rear]=item;
}
int DeleteCQ()
{
if(IsEmptyCQ())cout<<"Queue is empty"<<endl;
front=(front+1)%N; //將front往前一位
return q[front];
}
void main(){
CreatCQ();
for(int i=10;i>1;i--)
AddCQ(i);
cout<<DeleteCQ()<<endl;
cout<<DeleteCQ()<<endl;
AddCQ(12);
cout<<q[rear]<<endl;
cout<<q[front]<<endl;
system("pause");
}
- Nov 15 Tue 2011 13:26
環狀佇列(Circular Queue)
close
全站熱搜
留言列表
發表留言