c - 请求非结构或 union 中的成员 'next'

标签 c

我正在尝试编写一个函数来删除链表中的第一个节点,但出现以下错误:

prog.c: In function 'del':
prog.c:38:13: error: request for member 'next' in something not a structure or union
    head=head->next;
             ^

非常感谢任何帮助。下面是我的代码:

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};

int length(struct node* head)
{
struct node* current=head;
int count=0;
while(current!=NULL)
{
    count++;
    current=current->next;

}
return count;
}

void push(struct node** head, int ndata)
{
struct node* new_node=(struct node*) malloc(sizeof(struct node));
 new_node->data  = ndata;
  new_node->next = (*head);
  *head=new_node;


 }

void del(struct node** head)
{
if(head==NULL)
return;
else
{
struct node *temp=*head;
 head=head->next;     // Line 38
 printf("data del is %d\n", temp->data);
 free(temp);
}

}

int main()
{

struct node* head = NULL;

push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);

printf("count of nodes is %d", length(head));
del(&head);

return 0;
}

最佳答案

这一行:

head=head->next;

不正确,因为head没有指向列表中的第一个条目,

相反,它指向“头”指针,这些内容指向列表中的第一个条目。

所以你可以使用:

*head = (*head)->next;

关于c - 请求非结构或 union 中的成员 'next',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39311217/

相关文章:

c - 从 gui 前端启动控制台应用程序

c++ - GetImmersiveColorTypeFromName 总是返回 -1

c - 按照出现的顺序对奇数和偶数进行排序

c++ - 如何将位序列放入字节(C/C++)

C源代码,将字符串中单词的首字母从小写更改为大写

压缩多行 C 函数原型(prototype)

c++ - 如何将初始化的结构放入结构中?

c - 关于C中的结构

c - 物理载波监听返回的数据存储在哪里?

c - 通过添加当前目录来编辑 PATH。不通过键入不带 "./"的文件名来编译 *.c 文件