千家信息网

C++中如何实现循环链表和约瑟夫环

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,本篇内容介绍了"C++中如何实现循环链表和约瑟夫环"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!循环
千家信息网最后更新 2026年11月04日C++中如何实现循环链表和约瑟夫环

本篇内容介绍了"C++中如何实现循环链表和约瑟夫环"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

循环链表和约瑟夫环

循环链表的实现

单链表只有向后结点,当单链表的尾链表不指向NULL,而是指向头结点时候,形成了一个环,成为单循环链表,简称循环链表。当它是空表,向后结点就只想了自己,这也是它与单链表的主要差异,判断node->next是否等于head。

代码实现分为四部分:

  1. 初始化

  2. 插入

  3. 删除

  4. 定位寻找

代码实现:

void ListInit(Node *pNode){  int item;  Node *temp,*target;  cout<<"输入0完成初始化"<>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<<"输入您要插入的值:"<>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;jnext;    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;    delete temp;   }  else{    target = pNode;    for(int j=1;jnext;    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;}

约瑟夫问题

约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。这类问题用循环列表的思想刚好能解决。

注意:编写代码的时候,注意报数为m = 1的时候特殊情况

#include#includeusing 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为总人数,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;inext;      temp = start->next;      start->next = start->next->next;      start = start->next;      cout<data<<" ";      delete temp;    }  }  else{    for(int i=0;idata<<" ";      temp = start;      start = start->next;      delete temp;    }  }  cout<data<

"C++中如何实现循环链表和约瑟夫环"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

约瑟 约瑟夫 环链 时候 结点 问题 代码 C++ 个人 位置 内容 圆桌 情况 更多 知识 节点 输入 实用 特殊 成功 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广州百单互联网科技有限公司 互联网科技公司的优势 互联网科技有限公司经营范围 互联网巨头反垄断金融科技 互联网科技与金融科技 高科技与互联网 以房互联网科技 广州宏创互联网科技 深圳卡尔迅互联网科技有限公司怎么样 互联网教育科技公司vipkid 剑昆互联网络科技有限公司临桂 广州市恰铎互联网科技有限公司 洛阳昕诺飞互联网科技有限公司 民营互联网公司国防科技 畅思互联网科技 沐思想互联网科技有限公司 伍佰万互联网科技怎么赚钱 中国科技和美国互联网 科技信息时代互联网发展 科技互联网专业务虚会材料 天津市摩尔互联网科技 奇酷互联网科技 互联网与新科技 世界互联网领先科技成果 狮子座互联网科技有限公司 甘肃拓世互联网科技有限公司 深圳国际航运科技和互联网 世界互联网领先科技成果获奖 保山浩联互联网科技有限公司 中国主要互联网科技公司
0