p指针移动和内存分配问题
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct Lnode{
int data;
struct Lnode *next;
}Lnode;
void list_create(Lnode *L);
void print(Lnode *L);
int main(void)
{
Lnode L;
printf("%d\n",sizeof(Lnode));
list_create(&L);
print(&L);
}
void list_create(Lnode *L)
{
int i,n;
Lnode *p;
L->next=NULL;//create list with a head node
printf("Input the length of the list:\n");
scanf("%d",&n);//the next node is null
for(i=1;i<=n;i++){
p=(Lnode *)malloc(sizeof(Lnode));
printf("Input data%d ",i);
scanf("%d",&p->data);
p->next=L->next;//add the next node
L->next=p;
}
}//CreateList_l
void print(Lnode *L)
{
int i;
Lnode *p;
printf("Now the datas int the linked list is\n");
for(p=L->next;p!=NULL;p=p->next){
++i;
printf("%d ",p->data);
}
printf("\n%d\n",i);
}
void print(Lnode *L)
{
int i=0;
Lnode *p;
printf("Now the datas int the linked list is\n");
for(p=L->next;p!=NULL;p=p->next){
++i;
printf("%d ",p->data);
}
printf("\n%d\n",i);
}
void list_create(Lnode *L)
{
int i,n;
Lnode *p;
Lnode *q;
q = L;
L->next=NULL;//create list with a head node
printf("Input the length of the list:\n");
scanf("%d",&n);//the next node is null
for(i=1;i<=n;i++){
p=(Lnode *)malloc(sizeof(Lnode));
printf("Input data%d ",i);
scanf("%d",&p->data);
//尾插法
p->next=NULL;
q->next=p;
q=p;
}
}//CreateList_l