#include <malloc.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LinkNode;
void InitList(LinkNode **L){
(*L)=(LinkNode*) malloc(sizeof(LinkNode));
(*L)->next=NULL;
}
void CreateListF(LinkNode **L, ElemType a[], int n)
{ LinkNode *s;//printf("%d\n",L);printf("%d\t%d\n",L->data,L->next);
//(*L)=(LinkNode*)malloc(sizeof(LinkNode));
//(*L)->next=NULL;
for(int i=0;i<n;i++){
s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=a[i];
s->next=(*L)->next;
(*L)->next=s;
}
}
void CreateListT(LinkNode **L, ElemType a[], int n){
LinkNode *s, *r;
r=*L;
for(int i=0;i<n;i++){
s=(LinkNode*)malloc(sizeof (LinkNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void DispList(LinkNode **L){
LinkNode *p=(*L)->next;
//printf("%p %p %p %d ",p->next,(*L)->next,p,p->data);
while(p!=NULL){
//p=p->next;
printf("%d\t",p->data);
p=p->next;
}
printf("\n");
}
void DestroyList(LinkNode **L){
LinkNode *pre=*L, *p=(*L)->next;
while(p!=NULL){
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
int main(){
LinkNode *L=NULL;
//printf("l1=%d\n",L->next);
//printf("l8=%d\n",&L);
ElemType a[]={2,1};
int lenth= sizeof(a)/ sizeof(a[0]);
InitList(&L);
//CreateListF(&L,a,4);
CreateListT(&L,a,lenth);
DispList(&L);
DestroyList(&L);
CreateListF(&L,a,4);
DispList(&L);
}
为什么输出后会是这样(一直循环)