資源簡介
ysfGame.zip

代碼片段和文件信息
#include
using?namespace?std;
class?Node?????????//創建雙向循環鏈表的結點類
{
??????friend?class?Doublelist;????//作為友元類
??????friend?void?DoubleJoseph();??//作為友元函數來調用
??????
??????public:
?????????????Node();
?????????????int?data;
?????????????Node?*prior;
?????????????Node?*next;
???????private:
};
class?Doublelist?????//雙向循環鏈表類
{
??????friend?void?DoubleJoseph();????//作為友元函數來調用
??????public:
?????????????void?Creatlist(Doublelist?&L);???//雙向循環鏈表
?????????????int?getLength(Doublelist?&L);????//用于獲取雙向鏈表的長度
?????????????Doublelist();
??????private:
??????????????Node?*Head;
};
Node::Node()?????//Doublelist類的構造函數初始化結點數據
{
????????????data?=?0;
????????????prior?=?NULL;
????????????next?=?NULL;
}
Doublelist::Doublelist()????//Doublelist類的構造函數初始化鏈表數據
{
????????????????????????Head?=?NULL;
}
void?Doublelist::Creatlist(Doublelist?&L)???//建立雙向循環鏈表
{
?????cout?<“\t\t請輸入雙向生死游戲的總人數N:“;
?????int?n;
?????cin?>>?n;
?????Node?*p*s;
?????for(int?i=1;i<=n;i++)
?????{
?????????????p=new?Node;?????//構造頭結點
?????????????p->data=i;
?????????????p->next=NULL;??
?????????????if(i==1)
?????????????{
??????????????????L.Head=p;?
??????????????????p->prior=NULL;
??????????????????s=L.Head;
?????????????}
?????????????else
?????????????{
??????????????????s->next=p;
??????????????????p->prior=s;
??????????????????s=s->next;
?????????????}??
?????}
?????s->next?=?L.Head;
?????L.Head->prior?=?p;
}
int?Doublelist::getLength(Doublelist?&L)???//獲取雙向鏈表的長度
{
????Node?*p?=?L.Head;
????int?count?=?0;
????while(p->next?!=?L.Head)
????{
??????????????????count++;
??????????????????p?=?p->next;
????}
????count++;
????return?count;
}
void?DoubleJoseph()???//實現約瑟夫雙向生死游戲
{
?????Doublelist?L;???//定義鏈表
?????L.Creatlist(L);???//建立鏈表
?Node?*p*q*r;??//定義指針
?????p?=?L.Head;????//定義頭結點
?????cout?<“\t\t請輸入游戲開始的位置S:“;??//輸入開始報數位置S
?????int?s;
?????cin?>>?s;
?????cout?<“\t\t請輸入正向的死亡數字M:“;???//輸入正向死亡位置M
?????int?m;
?????cin?>>?m;??
?????cout?<“\t\t請輸入逆向的死亡數字W:“;???//輸入逆向死亡位置W
?????int?w;
?????cin?>>?w;
?????cout?<“\t\t請輸入剩余的生者人數“;?????//輸入剩余生存人數K
?????int?k;
?????cin?>>?k;
?????cout?< ?
?????int?t?=?1;????//初始定義t值
?????while(?k ?????{
??????????if(t%2)???//判斷正向逆向
??????????{?
???????????????for(int?j=0;?j ?{
?????????????????????????q=p;
?????????????????????????p=p->next;
?????????????????}
???????????????if(p?==?L.Head)?????//元素定位出列?
?????????????????{
?????????????????????????r=p;
?????????????????????????L.Head?=?p->next;??//指向下一元素結點
?????????????????????????q->next?=?p->next;?????????//依次更換元素結點
?????????????????????????p->next->prior?=?q;
?????????????????????????p?=?p->next;
?????????????????}?????????????
??????????????else???
??{
?????????????????????r?=?p;
?????????????????????q->next?=?p->next;
?????????????????????p->next->prior?=?q;
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????5121??2017-06-30?21:08??ysf\ysfg.cpp
?????文件??????477930??2019-01-08?19:24??ysf\報告.docx
評論
共有 條評論