求链表长度

来源:互联网 发布:千牛卖家数据访客 编辑:程序博客网 时间:2024/04/29 05:03

//求链表长度
#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;
}

原创粉丝点击