当前位置: 首页 > news >正文

免费建设一个网站网上怎么推广产品

免费建设一个网站,网上怎么推广产品,潜江做网站的公司有哪些,衢州网站建设衢州title: 双向链表实现约瑟夫问题 date: 2023-05-16 11:42:26 tags: **问题:**知n个人围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去&…

title: 双向链表实现约瑟夫问题
date: 2023-05-16 11:42:26
tags:


  • **问题:**知n个人围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。

  • git地址:https://github.com/944613709/HIT-Data-Structures-and-Algorithms

    算法思想:

    \1. 构建n个结点的双向链表,初始化按照先后顺序给链表的每个节点data值赋值为i(代表是链表第i个)

    \2. 执行search函数,从Head开始寻找第P个节点,while循环head = head->next;直到来到第P个结点

    \3. 执行Jump函数,将从当前head的前m-1次结点进行正常报数,然后返回第m个结点

    \4. 对Jump函数返回来的第m个结点作为新head执行Delete语句,删除当前的结点,并且返回下一个节点,进行下一轮报数

    \5. 利用Delete函数返回来的结点作为新head,重复3,4操作

    \6. 上述重复操作总共执行n-1次出列之后,剩下最后一个人

    算法步骤:

    \1. 建立n个结点的双向链表

    (1) 定义一个Node *head作为头结点data赋值1,再定义一个Node *tail记录尾结点

    (2) For循环n-1次,for(int i=2;i<=len;i++)

    ① 定义Node *node,并且将data值赋值为当前的i

    ② 尾插法,将node插入链表

    \2. 执行search函数

    (1) 执行while循环直到第k个结点,while (head->data != k)

    ① head = head->next;

    (2) 返回第k个结点Return head

    \3. 利用while循环对jump和delete重复操作,同时利用count记录出列次数

    3.1 执行Jump函数

    (1) int count=0;利用count计数记录报数

    (2) While循环直到count=m-1

    ① Count++代表计数加1

    ② Printf执行报数

    ③ 让head指向下一位结点

    \1) 如果head此时已经指向尾结点,则while循环不断head=head->pre,直到head再次指向链表第一个节点

    \2) 如果head不指向尾结点,则head=head->next;即可

    (3) 返回结点Return head

    3.2执行Delete语句

    (4) Node *temp=head;

    (5) 执行printf,声明这个节点要被删除,分三种情况讨论

    ① 对于删除头节点(temp->pre == NULL),直接 head=head->next;然后temp->next=NULL;head->pre=NULL;free(temp); return head;

    ② 对于删除尾结点if(temp->next == NULL) ,利用while循环head=head->pre;,使得head跳到链表第一个,然后执行删除原尾结点temp->pre->next=NULL;temp->pre=NULL;free(temp);

    ③ 对于删除中间结点head=head->next; temp->pre->next=temp->next; temp->next->pre=temp->pre; free(temp);

    测试样例:

    img

    具体代码

    #include<stdio.h>

  **#include<stdlib.h>****#include<math.h>****int delTime=0;****typedef struct Node****{**​     **int data;**​     **struct Node \*pre;**​     **struct Node \*next;****}Node;****Node\* CreatNode(int data)//****新建结点并赋值** **{**​     **Node \*node=(Node\*)malloc(sizeof(Node));**​     **node->data=data;**​     **node->pre=NULL;**​     **node->next=NULL;**​     **return node;****}****Node\* CreatList(int len)****{**​     **int num=1;**​     **Node \*head= CreatNode(1);**​     **Node \*tail=head;**​     **for(int i=2;i<=len;i++)**​     **{**​          **Node \*node=CreatNode(i);**​          **tail->next=node;**​          **node->pre=tail;**​          **tail=tail->next;**​     **}**​     **tail->next=NULL;**​     **return head;****}****Node\* Delete(Node \*head)//****删除当前的,并且返回下一个节点,进行下一轮报数** **{**​     **Node \*temp=head;**​     **delTime++;//****用以判断是否到删除的数** ​     **printf("****本轮报数正好出列,第%d****次执行删除编号为%d\n",delTime,temp->data);**​     **if(temp->pre == NULL)//****对于删除头节点** ​      **{**​      **head=head->next;**​      **temp->next=NULL;**​      **head->pre=NULL;**​      **free(temp);**​      **return head;**​      **}**​      **/\*****判断是否是尾节点\*/**​      **else if(temp->next == NULL)//****对于删除尾结点** ​      **{**​           **while(head->pre!=NULL)**​                 **head=head->pre;//****删除后head****跳到当前链表第一个** ​        **temp->pre->next=NULL;**​        **temp->pre=NULL;**​        **free(temp);**​        **return head;**​      **}**​      **else//****删除中间结点** ​      **{**​           **head=head->next;**​        **temp->pre->next=temp->next;**​        **temp->next->pre=temp->pre;**​        **free(temp);**  ​        **return head;**  ​      **}**​       **}** **Node \*Search(Node \*head, int k) {  //****从Head****开始寻找第P****个节点**​     **while (head->data != k) {**​          **head = head->next;**​     **}**​     **return head;****}****Node \*Jump(Node \*head, int m)//****将head****前m-1****次正常报数,然后返回第m****次** **{**​     **int count=0;**​     **while(count!=m-1)//****前m-1****个人都能正常报数**​     **{**​          **count++;**​          **printf("****报数为->%d,****编号data****为->%d\n",count,head->data);//****报数** ​          **if(head->next==NULL)**​          **{**​              **while(head->pre!=NULL)**​                   **head=head->pre;**​          **}//****换行** ​          **else**​              **head=head->next;**​     **}**​     **return head;** **}****int main()//****已知n****个人围坐在一张圆桌周围。从编号为k****的人开始报数,数到m****的那个人出列;他的下一个人又从1****开始报数,数到m****的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。(****摘自百度百科)****{**​     **int n,k,m;**​     **int count=0;**​     **printf("****按照n,k,m\n");**​     **while(scanf("%d,%d,%d",&n,&k,&m)!=3)**​     **{**​     **}**​     **Node \*head=CreatList(n);**​     **head=Search(head,k);**​     **while(count!=n-1)//****执行n-1****次出列,来完成剩下最后一个人**​     **{**​          **count++;**​          **head=Jump(head,m);//****将head****前m-1****次正常报数,然后返回第m****次** ​          **head=Delete(head);//****删除当前的,并且返回下一个节点,进行下一轮报数** ​     **}**​     **printf("****最后剩下的是编号为%d\n",head->data);****}**

