我的代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
char data;
struct node *next;
}Lnode,*Linklist;
void Initlist(Linklist L)
{
L=(Linklist)malloc(sizeof(Lnode));
L->next=NULL;
}
void Createlist(Linklist L)
{
Linklist p,newp;
int cnt=1;
p=(Lnode*)malloc(sizeof(Lnode));
L->next=p;
p->data='A';
for(;cnt<=26;cnt++)
{
newp=(Lnode*)malloc(sizeof(Lnode));
p->next=newp;
newp->next=NULL;
newp->data=(p->data)+1;
p=p->next;
}
}
void Printlist(Linklist L)
{
Linklist p;
p=(Linklist)malloc(sizeof(Lnode));
L->next=p;
while(p)
{
printf("%c",p->data);
p=p->next;
}
}
int main()
{
Lnode L;
Initlist(&L);
Createlist(&L);
Printlist(&L);
return 0;
}
程序已经修改调试完成,主要是Printlist的问题:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
char data;
struct node *next;
}Lnode,*Linklist;
void Initlist(Linklist L)
{
L=(Linklist)malloc(sizeof(Lnode));
L->next=NULL;
}
void Createlist(Linklist L)
{
Linklist p,newp;
int cnt=1;
p=(Lnode*)malloc(sizeof(Lnode));
L->next=p;
p->data='A';
for(;cnt<26;cnt++) //有改动
{
newp=(Lnode*)malloc(sizeof(Lnode));
p->next=newp;
newp->next=NULL;
newp->data=(p->data)+1;
p=p->next;
}
}
void Printlist(Linklist L) //有改动
{
Linklist p=L->next;
while(p)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
Lnode L;
Initlist(&L);
Createlist(&L);
Printlist(&L);
return 0;
}
追问请问一下 L->next=p 和 p=L->next 效果一样吗 实在是搞晕了
追答不一样的。前者是把p所指的结点链接到L的后面,后者是把p指向L的后一个结点。