C++链表初始化失败

北风吹冷 北风吹冷 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{
int data;
struct LNode*next;
}LinkList;
typedef struct LNode LinkList;

void InitLinkList(LinkList *head)
{
(head)=(LinkList*)malloc(sizeof(LNode));
(head)->next=NULL;
(head)->data=0;
}
void Insert(LinkList *head,int data)
{
printf("开始建立节点:\n");
LNode*node=(LNode*)malloc(sizeof(LNode));
node->data=data;
node->next=NULL;
/*开始插入节点*/
printf("开始插入节点:\n");
LNode *p=head;
while(head->next!=NULL)
p=p->next;
p->next=node;
printf("插入节点完毕:");
}
int main()
{
LinkList *head=NULL;
InitLinkList(head);
for(int i=0;i<3;i++)
Insert(head,i);
}

我在InitLinklist函数中已经初始化了
head,但是在插入节点的时候,进入到了Insert函数后,head的data /next依然
是未定义的,没有初始化成功!
有些不解!
调试信息如下:

+ head 0x002b1540 {data=0 next=0x00000000 } LNode *
但是到了Insert函数执行时

+ head 0x002b14f8 {data=-842150451 next=0xcdcdcdcd } LNode *
就变成了这样
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
北风吹冷
新手乐园 33,314 成员 41,769 内容