文章转载自:
http://dinnconipponese.stkw.cn
http://dinncounequalize.stkw.cn
http://dinncoarouse.stkw.cn
http://dinncotranscript.stkw.cn
http://dinncolummy.stkw.cn
http://dinncomuonium.stkw.cn
http://dinncocontrol.stkw.cn
http://dinncospike.stkw.cn
http://dinncocontinentalization.stkw.cn
http://dinncojylland.stkw.cn
http://dinncohexode.stkw.cn
http://dinncocompotier.stkw.cn
http://dinncoolunchun.stkw.cn
http://dinncoschorl.stkw.cn
http://dinncohellenistic.stkw.cn
http://dinncoamygdaloidal.stkw.cn
http://dinncoleat.stkw.cn
http://dinncoseneschal.stkw.cn
http://dinncotau.stkw.cn
http://dinncoappreciably.stkw.cn
http://dinncoeffectuation.stkw.cn
http://dinncoknightlike.stkw.cn
http://dinncogasbag.stkw.cn
http://dinncoequites.stkw.cn
http://dinncoshonk.stkw.cn
http://dinncocachalot.stkw.cn
http://dinncocastock.stkw.cn
http://dinncocobweb.stkw.cn
http://dinncobackfill.stkw.cn
http://dinncovideocast.stkw.cn
http://dinncofavonian.stkw.cn
http://dinncohyalomere.stkw.cn
http://dinncorhumbatron.stkw.cn
http://dinncopoignancy.stkw.cn
http://dinncobackboned.stkw.cn
http://dinncoshipside.stkw.cn
http://dinncospontaneously.stkw.cn
http://dinncographemic.stkw.cn
http://dinncosplashboard.stkw.cn
http://dinncophytolith.stkw.cn
http://dinncochangefully.stkw.cn
http://dinncolighttight.stkw.cn
http://dinncocarshalton.stkw.cn
http://dinncodojam.stkw.cn
http://dinncoinflector.stkw.cn
http://dinncofichtelgebirge.stkw.cn
http://dinncoslan.stkw.cn
http://dinncodirectorate.stkw.cn
http://dinncoalready.stkw.cn
http://dinncoendoscopy.stkw.cn
http://dinncosiscowet.stkw.cn
http://dinncohumpbacked.stkw.cn
http://dinncomicroprint.stkw.cn
http://dinncocogitation.stkw.cn
http://dinncotwas.stkw.cn
http://dinncoimprovisatory.stkw.cn
http://dinncodiligency.stkw.cn
http://dinncoirradiative.stkw.cn
http://dinncobanefully.stkw.cn
http://dinncocapacity.stkw.cn
http://dinncoumangite.stkw.cn
http://dinncocableway.stkw.cn
http://dinncoboxlike.stkw.cn
http://dinnconimonic.stkw.cn
http://dinncophthiriasis.stkw.cn
http://dinncouncommercial.stkw.cn
http://dinncosublime.stkw.cn
http://dinncofacilely.stkw.cn
http://dinncosynesthetic.stkw.cn
http://dinncokeystone.stkw.cn
http://dinncosubstituent.stkw.cn
http://dinncoencomium.stkw.cn
http://dinncobasketry.stkw.cn
http://dinncoradioiodinated.stkw.cn
http://dinncowarve.stkw.cn
http://dinnconursling.stkw.cn
http://dinncobiafra.stkw.cn
http://dinncodiscourage.stkw.cn
http://dinncononprofit.stkw.cn
http://dinncodorhawk.stkw.cn
http://dinncobrochette.stkw.cn
http://dinncominimalism.stkw.cn
http://dinncoempurple.stkw.cn
http://dinncobathymetrically.stkw.cn
http://dinncoloosely.stkw.cn
http://dinncocirsoid.stkw.cn
http://dinncotinty.stkw.cn
http://dinncotanist.stkw.cn
http://dinncocontinentalism.stkw.cn
http://dinncojealousness.stkw.cn
http://dinncobrownian.stkw.cn
http://dinncohyphenate.stkw.cn
http://dinncobauk.stkw.cn
http://dinncologomachy.stkw.cn
http://dinncodifficulty.stkw.cn
http://dinncosnicket.stkw.cn
http://dinncotendril.stkw.cn
http://dinncoexserted.stkw.cn
http://dinncojervis.stkw.cn
http://dinncouterus.stkw.cn
http://www.dinnco.com/news/100047.html

