#include<iostream>
using namespace std;
typedef struct Node
{
int data;
Node *next;
}lnode,*list;
list listinit()
{
list L;
L=(lnode*)malloc(sizeof(list));
if(L==NULL)
cout<<"没有足够的内存空间!"<<endl;
L->next=NULL;
return L;
}
list listcreateTail()//Tail
{
int i,len,data;
list L,p,r;
L=(lnode*)malloc(sizeof(list));
L->next=NULL;
r=L;
cout<<"input the length of this list:"<<endl;
cin>>len;
cout<<"input the data of this list:"<<endl;
// cin>>data;
for(i=0;i<len;i++)
{
cin>>data;
p=(lnode*)malloc(sizeof(list));
p->data=data;
r->next=p;
r=p;
}
r->next=NULL;
return L;
}
int LastKnum(list L,int k)//robust
{
if(L==NULL||k==0)
return NULL;
list p,q,r;
p=L->next;
r=L->next;
while(k)
{
if(p)
{
p=p->next;
k--;
q=p;
}
else
return NULL;
}
while(q)
{
q=q->next;
r=r->next;
}
return r->data;
{
{
list p;
p=L->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
list L,Lr;
int k;
L=listcreateTail();
output(L);
cout<<endl;
cin>>k;
cout<<LastKnum(L,k)<<endl;
system("pause");
return 0;
}
using namespace std;
typedef struct Node
{
int data;
Node *next;
}lnode,*list;
list listinit()
{
list L;
L=(lnode*)malloc(sizeof(list));
if(L==NULL)
cout<<"没有足够的内存空间!"<<endl;
L->next=NULL;
return L;
}
list listcreateTail()//Tail
{
int i,len,data;
list L,p,r;
L=(lnode*)malloc(sizeof(list));
L->next=NULL;
r=L;
cout<<"input the length of this list:"<<endl;
cin>>len;
cout<<"input the data of this list:"<<endl;
// cin>>data;
for(i=0;i<len;i++)
{
cin>>data;
p=(lnode*)malloc(sizeof(list));
p->data=data;
r->next=p;
r=p;
}
r->next=NULL;
return L;
}
int LastKnum(list L,int k)//robust
{
if(L==NULL||k==0)
return NULL;
list p,q,r;
p=L->next;
r=L->next;
while(k)
{
if(p)
{
p=p->next;
k--;
q=p;
}
else
return NULL;
}
while(q)
{
q=q->next;
r=r->next;
}
return r->data;
}
/*
int CommonNode(list L1,list L2){
}
*/
void output(list L){
list p;
p=L->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
list L,Lr;
int k;
L=listcreateTail();
output(L);
cout<<endl;
cin>>k;
cout<<LastKnum(L,k)<<endl;
system("pause");
return 0;
}