#include <iostream>
#include <stdlib.h>
#define MAX 8
using namespace std;
typedef struct node
{
int num;
struct node* pnext;
}qnode,*pnode;
pnode init()
{
int num_;
int size=MAX;
pnode head,p,q;
head=(pnode)malloc(sizeof(qnode));
head->pnext=NULL;
q=head;
while(size--)
{
p=(pnode)malloc(sizeof(qnode));
cin>>num_;
p->num=num_;
q->pnext=p;
q=p;
}
q->pnext=NULL;
return head;
}
//递归
pnode find(pnode head,int n)
{
pnode p=head->pnext;
if(n==1)
{
return p;
}
else
{
p=find(p,n-1);
}
return p;
}
//非递归
void find_(pnode head,int n)
{
pnode p=head;
int i;
for(i=0;i<n;i++)
{
p=p->pnext;
}
cout<<p->num;
}
int main()
{
pnode head,t;
head=init();
t=find(head,5);
find_(head,7);
}
3.11
最新推荐文章于 2024-08-27 22:07:23 发布