尾插法建立的链表长度是输入个数的两倍。
自己找的问题是:在有数据的节点后有一个空节点,然后才是下一个带数据的节点。
自己寻找过,也和同学讨论过,都感觉没啥大问题,就是找不出来,怎么改进一下,希望大佬解答,下面是代码
感谢大佬
#include<stdio.h>#include<stdlib.h> #define LEN sizeof(struct Deat)struct Deat{ char deat; //数据域 struct Deat *next; //指针域 };struct Deat Creat(){ struct Deat head,p,q; //头指针head,两个指针,一个开辟新空间,另一个来指向开辟的新空间 char c; //用来存放输入的数据进行判断 p=(struct Deat)malloc(LEN); head=p; head->next=NULL; printf("请输入链表数据,以$结束!\n"); while((c=getchar())!='$'){ p->deat=c; q=(struct Deat)malloc(LEN); p->next=q; p=q; p->next=NULL; } return head;}void Show(struct Deatl) { while(l->next!=NULL){ putchar(l->deat); l=l->next; }}int Howlong(struct Deatl){ int a=0; while(l->next!=NULL){ a=a+1; l=l->next; } return a;}int main(){ struct Deat *l; l=Creat(); printf("----------------------------\n"); Show(l); printf("----------------------------\n"); printf("该链表长度为%d\n",Howlong(l)); return 0;}