鱼C论坛

 找回密码
 立即注册
查看: 2222|回复: 5

链表数字排列大小

[复制链接]
发表于 2020-4-6 02:17:02 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>

struct Input
{
        int a;
        struct Input *next;
};
void addNumber(struct Input**count,int input);
void printNumber(struct Input*count);

void addNumber(struct Input**count,int input)
{
        struct Input *temp,*arrang,*nex;
        nex = *count;
        temp = (struct Input *)malloc(sizeof(struct Input));
        if(temp == NULL)
        {
                printf("OFF");
                exit(0);
        }
        temp->a = input;
        if(nex->next == NULL)
        {
                nex->next = temp;
                temp->next = NULL;
                exit(0);
        }
        while(1)
        {
                if((nex->next)->a>temp->a)
                {
                        arrang = nex->next;
                        nex->next = temp;
                        temp->next = arrang;
                        break;
                }
                if(nex->next == NULL)
                {
                        nex->next = temp;
                        temp->next = NULL;
                }
        }
}
void printNumber(struct Input *count)
{
        do
        {
                printf("%d ",count->a);
                count = count->next;
        }while(count != NULL);
}
void main()
{
        struct Input *count = NULL;
        int input;
        printf("输入数值(-1结束):");
        while(1)
        {
                scanf_s("%d",&input);
                if(input == -1)
                {
                        break;
                }
                addNumber(&count,input);
        }
        printf("the number you want to add:");
        scanf_s("%d",&input);
        addNumber(&count,input);
        printf("the result after arranging:\n");
        printNumber(count);
}

调试的时候在输入数据后就错误中断了,请问是为什么?应该怎么改?另外请问在addNumber中创建指针,使指针等于count时,是算作在count那里接一个指针还是系统临时再创建一个和count一样的链表?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-6 09:10:26 | 显示全部楼层
能力有限,我只能找出我觉得有问题的地方,若不对可以一起聊下
#include<stdio.h>
#include<stdlib.h>

struct Input
{
        int a;
        struct Input *next;
};
void addNumber(struct Input**count,int input);
void printNumber(struct Input*count);

