首页 > 代码库 > 顺序表程序实现
顺序表程序实现
由于线性表中每个数据元素的类型相同,可以用C或C++语言中的一维数组来实现顺序表。
#include<iostream>
using namespace std;
#define LIST_INIT_SIZE 50
class SqList
{
int *elem;//存′?储′¢空?间?基ù址·
int length;//当μ±前°长3¤度è
int listsize;//当μ±前°分·?配?的μ?存′?储′¢容èY量á?
public:
SqList(int leng);
int LocateElem(int e);//定¨位?
void ListInsert(int i,int e);//插2?入è?
void ListDelete(int i);//删é?除3y
void show();//显?示ê?顺3序Dò表±í元a素?
~SqList()
{
delete elem;
cout<<"destruct called"<<endl;
}
};
SqList::SqList(int leng)
{
elem=new int[LIST_INIT_SIZE];
if(!elem)cout<<"overflow"<<endl;
if(leng>LIST_INIT_SIZE)
cout<<"error";
else
{ length=leng;
int i=0;
int *p=elem;
for (i;i<leng;i++)
{
cout<<"input"<<i+1<<"num:";
cin>>*(p+i);
}
}
listsize=LIST_INIT_SIZE;
cout<<"the sqlist is constructed"<<endl;
show();
}
int SqList::LocateElem(int e)
{
int i=1;
int *p=elem;
while(i<=length&&(*p++!=e))
++i;
if(i<=length) return i;
else return 0;
}
void SqList::show()
{
int i=1;
int *p=elem;
for(i;i<=length;i++)
{
cout<<"the "<<i<<"th content is"<<*(p+i-1)<<endl;
}
}
void SqList::ListInsert(int i,int e)
{
int *q=&elem[i-1];//指?示ê?插2?入è?位?置?
int *p;
for(p=&elem[length-1];p>=q;--p)//插2?入è?位?置?及°之?后oó的μ?元a素?右óò移ò?
*(p+1)=*p;
*q=e;//插2?入è?e
++length;
cout<<"insertthe"<<i<<" successful"<<endl;
show();
cout<<"the lengtn of SqList is"<<length<<endl;
}
void SqList::ListDelete(int i)
{
if((i<1)||(i>length)) cout<<"delete error";
int *p=&elem[i-1];
int e=*p;
int *q=elem+length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--length;
cout<<"delete "<<i<<" the "<<e<< "successful"<<endl;
show();
}
int main()
{
SqList a(10);
int i=a.LocateElem(7);
if(i)
cout<<"locate is "<<i<<endl;
a.ListInsert(5,10);
a.ListDelete(3);
a.~SqList();
return 0;
}
using namespace std;
#define LIST_INIT_SIZE 50
class SqList
{
int *elem;//存′?储′¢空?间?基ù址·
int length;//当μ±前°长3¤度è
int listsize;//当μ±前°分·?配?的μ?存′?储′¢容èY量á?
public:
SqList(int leng);
int LocateElem(int e);//定¨位?
void ListInsert(int i,int e);//插2?入è?
void ListDelete(int i);//删é?除3y
void show();//显?示ê?顺3序Dò表±í元a素?
~SqList()
{
delete elem;
cout<<"destruct called"<<endl;
}
};
SqList::SqList(int leng)
{
elem=new int[LIST_INIT_SIZE];
if(!elem)cout<<"overflow"<<endl;
if(leng>LIST_INIT_SIZE)
cout<<"error";
else
{ length=leng;
int i=0;
int *p=elem;
for (i;i<leng;i++)
{
cout<<"input"<<i+1<<"num:";
cin>>*(p+i);
}
}
listsize=LIST_INIT_SIZE;
cout<<"the sqlist is constructed"<<endl;
show();
}
int SqList::LocateElem(int e)
{
int i=1;
int *p=elem;
while(i<=length&&(*p++!=e))
++i;
if(i<=length) return i;
else return 0;
}
void SqList::show()
{
int i=1;
int *p=elem;
for(i;i<=length;i++)
{
cout<<"the "<<i<<"th content is"<<*(p+i-1)<<endl;
}
}
void SqList::ListInsert(int i,int e)
{
int *q=&elem[i-1];//指?示ê?插2?入è?位?置?
int *p;
for(p=&elem[length-1];p>=q;--p)//插2?入è?位?置?及°之?后oó的μ?元a素?右óò移ò?
*(p+1)=*p;
*q=e;//插2?入è?e
++length;
cout<<"insertthe"<<i<<" successful"<<endl;
show();
cout<<"the lengtn of SqList is"<<length<<endl;
}
void SqList::ListDelete(int i)
{
if((i<1)||(i>length)) cout<<"delete error";
int *p=&elem[i-1];
int e=*p;
int *q=elem+length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--length;
cout<<"delete "<<i<<" the "<<e<< "successful"<<endl;
show();
}
int main()
{
SqList a(10);
int i=a.LocateElem(7);
if(i)
cout<<"locate is "<<i<<endl;
a.ListInsert(5,10);
a.ListDelete(3);
a.~SqList();
return 0;
}
来自为知笔记(Wiz)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。