【发布时间】:2016-06-02 19:11:25
【问题描述】:
当我想将项目添加到链表时,我的程序崩溃了。 这是我正在使用的结构
typedef struct seats
{
int number, reserved;
char name[1000];
} seats;
// node list
typedef struct node
{
seats seat;
struct node * next;
} node_t;
这是我的insert 函数
void AddSeat(node_t * head, seats a_seat) // unos podataka na kraj liste
{
node_t * current = head;
while (current->next != NULL) {
current = current->next;
}
/* now we can add a new variable */
current->next = malloc(sizeof(node_t));
current->next->seat = a_seat;
current->next->next = NULL;
}
【问题讨论】:
-
您能否edit 您的问题是添加您为列表创建第一个节点的代码,以及您调用 AddSeat 的代码?
-
您正试图通过价值传递席位。您需要将指针传递给您的例程。
-
您已成功通过价值传递席位:) 不幸的是,这不是您需要做的,因为您只修改了指针的副本:(
-
...如果您在调试器下运行应用程序,您会意识到这一点。
-
这个问题不是
seats a_seat,而是node_t * head。初始化链表和第一次调用addSeat()。
标签: c list input linked-list