#include<iostream>
using namespace std;
const int Size=5;
int q[Size];
int Left,Right;
bool left_end=0;//預設為右邊
void CreatCdeq()
{
Right=0;
Left=1;
}
bool IsEmptyCdeq()
{
if((Right+1)%Size==Left)
return true;
else
return false;
}
bool IsFullCdeq()
{
if((Right+2)%Size==Left)
return true;
else
return false;
}
void AddCdeq( bool left_end , int item )
{
if(IsFullCdeq()) //此時的環狀柱列不能全滿,全滿時會分不清楚全滿或全空!
cout<<"the Cdeq is full"<<endl;
if(!IsFullCdeq() && left_end)
q[Left=(Left-1+Size)%Size]=item;
else if(!IsFullCdeq() && !left_end)
q[Right=(Right+1)%Size]=item;
}
int DelCdeq( bool left_end )
{
int x;
if(IsEmptyCdeq())
cout<<"the Cdeq is empty"<<endl;
if(!IsEmptyCdeq() && left_end)
{
x=q[Left];
Left=(++Left)%Size;
return x;
}
else if(!IsEmptyCdeq() && left_end)
{
x=q[Right];
Right=(--Right+Size)%Size;
return x;
}
}
void main()
{
CreatCdeq();
AddCdeq(left_end,10);
cout<<IsFullCdeq()<<" Right="<<Right<<"Left="<<Left<<endl;
AddCdeq(left_end,10);
cout<<IsFullCdeq()<<" Right="<<Right<<"Left="<<Left<<endl;
AddCdeq(left_end,10);
cout<<IsFullCdeq()<<" Right="<<Right<<"Left="<<Left<<endl;
AddCdeq(left_end,10);
cout<<IsFullCdeq()<<" Right="<<Right<<"Left="<<Left<<endl;
for(int i=0;i<5;i++)
cout<<q[i]<<" ";
system("pause");
}
- Nov 18 Fri 2011 09:56
[資料結構] Circular Double Ended Queue
close
全站熱搜
留言列表