[英]Appending node to linked list
所以当我将节点插入NULL单元时,此代码工作正常。 我尝试将其实现为将单元格发送到开头,但从那时起,display_list函数仅显示最后一个单元格。 我一直想弄清楚它。 建议?
我将补充一点,这应该是一个在Linux中模仿dc的函数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct CELL {
int val;
struct CELL *next;
};
void append_node(struct CELL *llist, int num);
void display_list(struct CELL *llist);
主要似乎很好
int main(void)
{
int num = 0;
int first = 0;
int input = 0;
char quit = 'n';
char inputchar = ' ';
struct CELL *llist;
llist = (struct CELL *)malloc(sizeof(struct CELL));
llist->next = NULL;
while (quit == 'n'){
if (scanf("%d", &input) == 1){
if ( first == 1 )
append_node(llist, input);
if ( first == 0){
llist->val = input;
first = 1;
}
}
else{
inputchar = getchar();
if (llist->next == NULL && first == 0)
printf("List is empty.\n");
if (inputchar == 'f')
display_list(llist);
if (inputchar == 'q')
quit = 'y';
else if (llist->next != NULL){
switch (inputchar){
case 'q':
quit = 'y';
break;
}
}
}
}
free(llist);
return 0;
}
注释掉的代码运行良好! 直到我发现我应该将细胞添加到另一端,我很难搞清楚。 我在这里错过了什么?
void append_node(struct CELL *llist, int num) {
/* while(llist->next != NULL)
llist = llist->next;
llist->next = (struct CELL *)malloc(sizeof(struct CELL));
llist->next->val = num;
llist->next->next = NULL;*/
struct CELL *temp;
temp = (struct CELL *)malloc(sizeof(struct CELL));
temp->val = num;
temp->next = llist;
llist = temp;
}
void display_list(struct CELL *llist)
{
while(llist->next != NULL) {
printf("%d\n", llist->val);
llist = llist->next;
}
printf("%d\n", llist->val);
}
我承认我很难知道什么时候应该使用指针,我怀疑我可能在某个地方遗失了一个。 任何帮助,将不胜感激。
看看你的代码的这一部分,
void append_node(struct CELL *llist, int num) {
struct CELL *temp;
temp = (struct CELL *)malloc(sizeof(struct CELL));
temp->val = num;
temp->next = llist;
llist = temp; // Line1
}
注意Line1:当您更改llist
以指向新节点时,您将更改llist
的本地副本,而llist
in main继续保留其旧值。
这是您的链表设计中的一个缺陷。 客户端程序(主程序)根本不应访问CELL
结构。 您应该有另一个表示链接列表的结构,并且具有指向第一个单元格的指针。
像这样的东西,
struct LinkedList {
struct CELL *head;
};
你的main
应该是使用这种结构而不是CELL
。
我在你的代码中看到的其他几件事,
1)如果传递NULL
display_list
函数将失败。 这可能会更好,
void display_list(struct CELL *llist)
{
while(llist != NULL) {
printf("%d\n", llist->val);
llist = llist->next;
}
}
2)在main
结尾看到这一行,
free(llist);
您只释放链表中的第一个单元格。 您尚未释放添加到列表中的其他单元格。 这将导致程序中的内存泄漏 。
我该如何解决这个问题? 释放链表不应该由客户端(主)代码完成。 您应该提供另一个函数,它将递归释放所有已分配的单元格。 同样,如果您使用表示链接列表的结构遵循上述建议设计,则会更容易。
编辑:在评论部分中根据请求添加了示例。
如果您将设计更改为我的建议,您的显示将如下所示,
void display_list(struct LinkedList *llist)
{
struct CELL * head = llist->head;
while(head != NULL) {
printf("%d\n", head->val);
head = head->next;
}
}
@Mohammad Ghazanfar的观点是正确的,人们应该照顾这些观点。 另一方面,您可以更改以下函数以使代码正常工作。
更改void append_node(struct CELL *llist, int num);
函数签名void append_node(struct CELL *llist, int num);
void append_node(struct CELL **llist, int num);
功能定义如下
void append_node(struct CELL **llist, int num) {
/* while(llist->next != NULL)
llist = llist->next;
llist->next = (struct CELL *)malloc(sizeof(struct CELL));
llist->next->val = num;
llist->next->next = NULL;*/
struct CELL *temp;
temp = (struct CELL *)malloc(sizeof(struct CELL));
temp->val = num;
temp->next = (*llist);
(*llist) = temp;
return;
}
并替换append_node(llist, input);
的调用append_node(llist, input);
as append_node(&llist, input);
注意: - 我刚刚使您的代码正常工作。 这可能不是完美的解决方案。 你应该考虑@Mohammad Ghazanfar提到的要点。
希望这可以帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.