#include <iostream>
#include<iomanip>
using namespace std;
#define MAXSIZE 10000
typedef struct LNode
{
double data;
struct LNode *next;
}LNode,*LinkList;
void InitList(LinkList &L)
{
L=new LNode;
L->next=NULL;
return;
}
void CreateList_R(LinkList &L,int n)
{
LinkList r,p;int i=0;
L=new LNode;
L->next=NULL;
r=L;
//int n;
//cin>>n;
while(1)
{
p=new LNode;
cin>>p->data;
p->next=NULL;
r->next=p;
r=p;
i++;
if(i==n) break;
}
return;
}
int ListLength(LinkList L)
{
LinkList p;
p=L->next; //p指向第一个结点
int i=0;
while(p){//遍历单链表,统计结点数
i++;
cout<<p->data<<endl;
p=p->next;
}
return i;
}
double Sum(LinkList p,double sum)
{
if(p->next!=NULL)
{
sum = sum + 1;
return Sum(p->next, sum);
}
else
{
return sum+1;
}
}
int main()
{
LinkList L;int n;int i=0;double sum=0;//未赋值导致不A
InitList(L);
while(1)
{
cin>>n;
if(n==0)break;
CreateList_R(L,n);
cout<<Sum(L->next,sum)<<endl;
}
return 0;
}
description
Using a single linked list represents a sequence of integers, the recursive method of calculating the number of nodes in a single linked list.
Entry
A plurality of sets of data, each data has two rows, the first row of chain length n, the second line of the list of n elements (separated by a space between the elements). When n = 0, the input end.
Export
For each data output line, respectively, corresponding to the number of each node in the linked list.
Sample input 1
4 1 2 3 4 6 1 2 43 5 7 2 0
Sample Output 1
4 6