void addNumber(struct Input**count,int input)
{
        struct Input *temp,*arrang,*nex;
        nex = *count;//头节点
        temp = (struct Input *)malloc(sizeof(struct Input));
        if(temp == NULL)
        {
                printf("OFF");
                exit(0);
        }
        temp->a = input;       
        if(nex->next == NULL) //判断头节点
        {
                nex->next = temp;
                temp->next = NULL;
                exit(0);
        }
        while(1)                //好像没有设置遍历链表的代码
        {
               
                if((nex->next)->a>temp->a)
                {
                        arrang = nex->next;
                        nex->next = temp;
                        temp->next = arrang;
                        break;
                }
               
                if(nex->next == NULL)
                {
                        nex->next = temp;
                        temp->next = NULL;
                }
        }
}
void printNumber(struct Input *count)
{
        do
        {
                printf("%d ",count->a);
                count = count->next;
        }while(count != NULL);
}
void main()
{
        struct Input *count = NULL;
        int input;
        printf("输入数值(-1结束):");
        while(1)
        {
                scanf_s("%d",&input); // scanf_s ?  scanf()
                if(input == -1)
                {
                        break;
                }
                addNumber(&count,input);
        }
        printf("the number you want to add:");
        scanf_s("%d",&input);
        addNumber(&count,input);
        printf("the result after arranging:\n");
        printNumber(count);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 09:23:41 | 显示全部楼层
nex=*count 把*count的地址赋给nex,通过操作nex来修改*count的内容,这和一般指针的操作一样吧
你也可以先为nex申请内存,然后再*count=nex好像这样也可以
第一种的话*nex是系统临时创建的,它应该再函数结束后就没用了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 09:30:51 | 显示全部楼层
这一段
while(1)                //好像没有设置遍历链表的代码
        {
               
                if((nex->next)->a>temp->a)
                {
                        arrang = nex->next;
                        nex->next = temp;
                        temp->next = arrang;
                        break;
                }
               
                if(nex->next == NULL)
                {
                        nex->next = temp;
                        temp->next = NULL;
                }
        }
我觉得可以改成
while(1)
        {
                arrang=nex->next;
                if(arrang->a>temp->a && arrang==NULL)
                {
                        nex->nex=temp;
                        temp->next=arrang;
                        break;
                        }
                        nex=nex->next;
                }
你可以试试看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-8 17:28:59 | 显示全部楼层
大马强 发表于 2020-4-6 09:23
nex=*count 把*count的地址赋给nex,通过操作nex来修改*count的内容,这和一般指针的操作一样吧
你也可以 ...

还是不太懂,请问你说的第一种是哪一种?子函数里nex=*count能够直接修改count这个链表对吗?
#include<stdio.h>
#include<stdlib.h>

struct Input
{
        int a;
        struct Input *next;
};
void addNumber(struct Input **count,int input);
void printNumber(struct Input **count);

void addNumber(struct Input**count,int input)
{
        struct Input *temp=NULL,*p=NULL;
        static int i=0;
        i++;
        temp = (struct Input *)malloc(sizeof(struct Input));
        if(temp == NULL)
        {
                printf("%d OFF",i);
                exit(0);
        }

        temp->a = input;
        temp->next = NULL;

        if(i == 1)
        {
                (*count)->next = temp;
        }
        else
        {
                p = *count;
                *count = (*count)->next;
                        while((*count)->a < input || (*count)->next != NULL)
                        {
                                p = *count;
                                *count = (*count)->next;
                        }
                        if((*count)->next == NULL)
                        {
                                p->next = temp;
                        }
                        else
                        {
                                p->next = temp;
                                temp->next = *count;
                        }
        }
}
void printNumber(struct Input **count)
{
        do
        {
                printf("%d ",(*count)->a);
                *count = (*count)->next;
        }while(*count != NULL);
}
void main()
{
        struct Input *count = NULL;
        int input;
        printf("输入数值(-1结束):");
        while(1)
        {
                scanf_s("%d",&input);
                if(input == -1)
                {
                        break;
                }
                addNumber(&count,input);
        }
        printf("the number you want to add:");
        scanf_s("%d",&input);
        addNumber(&count,input);
        printf("the result after arranging:\n");
        printNumber(&count);
}
这个是我改了之后的,但是还是不行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-8 19:46:38 | 显示全部楼层
大马强 发表于 2020-4-6 09:30
这一段
while(1)                //好像没有设置遍历链表的代码
        {

已经试出来啦,谢谢!

#include<stdio.h>
#include<stdlib.h>

struct Input
{
        int a;
        struct Input *next;
};
void addNumber(struct Input **count,int input);
void printNumber(struct Input *count);

void addNumber(struct Input**count,int input)
{
        struct Input *temp=NULL;
        struct Input *p=NULL;
        struct Input *d=NULL;
        static int i=0;

        i++;

        temp = (struct Input *)malloc(sizeof(struct Input));
        if(temp == NULL)
        {
                printf("%d OFF",i);
                exit(0);
        }
        temp->a = input;
        temp->next = NULL;

        if(i == 1)
        {
                *count = temp;
        }
        else
        {
                p = *count;
                d = *count;
                        while(p->a <= input && p->next != NULL)
                        {
                                d = p;
                                p = p->next;
                        }
                        if((*count)->a > input)
                        {
                                *count = temp;
                                temp->next = p;
                        }
                        else if(p->a > input)
                        {
                                d->next = temp;
                                temp->next = p;
                        }
                        else
                        {
                                p->next = temp;
                        }
        }
}
void printNumber(struct Input *count)
{
        struct Input *p = count;
        do
        {
                printf("%d ",p->a);
                p = p->next;
        }while(p != NULL);
}
void main()
{
        struct Input *count = NULL;
        int input;
        printf("输入数值(-1结束):");
        while(1)
        {
                scanf_s("%d",&input);
                if(input == -1)
                {
                        break;
                }
                addNumber(&count,input);
        }
        printNumber(count);
        printf("\nthe number you want to add(-1 will over):");
        while(1)
        {
                scanf_s("%d",&input);
                if(input == -1)
                {
                        break;
                }
                addNumber(&count,input);
        }
        printf("the result after arranging:\n");
        printNumber(count);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 09:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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