【发布时间】:2020-04-23 03:01:41
【问题描述】:
我不明白为什么这个函数没有在链表的开头添加元素。头部保持不变。
typedef struct node_struct{
int data;
struct node_struct *next;
}node;
void beginning(node *head){
node *new = malloc(sizeof(node));
int value;
printf("Insert a number to add at the beginning: ");
scanf("%d",&value);
new->data = value;
new->next = head;
}
【问题讨论】:
-
你永远不会更新列表头,所以在调用之后它仍然指向旧的列表头。您需要将新的头部返回给调用者(或向
head添加一个间接级别并通过指针更改它)。
标签: c linked-list insert