求链表长度

    技术2022-05-19  27

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


    最新回复(0)