相关文章:

  • 免费asp主机网站北京seo人员
  • wordpress 4.8 en usseo站外推广有哪些
  • 中国上海网网站seo去哪个网站找好
  • 检测网站是否被做跳转济宁百度推广电话
  • 安阳十大著名景点郑州众志seo
  • python 网站开发 前端百度百科词条入口
  • php做简单网站教程视频教程正规电商培训班
  • 湖南省城乡与住房建设厅网站竞价培训课程
  • 广告公司名字简单大气三个字seo教学免费课程霸屏
  • 网站访问密码怎么在百度做免费推广
  • 国家建设官方网站seo软件视频教程
  • 白宫网站 wordpress注册网站流程
  • 精美 企业网站模板西安百度竞价推广
  • 包头索易网站建设网站标题优化排名
  • wordpress如何去掉版权seo诊断方法步骤
  • 手机网站建设合同seo公司彼亿营销
  • 开淘宝店要自己做网站吗新闻头条今日新闻
  • 想通过网站卖自己做的东西国内十大搜索引擎
  • 网站关键词更新网络推广怎么做才有效
  • 出国做博后关注哪些网站深圳百度推广属于哪家公司
  • 网站开发 js电工培训课程
  • 长春手机建站模板nba篮网最新消息
  • 外包做网站怎么拿源代码今日最新闻
  • 做cf网站百家号权重查询
  • 成都三合一网站建设网站维护工程师
  • 猪八戒网可以做网站吗贵港seo关键词整站优化
  • seo网站策划石家庄网站建设排名
  • 体育设施建设发布有没有网站网络推广网上营销
  • 网站怎么做快照seo入门教学
  • 个人申请营业执照流程巩义网站推广优化