Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c-链表中的指针问题_C - Fatal编程技术网

c-链表中的指针问题

c-链表中的指针问题,c,C,这是我的代码(处理链接列表): 我确实在list_insert函数中收到一些警告,但我无法找出原因。如果作为参数传递的节点为NULL,则此函数应在开头插入新节点,否则应在作为参数传递的节点后插入新节点 在此片段中: if (node == NULL){ node_t* new = create_node(); new->next = list->head; list->head = new; new->data = data; } 作业new->

这是我的代码(处理链接列表):

我确实在list_insert函数中收到一些警告,但我无法找出原因。如果作为参数传递的
节点
NULL
,则此函数应在开头插入新节点,否则应在作为参数传递的
节点
后插入新节点

在此片段中:

if (node == NULL){
   node_t* new = create_node();
   new->next = list->head;
   list->head = new;
   new->data = data;
}

作业
new->next=list->head不正确。有什么猜测吗?

在您对
结构节点的定义中:

typedef struct node {
        int data;
        struct node_t* next;
    } node_t;
typedef struct node {
        int data;
        struct node* next;
    } node_t;
您将
next
定义为指向
struct node\u t
的指针,但没有此类类型。您需要结构节点

typedef struct node {
        int data;
        struct node_t* next;
    } node_t;
typedef struct node {
        int data;
        struct node* next;
    } node_t;

在您对结构节点的定义中:

typedef struct node {
        int data;
        struct node_t* next;
    } node_t;
typedef struct node {
        int data;
        struct node* next;
    } node_t;
您将
next
定义为指向
struct node\u t
的指针,但没有此类类型。您需要结构节点

typedef struct node {
        int data;
        struct node_t* next;
    } node_t;
typedef struct node {
        int data;
        struct node* next;
    } node_t;

您不需要
typedef struct list{…}
,只需要类型为
node.\u t*
的变量始终包含列表头,空时为NULL

例如:

#include <stdlib.h>
#include <stdio.h>

typedef struct node {
  int data;
  struct node * next;
} node_t;

node_t* create_node(int data)
{
  node_t* tmp = malloc(sizeof(node_t));

  if (tmp == NULL){
    fprintf(stderr, "Error while allocating memory for node\n");
    exit(EXIT_FAILURE);
  }
  tmp->data = data;
  tmp->next = NULL;
  return tmp;
}

void list_append(node_t ** l, node_t* node){
  if (*l == NULL) {
    *l = node;
  }
  else {
    while ((*l)->next != NULL)
      l = &(*l)->next;

    (*l)->next = node;
  }
}

/* l, *l, where and node must not be NULL */
int list_insert(node_t ** l, node_t* where, node_t* node) {
  if (*l != where) {
    for (;;) {
      if (*l == NULL)
        return 0;
      l = &(*l)->next;
      if (*l = where)
        break;
    }
  }

  node->next = *l;
  *l = node;
  return 1;
}

void pr(node_t * l)
{
  printf("list is :");
  while (l != NULL) {
    printf(" %d", l->data);
    l = l->next;
  }
  putchar('\n');
}

void clear(node_t ** head)
{
  node_t * l = *head;

  while (l != NULL) {
    node_t * n = l->next;
    free(l);
    l = n;
  }

  *head = NULL;
}

int main()
{
  node_t * head = NULL;
  node_t * n3 = create_node(3);

  list_append(&head, create_node(1));
  list_append(&head, n3);
  pr(head);

  list_insert(&head, n3, create_node(2));
  pr(head);  

  list_insert(&head, head, create_node(0));
  pr(head);

  clear(&head);
  pr(head);

  return 0;
}

您不需要
typedef struct list{…}
,只需要类型为
node.\u t*
的变量始终包含列表头,空时为NULL

例如:

#include <stdlib.h>
#include <stdio.h>

typedef struct node {
  int data;
  struct node * next;
} node_t;

node_t* create_node(int data)
{
  node_t* tmp = malloc(sizeof(node_t));

  if (tmp == NULL){
    fprintf(stderr, "Error while allocating memory for node\n");
    exit(EXIT_FAILURE);
  }
  tmp->data = data;
  tmp->next = NULL;
  return tmp;
}

void list_append(node_t ** l, node_t* node){
  if (*l == NULL) {
    *l = node;
  }
  else {
    while ((*l)->next != NULL)
      l = &(*l)->next;

    (*l)->next = node;
  }
}

/* l, *l, where and node must not be NULL */
int list_insert(node_t ** l, node_t* where, node_t* node) {
  if (*l != where) {
    for (;;) {
      if (*l == NULL)
        return 0;
      l = &(*l)->next;
      if (*l = where)
        break;
    }
  }

  node->next = *l;
  *l = node;
  return 1;
}

void pr(node_t * l)
{
  printf("list is :");
  while (l != NULL) {
    printf(" %d", l->data);
    l = l->next;
  }
  putchar('\n');
}

void clear(node_t ** head)
{
  node_t * l = *head;

  while (l != NULL) {
    node_t * n = l->next;
    free(l);
    l = n;
  }

  *head = NULL;
}

int main()
{
  node_t * head = NULL;
  node_t * n3 = create_node(3);

  list_append(&head, create_node(1));
  list_append(&head, n3);
  pr(head);

  list_insert(&head, n3, create_node(2));
  pr(head);  

  list_insert(&head, head, create_node(0));
  pr(head);

  clear(&head);
  pr(head);

  return 0;
}

如果您收到警告,请先修复这些警告。如果您需要这方面的帮助,请在问题中显示您收到的警告。在列表中查找
节点
的循环还应处理在未找到
节点
的情况下到达列表末尾的情况。这是一个重复多次的过程,但困难一如既往,在于找到正确的重复。我不确定是否有一个规范的问答。密切相关,但不完全相同(处理标记的存在/不存在,而不是直接处理拼写错误导致混淆的标记)。如果您有兴趣,我会提供一个程序的更正/修改版本,其中包含执行示例启用编译器警告,用于gcc/clang,最小
-Wall-Wextra-pedantic
,对于VS
/W3
,在未发出警告的情况下编译代码之前,不要接受代码。(只要遵循这条规则,您就可以节省无数的时间)让编译器帮助您编写更好的代码。编译器将告诉您问题代码的确切行(通常是确切的起始字符)。永远不要忽略警告。如果收到警告,请先修复这些警告。如果您需要这方面的帮助,请在问题中显示您收到的警告。在列表中查找
节点
的循环还应处理在未找到
节点
的情况下到达列表末尾的情况。这是一个重复多次的过程,但困难一如既往,在于找到正确的重复。我不确定是否有一个规范的问答。密切相关,但不完全相同(处理标记的存在/不存在,而不是直接处理拼写错误导致混淆的标记)。如果您有兴趣,我会提供一个程序的更正/修改版本,其中包含执行示例启用编译器警告,用于gcc/clang,最小
-Wall-Wextra-pedantic
,对于VS
/W3
,在未发出警告的情况下编译代码之前,不要接受代码。(只要遵循这条规则,您就可以节省无数的时间)让编译器帮助您编写更好的代码。编译器将告诉您问题代码的确切行(通常是确切的起始字符)。永远不要忽视警告。valgrind是一个非常好的验证工具,人们必须知道它的存在,我尽我所能在它下面执行。valgrind是一个非常好的验证工具,人们必须知道它的存在,我尽我所能在它下面执行。