用c语言创建链表

Python016

用c语言创建链表,第1张

函数这里

Linklist List

printf("输入创建链表的长度:")

scanf("%d",&num)

CreateList_H(List,num) //创建链表

改为 

LNode List

printf("输入创建链表的长度:")

scanf("%d",&num)

CreateList_H(&List,num) //创建链表

函数内在堆上分配好内存,但是 没有传递到栈上

另外 你的变量名很迷人

#include<stdio.h>

#include<stdlib.h>

typedef struct data { int number struct data *next } DATA

int main() { int n DATA *head,*p

printf("how many?\n")scanf("%d",&n)head=create(n)printf("there is all\n")

while ( head!=NULL ) { printf("%d ",head->number)head=head->next} printf("\n")

while ( head!=NULL ) { p=headhead=head->nextfree(p)}

return 0

}

DATA *create(int n) { DATA *head=NULL,*t=NULL,*tial=NULL int i

printf("let's create it\n") head=(DATA*)malloc(sizeof(DATA))

if ( !head ) { printf("error") return NULL }

head->next=NULL printf("enter your data\n")  scanf("%d",&head->number)

for ( i=1,tial=headi<ni++ ) {

t=(DATA*)malloc(sizeof(DATA)) if ( !t ) { printf("error")return head }

scanf("%d",&t->number) t->next=NULLtial->next=t tial=t

}

return head

}