【发布时间】:2021-08-07 01:46:14
【问题描述】:
头指针指向函数和main中的不同节点。为什么?
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *link;
};
int insert_node(struct node *head){
struct node *ptr=(struct node*)malloc(sizeof(struct node));
struct node *temp=(struct node*)malloc(sizeof(struct node));
ptr->data=12;
ptr->link=NULL;
ptr->link=head;
head=ptr;
temp=head;
while(temp!=NULL){
printf("\n%d",temp->data);
temp=temp->link;
}
printf("\nHead in fn=:%d",head);
}
int main(){
struct node *head=(struct node*)malloc(sizeof(struct node));
head->data=56;
struct node *current=(struct node*)malloc(sizeof(struct node));
current->data=78;
head->link=current;
struct node *current1=(struct node*)malloc(sizeof(struct node));
current1->data=45;
current->link=current1;
struct node *current2=(struct node*)malloc(sizeof(struct node));
current2->data=69;
current2->link=NULL;
current1->link=current2;
insert_node(head);
printf("\nhead in main-:%d",head);
return 0;
}
【问题讨论】:
-
你设置
head=ptr;....
标签: c linked-list