# 算法问题(线性表)

（线性表）已知一个单链表中每个结点存放一个整数，并且结点数不少于2，请设计算法以判断该链表中第二项起的每个元素值是否等于其序号的平方减去其前驱的值，若满足则返回ture，否则返回false.

true或者false;

``````7
2 2 7 9 16 20 29``````

``true``

``````#include<stdio.h>
#include<stdlib.h>
struct f
{
int x;
struct f *next;
};
struct f *p;
struct f *f1(int m)
{
while(m--)
{
p1=(struct f *)malloc(sizeof(struct f));
scanf("%d",&p1->x);
else
p2->next=p1;
p2=p1;
}
p2->next=NULL;
}
int main()
{
struct f *p1,*p2,*p;
int m,t=2,flag=0;
scanf("%d",&m);
p=f1(m);
for(p1=p;p1!=NULL;p1=p1->next)
{
p2=p1->next ;
if(p1->next =NULL)
break;
if(p2->x==(t*t-(p1->x )))
flag=1;
else
{
flag=0;
break;
}
t++;
}
if(flag)
printf("true\n");
else
printf("false\n");
return 0;
}
``````

``````#include<iostream>
using namespace std;
#define NULL 0
int i,len,j,n,m;
{
int num;
};
{
cin>>m;
for(int x=1;x<=m;x++)
{
cin>>p1->num;
p2=p1;
p2->next=p1;
if(x==m) delete p1,p2->next=NULL;
}
}
{
p=a->next;p2=a;
int n=1;
while(p!=NULL)
{
n++;
if(n*n-p2->num!=p->num) return false;
p2=p;p=p->next;
}
/*while(a!=NULL)
{
cout<<a->num<<" ";
a=a->next;
}
cout<<endl;*/
return true;
}
int main()
{