|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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);
} |
|