您的位置:首页 > 其它

求链表长度

2011-03-03 19:45 127 查看
//求链表长度
#include<stdio.h>
#include<stdlib.h>
typedef struct No{
int date;
No* next;
} Node;
Node *creat()
{
Node *head=NULL,*p,*tail;
int x;
scanf("%d",&x);
while(x!=-1){
p=(Node*)malloc(sizeof(Node));
p->date=x;
p->next=NULL;
if(head==NULL)
tail=head=p;
else{
tail->next=p;
tail=p;
}
scanf("%d",&x);
}
return head;
}
int count(Node* head)
{
int sum=0;
Node* p=head;
while(p!=NULL){
sum++;
p=p->next;
}
return sum;
}
main()
{
Node *head;
head=creat();
printf("%d/n",count(head));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