创建一个链表,尾插法!!问题在哪??
#include <stdio.h>#include <stdlib.h>
typedef struct node{
char ch;
struct node *next;
}Node,*LinkList;
LinkList HeadCreat()//创建一个头结点
{
Node *Head=NULL;
Head = (Node *)malloc(sizeof(Node));
if(Head==NULL)
{
printf("创建链表不成功");
return 0;
}
else
return Head;
}
LinkList LinkCreat(LinkList H)//创建链表,向它添加元素
{
Node *r,*s;
char c;
int flag = 1;
r=H;
while(flag)
{
c = getchar();
if(c != '@')
{
s = (Node *)malloc(sizeof(Node));
s->ch = c;
r->next = s;
r = s;
}
else
{
flag = 0;
r->next=NULL;
}
}
return s;
}
void LinkDisplay(LinkList link)//想计算链表的长度
{
LinkList p;
int count = 0;
p = link->next;
while(p!=NULL)
{
p = p->next;
count++;
}
printf("%d",count);
}
int main()
{
LinkList H,link;
H = HeadCreat();
link = LinkCreat(H);
LinkDisplay(link);
return 0;
}