马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 GmiaoC 于 2021-9-19 01:15 编辑
小甲鱼的带飞C第47课 “用单链表随机输入一些整数进去,然后从小到大的方式排列出来“
看着小甲鱼的二重头指针感到有点头疼,于是自己用模块化的形式写了出来,算是成功实现了一大半,就是有个奇怪的BUG:
当输入了第一个整数之后,后面再输入的整数无论如何都不能大于第一个输入的整数,不然就报错
实在想不懂这个bug怎么出来的,求大佬们指点指点,感觉问题主要在程序片段2
程序片段1:#include<stdio.h>
#include<stdlib.h>
struct BOOK{
int num;
struct BOOK *next;
};
struct BOOK *createlist() //这是第一步一个创建链表的函数
{
struct BOOK *list=(struct BOOK *)malloc(sizeof(struct BOOK));
list->next=NULL;
return list; //返回了一个指针
}
struct BOOK *createnode(int date) //第二步创建一个新的结点只需要数据
{
struct BOOK *newnode=(struct BOOK *)malloc(sizeof(struct BOOK));
newnode->num=date;
newnode->next=NULL;
return newnode;//返回一个指针
}
void printlist(struct BOOK *list) //第四步打印单链表
{
struct BOOK *print;
print=list->next;
while(print!=NULL)
{
printf("\n%d",print->num);
print=print->next;
}
}
程序片段2:感觉问题主要在这里void insertnode(struct BOOK *list,int date)//第三步插入结点
{
struct BOOK *current=NULL;//一定一定要 把这里初始化,不然程序执行不下去
struct BOOK *previous=NULL;
struct BOOK *intonode = createnode(date);
if(list->next==NULL)
{
struct BOOK *intonode = createnode(date);
intonode->next=list->next;
list->next=intonode;
}
else
{
current=list->next;
previous=list;
while(current->num<date&¤t!=NULL)
{
previous=current;
current=current->next;
}
previous->next=intonode;
intonode->next=current;
}
}
程序片段3int main()
{
struct BOOK *list=createlist();
int date;
while(1)
{
scanf("%d",&date);
if(date!=0)
{
insertnode(list,date);
}
else
{
break;
}
}
printlist(list);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct BOOK {
int num;
struct BOOK *next;
};
// 下面这两个函数没有错,但是写的不好
// 换一下位置,createlist 直接调用 createnode
/*
struct BOOK *createlist() //这是第一步一个创建链表的函数
{
struct BOOK *list = (struct BOOK *)malloc(sizeof(struct BOOK));
list->next = NULL;
return list; //返回了一个指针
}
struct BOOK *createnode(int date) //第二步创建一个新的结点只需要数据
{
struct BOOK *newnode = (struct BOOK *)malloc(sizeof(struct BOOK));
newnode->num = date;
newnode->next = NULL;
return newnode; //返回一个指针
}
*/
struct BOOK *createnode(int date) //第二步创建一个新的结点只需要数据
{
struct BOOK *newnode = (struct BOOK *)malloc(sizeof(struct BOOK));
newnode->num = date;
newnode->next = NULL;
return newnode; //返回一个指针
}
struct BOOK *createlist() //这是第一步一个创建链表的函数
{
return createnode(0);
}
void printlist(struct BOOK *list) //第四步打印单链表
{
struct BOOK *print;
print = list->next;
while (print != NULL) {
printf("\n%d", print->num);
print = print->next;
}
}
/*
void insertnode(struct BOOK *list, int date) //第三步插入结点
{
struct BOOK *current = NULL; //一定一定要 把这里初始化,不然程序执行不下去
struct BOOK *previous = NULL;
struct BOOK *intonode = createnode(date);
if (list->next == NULL) {
//struct BOOK *intonode = createnode(date);
intonode->next = list->next;
list->next = intonode;
} else {
current = list->next;
previous = list;
//while (current->num < date && current != NULL) {
while (current != NULL && current->num < date) {
previous = current;
current = current->next;
}
previous->next = intonode;
intonode->next = current;
}
}
*/
// 上面的已经改好了,能用了,但是不好,下面这样不知道你能不能看懂
void insertnode(struct BOOK *list, int date) {
struct BOOK **current = &list->next;
while(*current != NULL && (*current)->num < date) {
current = &(*current)->next;
}
struct BOOK *temp = createnode(date);
temp->next = *current;
*current = temp;
}
// 申请了内存一定要释放,你那边可能没问题,但是我这边会报错的
// 对,你没有看错,申请了内存不释放,我这边是会报错的
void free_list(struct BOOK *list) {
if(list) free_list(list->next);
free(list);
}
int main() {
struct BOOK *list = createlist();
int date;
while (1) {
scanf("%d", &date);
if (date != 0) {
insertnode(list, date);
} else {
break;
}
}
printlist(list);
free_list(list);
return 0;
}
|