简体   繁体   English

指针在链表c实现中保持为空

[英]pointer stays null in linked list c implementation

Sorry to bother you guys but I'm having difficulty understanding why my linked list implementation stays null. 很抱歉打扰你们,但是我很难理解为什么我的链表实现仍然为空。 I'm trying to append elements to the beginning of the list. 我正在尝试将元素追加到列表的开头。 It's been a long time since I programmed in C and I simply can't spot the bug after hours of checking. 自从我用C语言编程以来已经有很长时间了,经过数小时的检查,我根本无法发现bug。 Is it because I assign e to list and then free e?? 是因为我将e分配给列表,然后释放e?

#include <stdio.h>
#include <stdlib.h>
struct element{
  struct element *next;
  int i;
};
typedef struct element element;

void add(element *list, int n){
  element *e;
  e = malloc(sizeof(element));
  e->i = n;
  e->next = list;
  list = e;
  free(e);

}

void display(element *list){
  element *p;
  p = list;
  while(p!=NULL){
    printf(" %d\n",p->i );
    p=p->next;
  }

}

int main(void){
  element *list,*tail,*e, *p;
  //list = (element *)malloc(sizeof(element));
  //list->next = 0;
  list=NULL;
  add(list, 1);
  add(list,2);  
  add(list, 3); 
  display(list);
  //printf("%d\n",list->next);
  free(list);

  return 0;
}

Okay, thanks to bubbles for suggesting extra level of indirection. 好吧,多亏了气泡提示了更多间接级别的建议。 I'm such a noob... why do I only need only ONE level of indirection to add element to the end of the list as shown below? 我真是个菜鸟……为什么我只需要一个间接级别就可以将元素添加到列表的末尾,如下所示?

#include <stdio.h>
#include <stdlib.h>
struct element{
  struct element *next;
  int i;
};
typedef struct element element;

void addB(element **list,int n){
    element *e = malloc(sizeof(element));
    e->i = n;
    e->next = *list;
    *list = e;
    //printf("%d\n", list->i);
}

void addE(element *list, int n){
  element *e = malloc(sizeof(element));
  e->i = n;
  e->next = NULL;
  element *p;
  p = list;
  if(!(list)){
    list = e;
  }else{
      while(p->next){
        p=p->next;
      }
      p->next = e;
  }

}

void display(element *list){
  element *p;
  p = list;
  while(p!=NULL){
    printf(" %d\n",p->i );
    p=p->next;
  }

}

void freelist(element *list){
  if(!(list->next)){
    free(list);
  }else{
    freelist(list->next);
  }

}

int main(void){
  element *list;
  list = NULL;
  addB(&list,1);
  addB(&list,2);
  addB(&list,3);
  addE(list,4);
  display(list);
  freelist(list);

  return 0;
}

You are freeing e in the same routine you are creating it. 您将在创建它的相同例程中释放e。 Instead you need to remove that free. 相反,您需要免费删除它。 For freeing the entire list, you will need a more substantive routine that traverses the list, freeing each element that was malloc'ed using the add routine. 为了释放整个列表,您将需要一个遍历列表的更实质性的例程,以释放使用add例程malloc的每个元素。

The problem is that you are freeing "e". 问题是您要释放“ e”。 even after you are assigning e to list, e still points to the same memory location. 即使在将e分配给列表之后,e仍指向相同的存储位置。 Freeing e will simply make the allocated memory back to the part of free heap. 释放e只会使分配的内存回到空闲堆的一部分。 Also the statement list = e will not have any effect, because you are altering a local copy of list. 同样,语句list = e不会有任何效果,因为您正在更改list的本地副本。 Modify the value of list variable you will have to define your add function something like 修改list变量的值,您将需要定义add函数,例如

void add(element **list, int n){
  element *e;
  e = malloc(sizeof(element));
  e->i = n;
  e->next = *list;
  *list = e;

}


int main(void){
  element *list,*tail,*e, *p;
  //list = (element *)malloc(sizeof(element));
  //list->next = 0;
  list=NULL;
  add(&list, 1);
  add(&list,2);  
  add(&list, 3); 
  display(list);
  //printf("%d\n",list->next);
  free(list); //Should be replaced with a loop.

  return 0;
}

The way you have freed the list will free only the last node of it. 您释放列表的方式将仅释放列表的最后一个节点。 You will have to write a loop to free the complete list. 您将必须编写循环以释放完整列表。

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

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