LinkList CreatListR(void)
{//返回单链表的头指针
char ch;
LinkList head;//头指针
ListNode *s,*r; //工作指针
head=NULL; //链表开始为空
r=NULL;//尾指针初值为空
ch=getchar(); //读入第1个字符
while(ch!='\n'){
s=(ListNode *)malloc(sizeof(ListNode));//生成新结点
s->data=ch; //将读入的数据放入新结点的数据域中
if (head!=NULL)
head=s;//新结点插入空表
else
r->next=s;//将新结点插到*r之后
r=s;//尾指针指向新表尾
ch=getchar(); //读入下一字符
}//endwhile
if (r!=NULL)
r->next=NULL;//对于非空表,将尾结点指针域置空head=s;
return head;
} 说明,这是单链表的尾插入法建表我觉得他: if (head!=NULL)
head=s;//新结点插入空表
这一句根本就执行不了(因为初始head=null,而while里根本就没有改过head的值,还有就是,根据他的思路我根本就建不了表的,我觉得)
网址在这里,大家看一看:http://www.hiahia.org/datastructure/xianxingbiao/xianxingbiao2.3.1.2.1.htm
{//返回单链表的头指针
char ch;
LinkList head;//头指针
ListNode *s,*r; //工作指针
head=NULL; //链表开始为空
r=NULL;//尾指针初值为空
ch=getchar(); //读入第1个字符
while(ch!='\n'){
s=(ListNode *)malloc(sizeof(ListNode));//生成新结点
s->data=ch; //将读入的数据放入新结点的数据域中
if (head!=NULL)
head=s;//新结点插入空表
else
r->next=s;//将新结点插到*r之后
r=s;//尾指针指向新表尾
ch=getchar(); //读入下一字符
}//endwhile
if (r!=NULL)
r->next=NULL;//对于非空表,将尾结点指针域置空head=s;
return head;
} 说明,这是单链表的尾插入法建表我觉得他: if (head!=NULL)
head=s;//新结点插入空表
这一句根本就执行不了(因为初始head=null,而while里根本就没有改过head的值,还有就是,根据他的思路我根本就建不了表的,我觉得)
网址在这里,大家看一看:http://www.hiahia.org/datastructure/xianxingbiao/xianxingbiao2.3.1.2.1.htm
{//返回单链表的头指针
char ch;
LinkList head;//头指针
ListNode *s,*r; //工作指针
head=NULL; //链表开始为空
r=NULL;//尾指针初值为空
ch=getchar(); //读入第1个字符
while(ch!='\n'){
s=(ListNode *)malloc(sizeof(ListNode));//生成新结点
s->data=ch; //将读入的数据放入新结点的数据域中
if (head == NULL){
head=s;//新结点插入空表
}
else{
r->next=s;//将新结点插到*r之后
}
r=s;//尾指针指向新表尾
ch=getchar(); //读入下一字符
}//endwhile
if (r!=NULL)
r->next=NULL;//对于非空表,将尾结点指针域置空head=s;
return head;
} 这个算法把head == null 改了应该就没什么问题了
......
ListNode* head=NULL;
......
}