我不知道是我代码写错了还是怎么
麻烦各位大家看看
//单链表前插法
#include<stdio.h>
#include<stdlib.h>
struct Student
{
int num;
struct Student *next;
};
struct Student *create()
{
struct Student *head,*n;//n是新节点
int i;
head=(struct Student *)malloc(sizeof(struct Student));
head->next=NULL;//头指针指向为空
while(n->num!=0)
{
n =(struct Student *)malloc(sizeof(struct Student));
printf("num=");
scanf("%d",&n->num);
n->next=head->next;//新节点指向head节点指向的节点
head->next=n;
}
// free(n);//添加free输出不正确
return head;
}
void print(struct Student *head)
{
int s;
struct Student *temp;
printf("-----------the list -----------");
temp=head;
while(temp!=NULL)
{
printf("the num=%d\n",temp->num);
temp=temp->next;
}
}
int main(){
struct Student *head;//定义一个头指针
head=create();
print(head);
return 0;
}
不加free
加上free