鱼C论坛

 找回密码
 立即注册
查看: 1848|回复: 3

单链表排列数字大小

[复制链接]
发表于 2020-4-8 18:29:56 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 白牡丹秀色可餐 于 2020-4-8 18:31 编辑

输入4 3 1 -1
      2 -1
printNumber函数打印出来一直是4
这是为什么?

#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;
                        while((*count)->a <= input && (*count)->next != NULL)
                        {
                                p = *count;
                                *count = (*count)->next;
                        }
                        if((*count)->a > input)
                        {
                                d = p;
                                p = temp;
                                temp->next = d;
                        }
                        else
                        {
                                p->next = temp;
                        }
        }
}
void printNumber(struct Input *count)
{
        struct Input *p = NULL;
        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
回复

使用道具 举报

发表于 2020-4-8 22:20:50 From FishC Mobile | 显示全部楼层

struct Input
{
        int a;
        //struct Input *next;//这个是否是错误的写法?
        Input *next;//如此定义才对吧?指向Input结构
//的一个指针
};

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 22:24:09 From FishC Mobile | 显示全部楼层
void addNumber(struct Input **count,int input);
这个函数是不是想说添加一个Input对象?参数1为Input指针的指针,参数2为一个Input的成员数据?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 22:29:31 From FishC Mobile | 显示全部楼层
这个结构没有初始化各个数据成员,是一个问题。
这个结构中指向自己的指针成员结束的标志是什么。开头的标志是什么。
换句话说:如果链表中只有自己首元素,这个指针成员的值是否该为NULL?或者当只有两个元素x1 x2时,第一个元素的指针成员可以指向第二个元素的地址,第二个元素的指针成员指向哪里呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 11:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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