最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ 中循環(huán)鏈表和約瑟夫環(huán)

 更新時間:2017年06月16日 11:46:58   投稿:lqh  
這篇文章主要介紹了C++ 中循環(huán)鏈表和約瑟夫環(huán)的相關(guān)資料,需要的朋友可以參考下

循環(huán)鏈表和約瑟夫環(huán)

循環(huán)鏈表的實(shí)現(xiàn)

單鏈表只有向后結(jié)點(diǎn),當(dāng)單鏈表的尾鏈表不指向NULL,而是指向頭結(jié)點(diǎn)時候,形成了一個環(huán),成為單循環(huán)鏈表,簡稱循環(huán)鏈表。當(dāng)它是空表,向后結(jié)點(diǎn)就只想了自己,這也是它與單鏈表的主要差異,判斷node->next是否等于head。

代碼實(shí)現(xiàn)分為四部分:

  1. 初始化
  2. 插入
  3. 刪除
  4. 定位尋找

代碼實(shí)現(xiàn):

void ListInit(Node *pNode){
  int item;
  Node *temp,*target;
  cout<<"輸入0完成初始化"<<endl;
  
  while(1){
    cin>>item;
    if(!item)
      return ;
    if(!(pNode)){ //當(dāng)空表的時候,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é)點(diǎn)和插入位置 
  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é)點(diǎn) ,一會便于釋放 
    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){ //查詢并返回結(jié)點(diǎn)所在的位置 
  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ù)學(xué)的應(yīng)用問題:已知n個人(以編號1,2,3...n分別表示)圍坐在一張圓桌周圍。從編號為k的人開始報數(shù),數(shù)到m的那個人出列;他的下一個人又從1開始報數(shù),數(shù)到m的那個人又出列;依此規(guī)律重復(fù)下去,直到圓桌周圍的人全部出列。這類問題用循環(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是當(dāng)前指針 
  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為總?cè)藬?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;
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • QT實(shí)現(xiàn)動態(tài)時鐘

    QT實(shí)現(xiàn)動態(tài)時鐘

    這篇文章主要為大家詳細(xì)介紹了QT實(shí)現(xiàn)動態(tài)時鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • 詳解Matlab實(shí)現(xiàn)動態(tài)表白圖的繪制

    詳解Matlab實(shí)現(xiàn)動態(tài)表白圖的繪制

    這篇文章主要利用Matlab實(shí)現(xiàn)繪制獨(dú)特的表白動圖,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定的幫助,感興趣的小伙伴可以了解一下
    2022-05-05
  • c++?對象分配在棧上還是在堆上問題分析

    c++?對象分配在棧上還是在堆上問題分析

    這篇文章主要為大家介紹了c++?對象在棧上還是在堆上問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • C++使用ZLIB庫實(shí)現(xiàn)目錄壓縮與解壓功能

    C++使用ZLIB庫實(shí)現(xiàn)目錄壓縮與解壓功能

    在軟件開發(fā)和數(shù)據(jù)處理中,對數(shù)據(jù)進(jìn)行高效的壓縮和解壓縮是一項(xiàng)重要的任務(wù),這不僅有助于減小數(shù)據(jù)在網(wǎng)絡(luò)傳輸和存儲中的占用空間,還能提高系統(tǒng)的性能和響應(yīng)速度,本文將介紹如何使用 zlib 庫進(jìn)行數(shù)據(jù)的壓縮和解壓縮,以及如何保存和讀取壓縮后的文件
    2025-02-02
  • C語言將24小時制轉(zhuǎn)換為12小時制的方法

    C語言將24小時制轉(zhuǎn)換為12小時制的方法

    這篇文章主要介紹了C語言將24小時制轉(zhuǎn)換為12小時制的方法,涉及C語言針對時間的相關(guān)操作技巧,非常簡單實(shí)用,需要的朋友可以參考下
    2015-07-07
  • c++中的string常用函數(shù)用法總結(jié)

    c++中的string常用函數(shù)用法總結(jié)

    以下是對c++中的string常用函數(shù)的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下
    2013-09-09
  • Java?C++?leetcode面試零矩陣

    Java?C++?leetcode面試零矩陣

    這篇文章主要為大家介紹了Java?C++題解leetcode面試零矩陣示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 淺談VS中添加頭文件時顯示無法找到文件的問題

    淺談VS中添加頭文件時顯示無法找到文件的問題

    下面小編就為大家?guī)硪黄獪\談VS中添加頭文件時顯示無法找到文件的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • C++無try-catch的異常捕獲示例詳解

    C++無try-catch的異常捕獲示例詳解

    這篇文章主要為大家介紹了C++無try-catch的異常捕獲示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • C++中的erase()函數(shù)用法小結(jié)

    C++中的erase()函數(shù)用法小結(jié)

    本文主要介紹了C++中的erase()函數(shù)用法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03

最新評論

东台市| 武义县| 梁平县| 夏邑县| 乌拉特中旗| 永善县| 通许县| 磴口县| 航空| 手机| 西和县| 法库县| 张家港市| 贵港市| 治多县| 扶余县| 雷州市| 大荔县| 班玛县| 江永县| 习水县| 嘉兴市| 修水县| 阜康市| 鲁山县| 屏边| 琼结县| 宝山区| 轮台县| 洛隆县| 阜宁县| 台江县| 安仁县| 临邑县| 鄂伦春自治旗| 伊通| 纳雍县| 葵青区| 喜德县| 伊宁市| 新营市|