C++ 中循環(huán)鏈表和約瑟夫環(huán)
循環(huán)鏈表和約瑟夫環(huán)
循環(huán)鏈表的實現(xiàn)
單鏈表只有向后結點,當單鏈表的尾鏈表不指向NULL,而是指向頭結點時候,形成了一個環(huán),成為單循環(huán)鏈表,簡稱循環(huán)鏈表。當它是空表,向后結點就只想了自己,這也是它與單鏈表的主要差異,判斷node->next是否等于head。
代碼實現(xiàn)分為四部分:
- 初始化
- 插入
- 刪除
- 定位尋找
代碼實現(xiàn):
void ListInit(Node *pNode){
int item;
Node *temp,*target;
cout<<"輸入0完成初始化"<<endl;
while(1){
cin>>item;
if(!item)
return ;
if(!(pNode)){ //當空表的時候,head==NULL
pNode = new Node ;
if(!(pNode))
exit(0);//未成功申請
pNode->data = item;
pNode->next = pNode;
}
else{
//
for(target = pNode;target->next!=pNode;target = target->next)
;
temp = new Node;
if(!(temp))
exit(0);
temp->data = item;
temp->next = pNode;
target->next = temp;
}
}
}
void ListInsert(Node *pNode,int i){ //參數(shù)是首節(jié)點和插入位置
Node *temp;
Node *target;
int item;
cout<<"輸入您要插入的值:"<<endl;
cin>>item;
if(i==1){
temp = new Node;
if(!temp)
exit(0);
temp->data = item;
for(target=pNode;target->next != pNode;target = target->next)
;
temp->next = pNode;
target->next = temp;
pNode = temp;
}
else{
target = pNode;
for (int j=1;j<i-1;++j)
target = target->next;
temp = new Node;
if(!temp)
exit(0);
temp->data = item;
temp->next = target->next;
target->next = temp;
}
}
void ListDelete(Node *pNode,int i){
Node *target,*temp;
if(i==1){
for(target=pNode;target->next!=pNode;target=target->next)
;
temp = pNode;//保存一下要刪除的首節(jié)點 ,一會便于釋放
pNode = pNode->next;
target->next = pNode;
delete temp;
}
else{
target = pNode;
for(int j=1;j<i-1;++j)
target = target->next;
temp = target->next;//要釋放的node
target->next = target->next->next;
delete temp;
}
}
int ListSearch(Node *pNode,int elem){ //查詢并返回結點所在的位置
Node *target;
int i=1;
for(target = pNode;target->data!=elem && target->next!= pNode;++i)
target = target->next;
if(target->next == pNode && target->data!=elem)
return 0;
else return i;
}
約瑟夫問題
約瑟夫環(huán)(約瑟夫問題)是一個數(shù)學的應用問題:已知n個人(以編號1,2,3...n分別表示)圍坐在一張圓桌周圍。從編號為k的人開始報數(shù),數(shù)到m的那個人出列;他的下一個人又從1開始報數(shù),數(shù)到m的那個人又出列;依此規(guī)律重復下去,直到圓桌周圍的人全部出列。這類問題用循環(huán)列表的思想剛好能解決。
注意:編寫代碼的時候,注意報數(shù)為m = 1的時候特殊情況
#include<iostream>
#include<cstdio>
using namespace std;
typedef struct Node{
int data;
Node *next;
};
Node *Create(int n){
Node *p = NULL, *head;
head = new Node;
if (!head)
exit(0);
p = head; // p是當前指針
int item=1;
if(n){
int i=1;
Node *temp;
while(i<=n){
temp = new Node;
if(!temp)
exit(0);
temp->data = i++;
p->next = temp;
p = temp;
}
p->next = head->next;
}
delete head;
return p->next;
}
void Joseph(int n,int m){
//n為總人數(shù),m為數(shù)到第m個的退出
m = n%m;
Node *start = Create(n);
if(m){//如果取余數(shù)后的m!=0,說明 m!=1
while(start->next!=start){
Node *temp = new Node;
if(!temp)
exit(0);
for(int i=0;i<m-1;i++) // m = 3%2 = 1
start = start->next;
temp = start->next;
start->next = start->next->next;
start = start->next;
cout<<temp->data<<" ";
delete temp;
}
}
else{
for(int i=0;i<n-1;i++){
Node *temp = new Node;
if(!temp)
exit(0);
cout<<start->data<<" ";
temp = start;
start = start->next;
delete temp;
}
}
cout<<endl;
cout<<"The last person is:"<<start->data<<endl;
}
int main(){
Joseph(3,1);
Joseph(3,2);
return 0;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:C/C++ 連接MySql數(shù)據(jù)庫的方法
欄 目:C語言
下一篇:C語言數(shù)據(jù)結構之 折半查找實例詳解
本文標題:C++ 中循環(huán)鏈表和約瑟夫環(huán)
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1447.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關鍵字的使用詳解
- 01-10深入C/C++浮點數(shù)在內存中的存儲方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設置


