一、用面向对象的思想模拟实现动态顺序表;
写一个顺序表类;该顺序表类的成员变量为:_sz记录顺序表实际储存的元素个数;_capacity记录顺序表的实际存储容量;_data为一个动态数组;储存元素;
二、该顺序表类的成员函数有
(1)类的默认成员数;
SeqList()//构造
SeqList(const SeqList& seqlist)//拷贝构造
SeqList& operator=(SeqList seqlist)//赋值运算符重载
~SeqList()//析构函数
(2)对顺序表元素的操作函数;
void PushBack(const DataType& x)//尾插
void PopBack()//尾删
void PushFront(const DataType& x)//头插
void PopFront()//头删
void Insert(int pos,const DataType& x)//某一位置之前插入某个元素
int Find(const DataType& x)//查找某一元素---二分查找,返回元素的位置
void Remove(const DataType& x)//删除第一个出现的元素
void RemoveAll(const DataType& x)//删除所有出现的元素
void Sort()//排序---冒泡--升序
void CheakCapacity()//检查并调整容量
三、代码实现:
//模拟实现顺序表
#include<iostream>
using namespace std;
typedef int DataType;
class SeqList
{
friend ostream& operator<<(ostream& os,const SeqList& seqList);
public:
SeqList()//构造
:_capacity(0)
,_sz(0)
,_data(NULL)
{
cout<<"构造"<<endl;
}
SeqList(const SeqList& seqlist)//拷贝构造
:_capacity(seqlist._capacity)
,_sz(seqlist._sz)
,_data(new DataType[sizeof(DataType)*seqlist._sz])
{
cout<<"拷贝构造"<<endl;
memcpy(_data,seqlist._data,sizeof(DataType)*seqlist._sz);
}
SeqList& operator=(SeqList seqlist)//赋值运算符重载
{
cout<<"赋值运算符重载"<<endl;
std::swap(_data,seqlist._data);
_capacity=seqlist._capacity;
_sz=seqlist._sz;
return *this;
}
~SeqList()//析构函数
{
cout<<"析构"<<endl;
if (_data!=NULL)
{
delete[] _data;
}
_capacity=0;
_sz=0;
}
public:
void PushBack(const DataType& x)//尾插
{
CheakCapacity();
_data[_sz++]=x;
}
void PopBack()//尾删
{
_sz--;
}
void PushFront(const DataType& x)//头插
{
CheakCapacity();
for (int i=_sz;i>=1;i--)
{
_data[i]=_data[i-1];
}
_data[0]=x;
_sz++;
}
void PopFront()//头删
{
for (int i=0;i<_sz-1;i++)
{
_data[i]=_data[i+1];
}
_sz--;
}
void Insert(int pos,const DataType& x)//某一位置之前插入某个元素
{
CheakCapacity();
if (pos<0||pos>_sz)
{
return ;
}
for (int i=_sz;i>pos;i--)
{
_data[i]=_data[i-1];
}
_data[pos]=x;
_sz++;
}
int Find(const DataType& x)//查找某一元素---二分查找,返回元素的位置
{
Sort();//二分查找的前提是数据已经排序好
int start=0;
int end=_sz-1;
int mid=0;
while (start<end)
{
mid=(start+end)/2;
if (x>_data[mid])
{
start=mid+1;
}
if (x<_data[mid])
{
end=mid-1;
}
if (x==_data[mid])
{
return mid;
}
}
return -1;
}
void Remove(const DataType& x)//删除第一个出现的元素
{
if (_data==NULL)
{
printf("seqlist empty!");
}
int pos=0;
while(pos<_sz&&_data[pos++]!=x)//找元素
{}
pos-=1;//该元素的位置
if (_data[pos]==x)
{
if (pos==_sz-1)//处理元素为最后一个
{
PopBack();
}
for(int i=pos;i<_sz-1;i++)
{
_data[i]=_data[i+1];
}
_sz--;
}
}
void RemoveAll(const DataType& x)//删除所有出现的元素
{
//(1),(5);(4);(3);(3);(2);(6);
if (_data==NULL)
{
printf("seqlist empty!");
}
int pos=0;
while (pos<_sz+1)
{
while(pos<=_sz&&_data[pos++]!=x)//找元素
{}
pos-=1;//该元素的位置
if (_data[pos]==x)
{
if (pos==_sz-1)//处理元素为最后一个
{
PopBack();
}
for(int i=pos;i<_sz-1;i++)
{
_data[i]=_data[i+1];
}
_sz--;
}
if (pos==_sz)
{
break;
}
}
}
void Sort()//排序---冒泡--升序
{
if (_data==NULL)
{
printf("seqlist empty!");
}
int mark=0;
for(int i=0;i<_sz-1;i++)//比较趟数
{
for (int j=0;j<_sz-i-1;j++)
{
if (_data[j]>_data[j+1])
{
mark=1;
DataType tmp=_data[j];
_data[j]=_data[j+1];
_data[j+1]=tmp;
}
}
if (mark=0)
{
break;
}
}
}
void Printf()
{
cout<<_capacity<<","<<_sz<<endl;
}
private:
void CheakCapacity()//检查并调整容量
{
if (_sz>=_capacity)
{
_capacity+=5;
DataType* tmp=new DataType[_capacity];
memcpy(tmp,_data,sizeof(DataType)*_sz);
delete[] _data;
_data=tmp;
}
}
private:
int _capacity;
int _sz;
DataType* _data;
};
ostream& operator<<(ostream& os,const SeqList& seqList)
{
for (int i=0;i<seqList._sz;i++)
{
os<<seqList._data[i]<<" ";
}
return os;
}
void test()
{
SeqList s;
s.PushBack(1);
s.PushBack(5);
s.PushBack(4);
s.PushBack(5);
s.PushBack(2);
s.PushBack(6);
s.PushBack(3);
s.PushBack(3);
s.Printf();
cout<<"s "<<s<<endl;
cout<<"-----"<<endl;
SeqList s1(s);
s1.Printf();
cout<<"s1(s)"<<s1<<endl;
cout<<"-----"<<endl;
SeqList s2;
s2=s;
s2.Printf();
cout<<"s2=s "<<s2<<endl;
/*
//s.PopBack();
//s.PopBack();
//cout<<"尾删两个元素:"<<s<<endl;
//s.PushFront(7);
//s.PushFront(8);
//s.PushFront(9);
//cout<<"头插三个元素:"<<s<<endl;
//s.PopFront();
//s.PopFront();
//cout<<"头删两个元素:"<<s<<endl;
//s.Insert(3,99);
//cout<<"下标为3的位置插入99:"<<s<<endl;
/*s.Sort();*/
//cout<<s.Find(3)<<endl;
//cout<<s.Find(99)<<endl;
/*s.Remove(1);*/
//s.RemoveAll(3);
//cout<<s<<endl;
}
int main()
{
test();
return 0;
}
接下来是用面向对象的思想实现用c++语言实现单链表