c - 我的链表有什么问题


int main(int argc, char *argv[])
{
  printf("successfully started mainn");
  struct uf_list myList;
  uf_list_allocate(&myList);
  printf("successfully allocated myListn");
  insert_node(&myList, 'c');
  printf("successfully inserted into myListn");
  return 0;
}

void uf_list_allocate(struct uf_list *list)
{
  list = malloc(sizeof(struct uf_list));
  if(list == NULL)
    {fprintf(stderr, "no memory for allocate");}
  list->head = list->tail = NULL;
}
//--------------------------------------------------------------------------------------
void insert_node(struct uf_list *list, const char label)   
{
  struct uf_node *it = malloc(sizeof(struct uf_node));
  if(it == NULL)
    {fprintf(stderr, "no memory for insert");}
  it->c = label;
  it->next = NULL;                                     
  it->rep = NULL;
  if(list->head == NULL)                                //the list is empty
    { list->head = list->tail = it;}
  else
    { list->tail->next = it; list->tail = it; }
  it->rep = list->head;
}
/*----------------------------------------------------------------------------*/
struct uf_node
{
  char c;
  struct uf_node *next;
  struct uf_node *rep;
};
/*----------------------------------------------------------------------------*/
struct uf_list
{
  struct uf_node *head;
  struct uf_node *tail;
};

一旦我尝试从main将元素插入我的列表中,我就会遇到分段错误。导致分段错误的原因是什么?如果您需要更多信息,例如structs的定义,请告诉我!

编辑:我意识到我做了什么。在allocate里面,我更改了局部变量的地址list.这意味着myList什么也没发生。但是,现在我有以下难题:我将myList声明放在main,之外,一切正常:

struct uf_list myList;
int main(int argc, char *argv[])
{
  printf("successfully started mainn");
  uf_list_allocate(&myList);
  printf("successfully allocated myListn");
  insert_node(&myList, 'c');
  insert_node(&myList, 'd');
  insert_node(&myList, 'e');
  printf("successfully inserted into myListn");
  print_uf_list(&myList); 

  return 0;
} 

我不太清楚为什么。似乎应该应用相同的逻辑,即,由于我将myList的地址传递到 assign 中,但随后将局部变量更改为地址list并对该地址进行操作,这如何反映在内存地址未作myList上?

在分配中,您不返回任何内容。塔特是问题所在。在 main 中,你应该只有一个指针作为局部变量,并为其分配分配器函数返回的内容。

编辑

更简单的是,由于它已经分配(在主堆栈上),您可以从该函数中删除分配代码,并拥有一个初始化函数。这就是您所需要的:

  Uf_list_init(struct uf_list *list)
 {
    list->head = list->tail = NULL;
 }

在原始代码中:

list = malloc(sizeof(struct uf_list));

你有一个指向 te 结构的指针,但你用一个全新的指针覆盖它。

C 按值传递参数。 uf_list_allocate应该采取uf_list **listRef,以便可以对其进行修改。

#include <stdio.h>
#include <stdlib.h>
struct uf_node
{
  char c;
  struct uf_node *next;
  struct uf_node *rep;
};
struct uf_list
{
  struct uf_node *head;
  struct uf_node *tail;
};
void uf_list_allocate(struct uf_list **listRef)
{
  *listRef = malloc(sizeof(struct uf_list));
  if(*listRef == NULL)
    {fprintf(stderr, "no memory for allocate"); exit(0);}
  (*listRef)->head = (*listRef)->tail = NULL;
}
void insert_node(struct uf_list *list, const char label)
{
  struct uf_node *it = malloc(sizeof(struct uf_node));
  if(it == NULL)
    {fprintf(stderr, "no memory for insert"); exit(0);}
  it->c = label;
  it->next = NULL;
  it->rep = NULL;
  if(list->head == NULL)                                //the list is empty
    { list->head = list->tail = it;}
  else
    { list->tail->next = it; list->tail = it; }
  it->rep = list->head;
}
int is_empty(const struct uf_list *list)
{
  return list->head == NULL;
}
void remove_node(struct uf_list *list)
{
  if (is_empty(list))
  {
    printf("List underflown"); exit(0);
  }
  else
  {
    struct uf_node *oldhead = list->head;
    list->head = list->head->next;
    if (list->tail == oldhead)
      list->tail = NULL;
    free(oldhead);
    printf("Node removedn");
  }
}
void deallocate(struct uf_list **listRef)
{
  struct uf_list *list = *listRef;
  if(!is_empty(list))
  {
    while(!is_empty(list))
      remove_node(list);
  }
  free(list);
  list = NULL;
  printf("List deallocatedn");
}
void printList(const struct uf_list *myList)
{
  struct uf_node *cur = myList->head;
  while(cur!=NULL)
  {
    printf("%c -> ", cur->c);
    cur = cur->next;
  }
  printf("n");
}
int main(int argc, char *argv[])
{
  printf("successfully started mainn");
  struct uf_list *myList;
  uf_list_allocate(&myList);
  printf("successfully allocated myListn");
  insert_node(myList, 'c');
  printf("successfully inserted c into myListn");
  insert_node(myList, 'd');
  printf("successfully inserted d into myListn");
  printList(myList);
  insert_node(myList, 'e');
  printf("successfully inserted e into myListn");
  printList(myList);
  remove_node(myList);
  printf("successfully removed c (head) from myListn");
  printList(myList);
  deallocate(&myList);
  return 0;
}

相关内容

  • 没有找到相关文章

最新更新