#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char element;
struct node *next;
}Lnode,*LinkList;
typedef struct node NODE;
Lnode *Creat()//建立单链表
{
int i;//i为计数器;
char ch;//s为要输入的字符串、
Lnode *h,*p,*t;
h=(Lnode *)malloc(sizeof(Lnode));
h->next=NULL;
t=h;
printf("请输入一个字符,空格分开,回车结束\n");
while(getchar()!='\n')
{
ch=getchar();
p=(NODE *)malloc(sizeof(NODE));//采用尾插入法
p->element=ch;
p->next=NULL;
t->next=p;
t=p;
}
return (h);
}
void main()
{
Creat();
}
编写时出现很多错误,总结如下:
1.typedef struct node
{
char element;
struct node *next;
}Lnode *LinkList;
中间忘记加“,"号。
2.
while(getchar()!='\n')
写为while(ch=getchar()!='\n')
出现错误,因为没有注意到“!=”的优先级比"="高。
此为单链表的尾部插入法。