【C语言求助】一道有yi点难的链表题

飞禽走兽卍一2b 2021-05-02 09:57:09
题目在注释里,这题可能有点复杂,有劳各位费心、费眼、费脑了
我目前卡在(b)步,下面就是执行(a)(b)的代码,问题就是 在creat_sort_lianbiao(struct node *head)那里的循环里面会无法执行
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
1.链表与数组综合操作(本题满分100分,每问20分)
a) 生成一个长度100、取值范围在0-300的整数构成的链表,并打印
b) 将其按从小到大的顺序插入到另一个链表,并遍历打印
c) 将这个排好序的链表的结果写入到文件1000_numbers.txt
d) 再从文件读入数据,存储到一个数组,并打印
e) 统计出现频次最多的数字,并打印
*/
struct node
{
int num;
struct node *next;
};
struct node *creat_lianbiao()
{
//生成一个长度100,取值范围在0-300的随机整数构成的链表...Done
//int num;
srand( time(0) );

int i = 0;
struct node *head = NULL, * p = head, *newnode = NULL;
while(1)
{

newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = rand()%301 ;
//printf("%d ",newnode->num);

if(head == NULL)
{
head = newnode;
p = head;
}

else if(head != NULL)
{

p->next = newnode;
p = p->next;
}

i++;

if(i == 100)
{
p->next = NULL;
break;
}

}
//p=head;
return head;
}
//遍历打印链表的函数
void print_lianbiao(struct node *head)
{
struct node *p = head;
int i = 0;
while(p != NULL)
{
i++;
printf("the %dth number: ", i );
printf("%d\n", p->num );
p = p->next;

}
}
/*核心部分:
说明:
1.本题不能在原链表里排序后再插入到另一个链表
2.不能使用链表进行插入
3.我的想法是:循环,找出每次循环的最小值,
把这个最小值所在的结点插入到新建的链表里,同时在原链表里删除该结点
这样在下一次循环的时候就不会找到上次的最小值
4.问题是循环无法执行下去

*/
struct node *creat_sort_lianbiao(struct node *head)
{
struct node *p = head, *head_2 = NULL ,*p_min = head ,*p_2 = head_2;
int min = p_min->num ,i=0;

while(1)//
{
p = head;
min = head->num;
while(1)//每次循环找当前链表里的最小值
{

if(min > p->num)//如果min记录的大于当前值
{
//printf("Done...\n");
min = p->num;
p_min = p;//把当前值的地址给p_min
}
if(p->next == NULL) break; //如果到尾了,break
p = p->next;
}
p = head;

i++;
while(1)//每次循环把原链表的min值所在地址赋给p_2,并将原链表的该结点删除
{

if(p->next == p_min)//循环搜索,如果匹配到p_min
{

p_2->next = (struct node *)malloc(sizeof(struct node)); //开辟节点
p_2->next->next = (struct node *)malloc(sizeof(struct node));//再开辟节点

if(head_2 == NULL)//如果head_2是空链表
{
//printf("Done...\n");
head_2 = p_min;
p->next = p->next->next; //删除原链表中的min值的节点
printf("p_min->num = %d\n",p_min->num);
break;

}
else if(head_2 != NULL)
{

p_2->next = p_min;
p_2 = p_2->next;
p_2->next = NULL;
p->next = p->next->next;
break;

}
}
p = p->next;

}
if(i == 100)//如果执行了100次就结束
{
//printf("Done...\n");
break;
}

}
//printf("Done...\n");
return head_2;



}


void main()
{

printf("第一题:\n");
struct node * head = NULL, * p = head ,* head_2=NULL;

head = creat_lianbiao();//生成一个长度100,取值范围在0-300的随机整数构成的链表...Done
print_lianbiao(head);//打印原链表...Done
putchar('\n');
putchar('\n');
head_2 = creat_sort_lianbiao(head);//生成新的链表
printf("Done...");
print_lianbiao(head_2);//打印新的链表


}

令人无语的结果:
...全文
169 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 6 楼 qzjhjxj的回复:
谢谢你一直以来的帮助
  • 打赏
  • 举报
回复
引用 6 楼 qzjhjxj的回复:
哈哈哈 我这边C语言的洗礼结束了,但是学习不会止步。 最后的期中考题独立完成,爽爽哒 要去学Python了。。。
qzjhjxj 2021-05-04
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
大家先不用回复,我再改改,这个版本甚至都不完善
源代码大师 2021-05-03
  • 打赏
  • 举报
回复
C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html
  • 打赏
  • 举报
回复
供参考~ 欢迎大家找错(滑稽)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
 1.链表与数组综合操作(本题满分100分,每问20分)
 a) 生成一个长度100、取值范围在0-300的整数构成的链表,并打印
 b) 将其按从小到大的顺序插入到另一个链表,并遍历打印
 c) 将这个排好序的链表的结果写入到文件1000_numbers.txt
 d) 再从文件读入数据,存储到一个数组,并打印
 e) 统计出现频次最多的数字,并打印
*/
struct node
{
    int num;
    struct node *next;
};
struct node *creat_lianbiao()
{
    //生成一个长度100,取值范围在0-300的随机整数构成的链表...Done
    //int num;
    srand( time(0) );

