#include<iostream.h>
const int maxsize=100;
#include<fstream>
class sequenlist
{
public :
int a[maxsize];//表示线性表
int len;//线性表的长度
int length(sequenlist L);//线性表的长度
void Insert(sequenlist &L,int x,int i);//将元素x插入到书序表L的第i个位置
void Dele(sequenlist &L,int i);//删除顺序表L第i个位置的元素
void setnull(sequenlist &L);//书序表L指控表
int Locate(sequenlist L,int x);//定位,在顺序表中查找元素x的位置
int Get(sequenlist L,int i);//取顺序表L中第i个位置的元素
int prior(sequenlist L,int x);//去顺序表中L中元素X的前驱
int next(sequenlist L,int x);//在顺序表中L取元素x的后继
};
int sequenlist::length(sequenlist L)
{
return L.len;
// return sizeof(L.a)/sizeof(int);
}
void sequenlist::Insert(sequenlist &L,int x,int i)
{
if(L.len>=maxsize-1)
cout<<"overlow/n";
else if(i<1||i>L.len+1)
cout<<"illegal i/n";
else
{
for(int j=L.len;j>=i;j--)
L.a[j+1]=L.a[j];
L.a[i]=x;
L.len++; //the sequenlist length plus one
}
}
void sequenlist::Dele(sequenlist &L,int i)
{
if(i<1||i>L.len)
cout<<"the position is illegal/n";
else
{
for(int j=i+1;j<=L.len;j++)
L.a[j-1]=L.a[j];
L.len--;
}
}
int sequenlist::Locate(sequenlist L,int x)
{
int j=0;
while(j<L.len&&L.a[j]!=x)
j++;
if(j<L.len) return j;
else return -1;
}
int sequenlist::Get(sequenlist L,int i)
{
if(i<1||i>L.len)
{
cout<<"the position is illegal/n";
return NULL;
}
else
return L.a[i-1];
}
int sequenlist::prior(sequenlist L,int x)
{
int i;
i=L.Locate(L,x);
if(i<1)
{
cout<<"the x is not being/n";
return NULL;
}
else if(i==1)
{
cout<<"the x is the first element ,so there is not the prior element /n";
return NULL;
}
else
return a[i-1];
}
int sequenlist::next(sequenlist L,int x)
{
int i;
i=L.Locate(L,x);
if(i<1)
{
cout<<"the x is not being/n";
return NULL;
}
else if(i==L.len)
{
cout<<"the x is the last element/n";
return NULL;
}
else
return L.a[i+1];
}
void main()
{
using namespace std;
sequenlist sequ;
ifstream inFile;//
inFile.open("sequen.txt");
if(!inFile.is_open())
{
cout<<"打开文件失败/n";
exit(EXIT_FAILURE);
}
cout<<"input the data from the sequen.txt"<<endl;
int i=0;
while(inFile.good())
{
inFile>>sequ.a[i];
i++;
}
sequ.len=i;
cout<<" 输出顺序表中的内容"<<endl;
for(int j=0;j<5;j++)
cout<<sequ.a[j]<<endl;
int len=sequ.length(sequ);
cout<<"the length of the sequenlist"<<len<<endl;
sequ.Insert(sequ,6,6);
cout<<sequ.Get(sequ,3)<<endl;
cout<<sequ.Locate(sequ,3)<<endl;
cout<<sequ.prior(sequ,3)<<endl;
cout<<sequ.next(sequ,3)<<endl;
sequ.Dele(sequ,6);
}