#include "stdio.h "
#include "malloc.h "
typedef struct lNode//一般这里的lNode用小写吧
{
char s[25];
struct lNode *next;
}LNode,*linklist;
//建立链表函数,l为链表的头指针,n为链表的结点数
void createlist(linklist &l,int n)
{
l=(linklist)malloc(sizeof(LNode));
l-> next=NULL;
// fflush(stdin);
for(int i=0;i<n;i++)
{
linklist p=(linklist)malloc(sizeof(LNode));
scanf( "%s",p-> s);
cout<<"p->s:"<<p->s<<endl;
p-> next=l-> next;
l-> next=p;
//fflush(stdin);
}
}
//链表打印函数,l为链表头指针
void display(linklist &l)
{
while(l=l->next)
{
printf( "%s ",l-> s);
// l=l->next;
}
}
int main()
{
linklist l;
createlist(l,5);
display(l);
return 0;
#include "malloc.h "
typedef struct lNode//一般这里的lNode用小写吧
{
char s[25];
struct lNode *next;
}LNode,*linklist;
//建立链表函数,l为链表的头指针,n为链表的结点数
void createlist(linklist &l,int n)
{
l=(linklist)malloc(sizeof(LNode));
l-> next=NULL;
// fflush(stdin);
for(int i=0;i<n;i++)
{
linklist p=(linklist)malloc(sizeof(LNode));
scanf( "%s",p-> s);
cout<<"p->s:"<<p->s<<endl;
p-> next=l-> next;
l-> next=p;
//fflush(stdin);
}
}
//链表打印函数,l为链表头指针
void display(linklist &l)
{
while(l=l->next)
{
printf( "%s ",l-> s);
// l=l->next;
}
}
int main()
{
linklist l;
createlist(l,5);
display(l);
return 0;
}
注意以下与上边的区别
void createlist(linklist &l,int n)
{
l=(linklist)malloc(sizeof(LNode));
linklist f=l; //注意这里++++++++
l-> next=NULL;
for(int i=0;i<n;i++)
{
linklist p=(linklist)malloc(sizeof(LNode));
scanf( "%s",p-> s);
cout<<"p->s:"<<p->s<<endl;
p-> next=l-> next;
l-> next=p;
l=p;
}
l=f; //注意这里++++++++++++++++++
}