    int i = 0;
    struct node  *head = NULL, * p = head, *newnode = NULL;
    while(1)
    {

        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->num = rand()%301 ;
        //printf("%d ",newnode->num);

        if(head == NULL)
        {
            head = newnode;
            p = head;
        }

        else if(head != NULL)
        {

            p->next = newnode;
            p = p->next;
        }

        i++;

        if(i == 100)
        {
            p->next = NULL;
            break;
        }

    }
    //p=head;
    return head;
}
//遍历打印链表的函数
void print_lianbiao(struct node *head)
{
    struct node *p = head;
    int i = 0;
    while(p != NULL)
    {
        i++;
        printf("the %dth number: ", i );
        printf("%d\n", p->num );
        p = p->next;

    }
}
/*核心部分:
  说明:
      1.本题不能在原链表里排序后再插入到另一个链表
      2.不能使用链表进行插入
      3.我的想法是:循环,找出每次循环的最小值,
                    把这个最小值所在的结点插入到新建的链表里,同时在原链表里删除该结点
                    这样在下一次循环的时候就不会找到上次的最小值
      4.问题是循环无法执行下去

*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
struct node *creat_sort_lianbiao(struct node *head)
{
    struct node *p = head, *head_2 = NULL ,*p_min = head ,*p_2 = head_2 ,*newnode_2 = NULL ,*tmpnode = NULL;
    int min = p_min->num ,i = 0;

    while(1)//
    {
        p = head;
        min = head->num;
        p_min = head;
        while(1)//每次循环找当前链表里的最小值
        {

            if(min > p->num)//如果min记录的大于当前值
            {
                //printf("Done...\n");
                min = p->num;
                p_min = p;//把当前值的地址给p_min

            }
            printf("min=%d\n",min);
            if(p->next == NULL) break; //如果到尾了,break
            p = p->next;
        }
        p = head;

        i++;
        while(1)//每次循环把原链表的min值所在地址赋给p_2,并将原链表的该结点删除
        {

            if(p_min == head)
            {



                    newnode_2 = (struct node *)malloc(sizeof(struct node));//开辟节点
                    newnode_2->num = p_min->num;
                    newnode_2->next = NULL;
                   // p_2->next->next = (struct node *)malloc(sizeof(struct node));//再开辟节点

                    if(head_2 == NULL)//如果head_2是空链表
                    {
                        //printf("%d",head_2);
                        head_2 = newnode_2;
                        p_2 = head_2;
                        p_2->next = NULL;

                        tmpnode = head;
                        head = head->next;
                        free(tmpnode);

                        p = head;

                        //printf("%d\n",head_2);
                        printf("p_2->num = %d\n", p_2->num );
                        break;

                    }
                    else if(head_2 != NULL)
                    {
                        //p_2 = head_2;
                        p_2->next = newnode_2;
                        p_2 = p_2->next;
                        p_2->next = NULL;

                        tmpnode = head;
                        head = head->next;
                        free(tmpnode);

                        p = head;

                        printf("p_2->num = %d\n", p_2->num );
                        break;

                    }

            p = p->next;

            }

            else if(p_min != head)
            {


                if(p->next == p_min)//循环搜索,如果匹配到p_min
                {
                    printf("I find it !\n");

                    newnode_2 = (struct node *)malloc(sizeof(struct node));//开辟节点
                    newnode_2->num = p_min->num;
                    newnode_2->next = NULL;
                    // p_2->next->next = (struct node *)malloc(sizeof(struct node));//再开辟节点

                    if(head_2 == NULL)//如果head_2是空链表
                    {
                        //printf("%d",head_2);
                        head_2 = newnode_2;
                        p_2 = head_2;
                        p_2->next = NULL;
                        p->next = p->next->next; //删除原链表中的min值的节点
                        free(p_min);

                        //printf("%d\n",head_2);
                        printf("p_2->num = %d\n", p_2->num );
                        break;

                    }
                    else if(head_2 != NULL)
                    {

                        p_2->next = newnode_2;
                        p_2 = p_2->next;
                        p_2->next = NULL;
                        p->next = p->next->next;
                        free(p_min);

                        printf("p_2->num = %d\n", p_2->num );
                        break;

                    }

                }
                p = p->next;

            }


        }
        printf("The %dth round\n",i);
        if(i == 100)//如果执行了100次就结束
        {
            //printf("Done...\n");
            break;
        }

    }
    //printf("Done...\n");
    return head_2;



}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void main()
{
    printf("This is the mid-term exam of 张圣儒 no.20344152\n");
    printf("第一题:\n");
    struct node * head = NULL, * p = head ,* head_2=NULL;

    head = creat_lianbiao();//生成一个长度100,取值范围在0-300的随机整数构成的链表...Done
    print_lianbiao(head);//打印原链表...Done
    putchar('\n');
    putchar('\n');
    head_2 = creat_sort_lianbiao(head);//生成新的链表
    //printf("Done...");
    print_lianbiao(head_2);//打印新的链表


}
(a)(b)步运行结果:
  • 打赏
  • 举报
回复
啊。。。经过艰苦的奋斗,我终于。。。终于完成了,bug也修完了(没想到是被自己解决的)
  • 打赏
  • 举报
回复
第二大段注释那里“不能用链表排序”打错了,应该是“不能用数组辅助排序”

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