链表数字排列大小
#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一样的链表? 能力有限,我只能找出我觉得有问题的地方,若不对可以一起聊下{:10_256:}
#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);
} nex=*count 把*count的地址赋给nex,通过操作nex来修改*count的内容,这和一般指针的操作一样吧
你也可以先为nex申请内存,然后再*count=nex好像这样也可以
第一种的话*nex是系统临时创建的,它应该再函数结束后就没用了 这一段
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;
}
你可以试试看 大马强 发表于 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);
}
这个是我改了之后的,但是还是不行 大马强 发表于 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);
}
页:
[1]