顺序表的实现


#include <iostream>
using namespace std;
//typedef int elemtype;
const int maxsize=100; 
class sequenlist
{
public:


int a[maxsize];     //表示线性表(a1,a2,…,an,aMaxlen), elemtype表示某种具体数据类型
int  len;                     //len表示线性表的实际长度  
int  length(sequenlist L );   //求顺序表L的长度
void Create();                                 //创建线性表
void PrintOut(sequenlist L);             //输出线性表
void Insert(sequenlist &L , int x , int i);   //将元素x插入顺序表L第i个位置
void Delet(sequenlist &L , int  i) ;         //删除顺序表L第i个位置的元素
void setnull(sequenlist &L );             //顺序表L置空表
int Locate(sequenlist  L , int  x) ;     //定位,顺序表L中查找元素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的后继
};
void sequenlist::Create()
{
cout<<"\n Input Length=";
cin>>len;
cout<<"\n Input Data:\n";
for(int k=0;k<len;k++)
cin>>a[k];
}
void sequenlist::PrintOut(sequenlist L)
{
for(int i=0;i<L.len;i++)
{
cout<<L.a[i]<<" ";
}
cout<<endl;
}
int sequenlist::length(sequenlist L )
{
return L.len;
}
void sequenlist::Insert(sequenlist &L , int x , int i)
{
if(L.len>=maxsize)
cout<<"溢出"<<endl;
else if((i>L.len+1)||(i<1))
cout<<"插入位置不正确"<<endl;
else
{
for(int j=L.len;j>=i;j--)
L.a[j]=L.a[j-1];
L.a[i-1]=x;
len++;
}
}
void sequenlist::Delet(sequenlist &L , int i)
{
if((i<1)||(i>L.len))
cout<<" position is not correct!"<<endl;
else {


for(int j=i-1;j<L.len-1;j++)
L.a[j]=a[j+1];                          //元素前移
L.len--;                                    //表长度减1
}
}


void main()
{
sequenlist sl;//声明创建一个类对象sl
sl.Create();
sl.PrintOut(sl);
int i,e;
cout<<"\n插入的位置i和插入的数据e分别是:";
cin>>i>>e;
sl.Insert(sl,e,i);   //调用插入算法函数
sl.PrintOut(sl);
int j;
cout<<"\n删除第j个元素,j是:"<<endl;
cin>>j;
sl.Delet(sl,j);     //调用删除算法函数
sl.PrintOut(sl);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值