我有我创建的代码链接列表C. signly链表如下问题:指针的单链表指针用C
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *mknode(int data)
{
struct node* np=malloc(sizeof(struct node));
np->data=data;
np->next=NULL;
return np;
}
struct node * insert (struct node* list,int data)
{
struct node *np;
struct node*curr=list;
struct node* prev=NULL;
np=mknode(data);
for(;curr &&data<curr->data;curr=curr->next)
prev=curr;
np->next=curr;
if(prev)
prev->next=np;
else
list=np;
return list;
}
int main()
{
struct node* head;
head=malloc(sizeof(struct node));
head=insert(head,7);
head=insert(head,2);
head=insert(head,4);
printf("%d",head->data);
printf("%d",head->next->data);
printf("%d",head->next->next->data);
return 0;
}
然而,当我互联网上搜索,我意识到,双指针用于创建链表而不是普通指针。我的意思是,struct node **list
,而不是struct node * list
。我想知道为什么 ?哪一个是正确的,如果他们两个都是真的,他们之间有什么区别,我用我在这里写的示例主实现了我的实现,它工作正常,但我不知道为什么我应该使用指针指针?提前致谢。
'head = malloc(sizeof(struct node));'头部未正确初始化。 – BLUEPIXY
@PeterMiehle我不认识的问题,你链接here.Sorry重复的,但我没有做重复的目的 – caesar