C語言的循環鏈表和約瑟夫環
約瑟夫問題)是一個數學的應用問題,對於學習C語言四非常挺有幫助的,下面是小編為大家蒐集整理出來的有關於C語言的循環鏈表和約瑟夫環,一起了解下吧!
循環鏈表的實現單鏈表只有向後結點,當單鏈表的尾鏈表不指向NULL,而是指向頭結點時候,形成了一個環,成為單循環鏈表,簡稱循環鏈表。當它是空表,向後結點就只想了自己,這也是它與單鏈表的主要差異,判斷node->next是否等於head。
代碼實現分為四部分:
1. 初始化
2. 插入
3. 刪除
4. 定位尋找
代碼實現:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 | void ListInit(Node *pNode){
int item;
Node *temp,*target;
cout<< "輸入0完成初始化" <<endl; 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){ //參數是首節點和插入位置
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; //保存一下要刪除的首節點 ,一會便於釋放
pNode = pNode->next;
target->next = pNode;
temp;
}
else {
target = pNode;
for ( int j= 1 ;j<i- 1 ;++j) target= "target-" >next;
temp = target->next; //要釋放的node
target->next = target->next->next;
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; }</i- 1 ;++j)></i- 1 ;++j)></endl;></endl;> |
約瑟夫環(約瑟夫問題)是一個數學的應用問題:已知n個人(以編號1,2,3…n分別表示)圍坐在一張圓桌周圍。從編號為k的人開始報數,數到m的那個人出列;他的下一個人又從1開始報數,數到m的`那個人又出列;依此規律重複下去,直到圓桌周圍的人全部出列。這類問題用循環列表的思想剛好能解決。
注意:編寫代碼的時候,注意報數為m = 1的時候特殊情況
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 | #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;
}
head;
return p->next; } void Joseph( int n, int m){
//n為總人數,m為數到第m個的退出
m = n%m;
Node *start = Create(n);
if (m){ //如果取餘數後的m!=0,説明 m!=1
while (start->next!=start){
Node *temp = new Node;
if (!temp)
exit( 0 );
for ( int i= 0 ;i<m- 1 ;i++) 1 = "" m= "3%2" start= "start-" >next;
temp = start->next;
start->next = start->next->next;
start = start->next;
cout<<temp->data<< " " ;
temp;
}
}
else {
for ( int i= 0 ;i<n- 1 ;i++){ node= "" start-= "" temp= "new" >data<< " " ;
temp = start;
start = start->next;
temp;
}
}
cout<<endl; last= "" person= "" start-= "" the= "" >data<<endl; int = "" pre= "" return = "" >
</endl;></endl;></n- 1 ;i++){></temp-></m- 1 ;i++)></cstdio></iostream> |
-
c#實現sunday算法實例
Sunday算法思想跟BM算法很相似,在匹配失敗時關注的是文本串中參加匹配的最末位字符的下一位字符,下面小編為大家整理了c#實現sunday算法實例,希望能幫到大家!因正則表達式搜索總是出現死循環,開始考慮改為其他搜索方式,因為自帶的IndexOf默認只能找到第一個或最後一...
-
C語言數據結構中棧操作實驗大綱
c語言中棧是一種數據結構,後進先出,即最後進入棧的數據最先彈出。以下是本站小編搜索整理的關於C語言數據結構中棧操作實驗,需要的`朋友可以參考一下!想了解更多相關信息請持續關注我們應屆畢業生考試網!實驗:編寫一個程序實現順序棧的各種基本運算,並在此基礎上設...
-
計算機二級C語言上機程序填空題
考慮到一部分同學的計算機二級C語言程序填空題基礎比較薄弱,為此本站小編為大家整理了計算機二級C語言上機程序填空題,希望能夠幫助到同學們。一、程序填空題1、給定程序中,函數fun的功能是根據形參i的值返回某個函數的值。當調用正確時,程序輸出:x1=5.000000,x2=3...
-
Swift與C語言指針結合使用實例
引導語:Swift可與Object-C共同運行於MACOS和iOS平台,用於搭建基於蘋果平台的應用程序。以下是本站小編分享給大家的Swift與C語言指針結合使用實例,歡迎閲讀!####用以輸入/輸出的參數指針C和Objective-C並不支持多返回值,所以CocoaAPI中常常將指針作為一種在方法間...