鱼C论坛

 找回密码
 立即注册
查看: 2956|回复: 2

[已解决]c语言链表

[复制链接]
发表于 2022-10-24 18:17:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目:n个人围成一圈,从第一个人开始报数,数到 m的人出列,再由下一个人重新从1开始报数,数到 m的人再出圈,依次类推,直到所有的人都出圈,请输出依次出圈人的编号。(用链表)
例如:
输入:10 3
输出:3 6 9 2 7 1 8 5 10 4
  1. #include <stdio.h>
  2. typedef struct loop//定义
  3. {
  4.         int num;
  5.         struct loop *next;
  6. } node;

  7. int main()
  8. {
  9.         node *head=NULL;
  10.         int n;
  11.         for(i=1;i<=n;i++)//store
  12.         {
  13.                 node *p=(node*)malloc(sizeof(node));//将每个数字存入对应位置
  14.                 p->num=i;
  15.                 p->next=NULL;
  16.                 node *last=head;
  17.                 if(last)
  18.                 {
  19.                         while(last->next){
  20.                                 last=last->next;
  21.                         }
  22.                         last->next=p;
  23.                 }
  24.                 else
  25.                 {
  26.                         head=p;
  27.                 }
  28.         }{:10_286:}
  29.         node *q;
  30.         while(n>0)//删除元素(不完整)
  31.         {
  32.                 k++;
  33.                 if(k==3)
  34.                 {
  35.                         for(q=NULL,p=list.head;p;q=p,p=p->next)
  36.                         {
  37.                                 if(q){
  38.                                         q->next=p.next;
  39.                                 }
  40.                                 else{
  41.                                         list.head=p->next;
  42.                                 }
  43.                                 free (p);
  44.                                 break;
  45.                         }
  46.                 }
  47.         }
  48. }
复制代码

第一次学习链表,学着创建了一个链表,但不知如何删除链表?以上是部分代码,之后就写不下去了

最佳答案
2022-10-24 19:08:56
本帖最后由 jhq999 于 2022-10-24 19:14 编辑
  1. #include <stdio.h>
  2. typedef struct loop//定义
  3. {
  4.         int num;
  5.         struct loop *next;
  6. } node;

  7. int main()
  8. {
  9.         node *head=NULL;
  10.         int n=10;////////
  11.         for(int i=1;i<=n;i++)//store
  12.         {
  13.                 node *p=(node*)malloc(sizeof(node));//将每个数字存入对应位置
  14.                 p->num=i;
  15.                 p->next=NULL;
  16.                 node *last=head;
  17.                 if(last)
  18.                 {
  19.                         while(last->next){
  20.                                 last=last->next;
  21.                         }
  22.                         last->next=p;
  23.                 }
  24.                 else
  25.                 {
  26.                         head=p;
  27.                 }
  28.                 if(i==n)p->next=head;////环
  29.         }

  30.         node *q=head;//////
  31.         int k=1;
  32.         while(head->next!=head)//删除元素(不完整)
  33.         {

  34.                 if(k==3)
  35.                 {
  36.                     printf("%d\n",head->num);
  37.                     q->next=head->next;
  38.                     free(head);
  39.                     head=q->next;
  40.                     //k=0;
  41.                     k=1
  42.                 }
  43.                 else
  44.                 {
  45.                     q=head;
  46.                     head=head->next;
  47.                    k+=1
  48.                 }
  49.                 //k++;

  50.         }
  51.         printf("%d\n",head->num);
  52.         free(head);
  53.         return 0;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-24 19:08:56 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-10-24 19:14 编辑
  1. #include <stdio.h>
  2. typedef struct loop//定义
  3. {
  4.         int num;
  5.         struct loop *next;
  6. } node;

  7. int main()
  8. {
  9.         node *head=NULL;
  10.         int n=10;////////
  11.         for(int i=1;i<=n;i++)//store
  12.         {
  13.                 node *p=(node*)malloc(sizeof(node));//将每个数字存入对应位置
  14.                 p->num=i;
  15.                 p->next=NULL;
  16.                 node *last=head;
  17.                 if(last)
  18.                 {
  19.                         while(last->next){
  20.                                 last=last->next;
  21.                         }
  22.                         last->next=p;
  23.                 }
  24.                 else
  25.                 {
  26.                         head=p;
  27.                 }
  28.                 if(i==n)p->next=head;////环
  29.         }

  30.         node *q=head;//////
  31.         int k=1;
  32.         while(head->next!=head)//删除元素(不完整)
  33.         {

  34.                 if(k==3)
  35.                 {
  36.                     printf("%d\n",head->num);
  37.                     q->next=head->next;
  38.                     free(head);
  39.                     head=q->next;
  40.                     //k=0;
  41.                     k=1
  42.                 }
  43.                 else
  44.                 {
  45.                     q=head;
  46.                     head=head->next;
  47.                    k+=1
  48.                 }
  49.                 //k++;

  50.         }
  51.         printf("%d\n",head->num);
  52.         free(head);
  53.         return 0;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-24 21:34:48 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 07:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表