鱼C论坛

 找回密码
 立即注册
查看: 2463|回复: 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
#include <stdio.h>
typedef struct loop//定义 
{
        int num;
        struct loop *next;
} node;

int main()
{
        node *head=NULL;
        int n;
        for(i=1;i<=n;i++)//store
        {
                node *p=(node*)malloc(sizeof(node));//将每个数字存入对应位置 
                p->num=i;
                p->next=NULL;
                node *last=head;
                if(last)
                {
                        while(last->next){
                                last=last->next;
                        }
                        last->next=p;
                }
                else
                {
                        head=p;
                }
        } 
        node *q;
        while(n>0)//删除元素(不完整) 
        {
                k++;
                if(k==3)
                {
                        for(q=NULL,p=list.head;p;q=p,p=p->next)
                        {
                                if(q){
                                        q->next=p.next;
                                }
                                else{
                                        list.head=p->next;
                                }
                                free (p);
                                break;
                        }
                }
        }
}
第一次学习链表,学着创建了一个链表,但不知如何删除链表?以上是部分代码,之后就写不下去了

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

int main()
{
        node *head=NULL;
        int n=10;////////
        for(int i=1;i<=n;i++)//store
        {
                node *p=(node*)malloc(sizeof(node));//将每个数字存入对应位置
                p->num=i;
                p->next=NULL;
                node *last=head;
                if(last)
                {
                        while(last->next){
                                last=last->next;
                        }
                        last->next=p;
                }
                else
                {
                        head=p;
                }
                if(i==n)p->next=head;////环
        }

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

                if(k==3)
                {
                    printf("%d\n",head->num);
                    q->next=head->next;
                    free(head);
                    head=q->next;
                    //k=0;
                    k=1
                }
                else
                {
                    q=head;
                    head=head->next;
                   k+=1
                }
                //k++;

        }
        printf("%d\n",head->num);
        free(head);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

int main()
{
        node *head=NULL;
        int n=10;////////
        for(int i=1;i<=n;i++)//store
        {
                node *p=(node*)malloc(sizeof(node));//将每个数字存入对应位置
                p->num=i;
                p->next=NULL;
                node *last=head;
                if(last)
                {
                        while(last->next){
                                last=last->next;
                        }
                        last->next=p;
                }
                else
                {
                        head=p;
                }
                if(i==n)p->next=head;////环
        }

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

                if(k==3)
                {
                    printf("%d\n",head->num);
                    q->next=head->next;
                    free(head);
                    head=q->next;
                    //k=0;
                    k=1
                }
                else
                {
                    q=head;
                    head=head->next;
                   k+=1
                }
                //k++;

        }
        printf("%d\n",head->num);
        free(head);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-24 21:34:48 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-7 23:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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