c语言 - 无法向链表添加新节点



我正试图编写一个函数,在liked列表的末尾添加新节点,程序因未知原因终止。我添加了"Works for now"邮票,以直观地显示正在发生的事情。看起来程序就在if (*first == NULL) {...}之前终止

代码:

#include <stdio.h>
#include <stdlib.h>
struct student
{
int ID;
char name[50];
char surname[50];
struct student *next;
};
void insertattheend(struct student **first)
{
struct student *new = (struct student *)malloc(sizeof(struct student));
struct student *last = *first;
int Idtemp;
char Nametemp[50], Surnametemp[50];
printf("ID: ");
scanf("%d", &Idtemp);
printf("Name: ");
scanf("%s", Nametemp);
printf("Surname: ");
scanf("%s", Surnametemp);
new->ID = Idtemp;
int i;
for (i = 0; i < 50; i++)
{
new->name[i] = Nametemp[i];
new->surname[i] = Surnametemp[i];
}
printf("Works for nown");
new->next = NULL;
printf("Works for nown");
if (*first == NULL)
{
printf("Works for nown");
*first = new;
printf("Works for nown");
return;
}
while (last->next != NULL)
last = last->next;
last->next = new;
return;
}
void printlist(struct student *oof)
{
while (oof != NULL)
{
printf("ID: %dnName: %snSurname: %s ", oof->ID, oof->name, oof->surname);
oof = oof->next;
}
}
int main(void)
{
struct student *head = NULL;
head = (struct student *)malloc(sizeof(struct student));
char operation;
// a - insert
printf("Choose operation: ");
scanf("%c", &operation);
if (operation == 'a')
insertattheend(&head); // add element
printlist(head);
return 0;
}

输出

Choose operation: a
ID: 12
Name: John
Surname: Nhoj
Works for now
Works for now

我想的都试过了。有人知道怎么做吗?感谢的帮助

首先,您的头部插入是不必要的复杂。您可以通过读取临时student而不是大量不相关的缓冲区来显著做空它。下面是一个半完整的例子:

void insertattheend(struct student **first)
{
// this is where we'll read our data first
struct student s = {0};
// TODO: ALL of these need proper error checking to ensure
//  they succeeded
printf("ID: ");
scanf("%d", &s.ID);
printf("Name: ");
scanf("%49s", s.name);
printf("Surname: ");
scanf("%49s", s.surname);
// march 'first' down the pointer chain until it addresses 
// the last pointer in the list (which wil be the 'head' 
// pointer itself if the list is empty, or the 'next'
// member of the last node if the list is not empty.
while (*first)
first = &(*first)->next;
// allocate a new node
*first = malloc(sizeof **first);
if (*first == NULL)
{
perror("Failed to allocate new list node");
exit(EXIT_FAILURE);
}
// structure copy 's' into the location pointed to by
//  the pointer pointed to by 'first'
**first = s;
// done
}

其次,您的main不必要地为head指向的节点分配空间,而实际上它应该简单地以NULL开头,并允许插入函数从那里管理它。下面显示半完整程序。我怀疑你还会遇到其他问题,我在这里巧妙地解决了其中一些问题。

#include <stdio.h>
#include <stdlib.h>
struct student
{
int ID;
char name[50];
char surname[50];
struct student *next;
};
void insertattheend(struct student **first)
{
// this is where we'll read our data first
struct student s = {0};
// TODO: ALL of these need proper error checking to ensure
//  they succeeded
printf("ID: ");
scanf("%d", &s.ID);
printf("Name: ");
scanf("%49s", s.name);
printf("Surname: ");
scanf("%49s", s.surname);
// march 'first' down the pointer chain until it addresses 
// the last pointer in the list (which wil be the 'head' 
// pointer itself if the list is empty, or the 'next'
// member of the last node if the list is not empty.
while (*first)
first = &(*first)->next;
// allocate a new node
*first = malloc(sizeof **first);
if (*first == NULL)
{
perror("Failed to allocate new list node");
exit(EXIT_FAILURE);
}
// structure copy 's' into the location pointed to by
//  the pointer pointed to by 'first'
**first = s;
// done
}
void printlist(const struct student *oof)
{
while (oof != NULL)
{
printf("ID: %dnName: %snSurname: %sn", oof->ID, oof->name, oof->surname);
oof = oof->next;
}
}
void deletelist(struct student *lst)
{
while (lst)
{
struct student *tmp = lst;
lst = lst->next;
free(tmp);
}
}
int main(void)
{
struct student *head = NULL;
char operation;
printf("Choose operation: ");
scanf(" %c", &operation);
if (operation == 'a')
insertattheend(&head); // add element
printlist(head);
deletelist(head);
return 0;
}

相关内容

  • 没有找到相关文章

最新更新