|
发表于 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);
} |
|