简体   繁体   English

将节点附加到链接列表

[英]Appending node to linked list

So This code was working fine when I was inserting the node towards the NULL cell. 所以当我将节点插入NULL单元时,此代码工作正常。 I tried implementing this to send the cells to the beginning, but since then the display_list function only shows the last cell. 我尝试将其实现为将单元格发送到开头,但从那时起,display_list函数仅显示最后一个单元格。 I've been trying to figure it out for a while. 我一直想弄清楚它。 Suggestions? 建议?

I will add that this is supposed to be a function that mimics dc in Linux. 我将补充一点,这应该是一个在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);

The main seems to be fine 主要似乎很好

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;
}

The commented out code was working well! 注释掉的代码运行良好! This was until I discovered I was supposed to be adding the cells to the other end, which I'm having difficulty figuring out. 直到我发现我应该将细胞添加到另一端,我很难搞清楚。 What am I missing here? 我在这里错过了什么?

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);
}

I will admit that I have difficulty knowing when I should use pointers, and I suspect that I may be missing one somewhere. 我承认我很难知道什么时候应该使用指针,我怀疑我可能在某个地方遗失了一个。 Any help would be appreciated. 任何帮助,将不胜感激。

Look at this part of your code, 看看你的代码的这一部分,

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
}

Notice Line1 : When you change llist to point to the new node you are changing the local copy of llist while llist in main continues to retain its old value. 注意Line1:当您更改llist以指向新节点时,您将更改llist的本地副本,而llist in main继续保留其旧值。

How do I correct this ? 我该如何纠正?

This is a flaw in your design of the linked list. 这是您的链表设计中的一个缺陷。 The client program(main) should not access the CELL structure at all. 客户端程序(主程序)根本不应访问CELL结构。 You should have another structure which represents the linked list and has a pointer to the first cell. 您应该有另一个表示链接列表的结构,并且具有指向第一个单元格的指针。

Something like this, 像这样的东西,

struct LinkedList {
  struct CELL *head;
};

Your main should be using this structure and not CELL . 你的main应该是使用这种结构而不是CELL


Few other thing I saw in your code, 我在你的代码中看到的其他几件事,

1) display_list function will fail if NULL is passed to it. 1)如果传递NULL display_list函数将失败。 It could be better done like this, 这可能会更好,

void display_list(struct CELL *llist)
{
  while(llist != NULL) {
    printf("%d\n", llist->val);
    llist = llist->next;
  }
}

2) See this line at the end of your main , 2)在main结尾看到这一行,

free(llist);

You are freeing ONLY the first cell in the linked list. 您只释放链表中的第一个单元格。 You have not freed the other cells which were added to the list. 您尚未释放添加到列表中的其他单元格。 This will cause a Memory leak in your program. 这将导致程序中的内存泄漏

How do I solve this ? 我该如何解决这个问题? Freeing the linked list should not be done by the client(main) code. 释放链表不应该由客户端(主)代码完成。 You should provide another function which will recursively free all the allocated cells. 您应该提供另一个函数,它将递归释放所有已分配的单元格。 Again, this is much easier if you follow the above suggested design with a structure representing the linked list. 同样,如果您使用表示链接列表的结构遵循上述建议设计,则会更容易。


Edit : Added an example on request in the comments section. 编辑:在评论部分中根据请求添加了示例。

Your display will look something like this if you change your design to what I have suggested, 如果您将设计更改为我的建议,您的显示将如下所示,

void display_list(struct LinkedList *llist)
{
  struct CELL * head = llist->head;
  while(head != NULL) {
    printf("%d\n", head->val);
    head = head->next;
  }
}

@Mohammad Ghazanfar points are true and one should take care of those points. @Mohammad Ghazanfar的观点是正确的,人们应该照顾这些观点。 On the other hand you can change the following functions to make your code working. 另一方面,您可以更改以下函数以使代码正常工作。

changing function signature of void append_node(struct CELL *llist, int num); 更改void append_node(struct CELL *llist, int num);函数签名void append_node(struct CELL *llist, int num); to void append_node(struct CELL **llist, int num); void append_node(struct CELL **llist, int num); and the function defination is as follows 功能定义如下

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;
}

and replacing the call of append_node(llist, input); 并替换append_node(llist, input);的调用append_node(llist, input); as append_node(&llist, input); as append_node(&llist, input);

Note :- I have just made your code working. 注意: - 我刚刚使您的代码正常工作。 This might not be the perfect solution. 这可能不是完美的解决方案。 You should consider points mentioned by @Mohammad Ghazanfar. 你应该考虑@Mohammad Ghazanfar提到的要点。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM