我正在尝试编写一个函数来删除链表中的第一个节点,但出现以下错误:
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/