欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間: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)時(shí)候,形成了一個(gè)環(huán),成為單循環(huán)鏈表,簡(jiǎ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)空表的時(shí)候,head==NULL 
      pNode = new Node ;
      if(!(pNode))
        exit(0);//未成功申請(qǐng) 
      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ǎng)h除的首節(jié)點(diǎn) ,一會(huì)便于釋放 
    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;
}

約瑟夫問(wèn)題

約瑟夫環(huán)(約瑟夫問(wèn)題)是一個(gè)數(shù)學(xué)的應(yīng)用問(wèn)題:已知n個(gè)人(以編號(hào)1,2,3...n分別表示)圍坐在一張圓桌周圍。從編號(hào)為k的人開始報(bào)數(shù),數(shù)到m的那個(gè)人出列;他的下一個(gè)人又從1開始報(bào)數(shù),數(shù)到m的那個(gè)人又出列;依此規(guī)律重復(fù)下去,直到圓桌周圍的人全部出列。這類問(wèn)題用循環(huán)列表的思想剛好能解決。

注意:編寫代碼的時(shí)候,注意報(bào)數(shù)為m = 1的時(shí)候特殊情況

#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個(gè)的退出
  m = n%m;
  
  Node *start = Create(n);
  
  if(m){//如果取余數(shù)后的m!=0,說(shuō)明 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;
}

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

相關(guān)文章

  • C++?Protobuf實(shí)現(xiàn)接口參數(shù)自動(dòng)校驗(yàn)詳解

    C++?Protobuf實(shí)現(xiàn)接口參數(shù)自動(dòng)校驗(yàn)詳解

    用C++做業(yè)務(wù)發(fā)開的同學(xué)是否還在不厭其煩的編寫大量if-else模塊來(lái)做接口參數(shù)校驗(yàn)?zāi)??今天,我們就模擬Java里面通過(guò)注解實(shí)現(xiàn)參數(shù)校驗(yàn)的方式來(lái)針對(duì)C++?protobuf接口實(shí)現(xiàn)一個(gè)更加方便、快捷的參數(shù)校驗(yàn)自動(dòng)工具,希望對(duì)大家有所幫助
    2023-04-04
  • C語(yǔ)言中break與continue的用法和區(qū)別詳解

    C語(yǔ)言中break與continue的用法和區(qū)別詳解

    當(dāng)我們使用while或for循環(huán)時(shí),如果想提前結(jié)束循環(huán)(在不滿足結(jié)束條件的情況下結(jié)束循環(huán)),可以使用break或continue關(guān)鍵字,這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中break與continue的用法和區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • C++之OpenCV圖像高光調(diào)整具體流程

    C++之OpenCV圖像高光調(diào)整具體流程

    PS中的高光命令是一種校正由于太接近相機(jī)閃光燈而有些發(fā)白的焦點(diǎn)的方法,對(duì)高光區(qū)和非高光區(qū)的邊緣作平滑處理,接下來(lái)通過(guò)本文給大家分享C++之OpenCV圖像高光調(diào)整具體流程,感興趣的朋友一起看看吧
    2021-09-09
  • 詳解C語(yǔ)言中printf輸出的相關(guān)函數(shù)

    詳解C語(yǔ)言中printf輸出的相關(guān)函數(shù)

    這篇文章主要介紹了C語(yǔ)言中printf輸出的相關(guān)函數(shù)總結(jié),是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-08-08
  • C++中strtok()函數(shù)的用法介紹

    C++中strtok()函數(shù)的用法介紹

    以下是對(duì)C++中strtok()函數(shù)的使用方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下
    2013-09-09
  • C++使用UDP通訊的實(shí)現(xiàn)示例

    C++使用UDP通訊的實(shí)現(xiàn)示例

    本文實(shí)現(xiàn)對(duì)C++使用UDP做了簡(jiǎn)單封裝,實(shí)現(xiàn)通訊,包括服務(wù)端和客戶端,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • C語(yǔ)言代碼實(shí)現(xiàn)通訊錄管理系統(tǒng)

    C語(yǔ)言代碼實(shí)現(xiàn)通訊錄管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言代碼實(shí)現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C++?JSON庫(kù)?nlohmann::basic_json::accept的用法解析

    C++?JSON庫(kù)?nlohmann::basic_json::accept的用法解析

    nlohmann::basic_json::accept 是 Nlohmann JSON 庫(kù)中的一個(gè)方法,它用于檢查一個(gè)字符串是否可以解析為有效的 JSON,這篇文章主要介紹了C++?JSON庫(kù)nlohmann::basic_json::accept的用法,需要的朋友可以參考下
    2023-06-06
  • C/C++內(nèi)存管理基礎(chǔ)與面試

    C/C++內(nèi)存管理基礎(chǔ)與面試

    本章主要介紹C語(yǔ)言與C++的內(nèi)存管理,以C++的內(nèi)存分布作為引入,介紹C++不同于C語(yǔ)言的內(nèi)存管理方式(new?delete對(duì)比?malloc?free),感興趣的朋友來(lái)看看吧
    2022-07-07
  • C++讀取配置文件的示例代碼

    C++讀取配置文件的示例代碼

    這篇文章主要介紹了C++讀取配置文件的示例代碼,幫助大家更好的理解和學(xué)習(xí)C++開發(fā),感興趣的朋友可以了解下
    2020-08-08

最新評(píng)論