#include <iostream>
#include <stdlib.h>
using namespace std;
#define SIZE 100
struct SqList
{
int data[SIZE];
int length;
SqList()
{
length=0;
}
};
int GetElem(SqList *l,int i)
{
if(l->length==0)
{
cout<<"SqList is empty"<<endl;
exit(1);
}
if(i<1 || i>l->length)
{
cout<<"illegal index"<<endl;
exit(1);
}
return l->data[i-1];
}
bool Insert(SqList *l,int i,int val)
{
if(i<1 || i>l->length+1)
{
cout<<"illegal insert position"<<endl;
return false;
}
if(l->length==SIZE)
{
cout<<"Sqist is full"<<endl;
return false;
}
if(i<=l->length)
{
for(int k=l->length; k>=i; k--)
{
l->data[i]=l->data[i-1];
}
}
l->data[i-1]=val;
l->length++;
return true;
}
bool Remove(SqList *l,int i)
{
if(l->length==0)
{
cout<<"SqList is empty"<<endl;
return false;
}
if(i<1 || i>l->length)
{
cout<<"illegal delete position"<<endl;
return false;
}
if(i<l->length)
{
for(int k=i-1; k<l->length; k++)
{
l->data[i]=l->data[i+1];
}
}
l->length--;
return true;
}
void Display(SqList *l)
{
if(l)
{
for(int i=0; i<l->length; i++)
{
cout<<l->data[i]<<" ";
}
cout<<endl;
}
}
int main()
{
SqList *l=new SqList();
for(int i=1; i<=10; i++)
{
Insert(l,i,i*i);
}
Display(l);
cout<<GetElem(l,5)<<endl;
Remove(l,5);
Display(l);
return 0;
}
线性表的顺序存储
最新推荐文章于 2024-07-27 16:38:03 发布