将节点追加到链表

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
}

注意第 1 行:当您将 llist 更改为指向新节点时,您正在更改 llist 的本地副本,而 main 中的 llist 继续保留其旧值。

如何更正此问题?

这是你链表设计的缺陷。客户端程序(主程序)根本不应该访问 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);

您只释放了链接列表中的第一个单元格。您尚未释放已添加到列表中的其他单元格。这将在您的程序中导致 Memory leak

我该如何解决这个问题?释放链表不应由客户端(主)代码完成。您应该提供另一个函数,它将递归释放所有分配的单元格。同样,如果您按照上面建议的设计使用表示链表的结构,这会容易得多。


编辑:根据评论部分的要求添加了示例。

如果您将设计更改为我建议的设计,您的显示将看起来像这样,

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) {
  /* 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);

注意:- 我刚刚使您的代码正常工作。这可能不是完美的解决方案。您应该考虑@Mohammad Ghazanfar 提到的要点。

希望对您有所帮助:)