数据结构1_顺序表程序实现
由于线性表中每个数据元素的类型相同,可以用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;
}
posted on 2014-05-15 13:17 zhuangwy_CV 阅读(450) 评论(0) 编辑 收藏 举报