List 是标准类库中的一个类,可以简单视之为双向链表,以线性列的方式管理物件集合。list 的特色是在集合的任何位置增加或删除元素都很快,但是不支持随机存取。list 是类库提供的众多容器(container)之一,除此之外还有vector、set、map、…等等。
我们可以使用数组实现list。
这里我只实现int类型的list,其他类型可以使用模板实现。
这是list的方法
#include<iostream>
using namespace std;
typedef int T;
class ArrayList2 {
public:
ArrayList2();//构造空线性表
ArrayList2(int n);//构造长度(即size)为n的线性表
~ArrayList2();//析构函数
int find(T &x) const;//如果存在,则返回第一次出现的x的位置,否则返回-1。
void push_back(const T&x); //将x放在最后一项
void insert(int position,const T&x);
//如果0 = <position <= size(),则将x插入到位置位置。
//否则,动作为空。
ArrayList2(const ArrayList2& c);//拷贝构造函数
const ArrayList2& operator =(ArrayList2& c);//重载=符号
int size()const;//返回线性表中元素个数
int capacity() const; //返回数组的容量
bool full()const; //如果表满,返回true,否则,返回false。
bool empty()const{
return count==0;
}//如果表空,返回true,否则,返回false。
void clear();//将表置为空表
void traverse(void (* visit)(T& a)) ;
//将函数(* visit)应用到表中每个元素上.void
void retrieve(int position,T&x) const;
//如果0 = <position <= size() - 1,则用x返回位于position的元素
//否则,动作为空。
void replace(int position,const T&x);
//如果0 = <position <= size() - 1,则将位置position的元素修改成x。
//否则,动作为空。
void erase(int position,T&x);
//如果0 = <position <= size() - 1,则将位置position的元素删除,
//并将删除的元素用x返回,否则,动作为空。
void erase(int position);
T&operator [](int position);//重载[]符号
//如果0 = <position <= size() - 1,则返回位于position的元素
//否则,动作为空。
private:
T * elems;
int count; //记录数组elems中存储的元素个数
int arraySize ; // 数组长度
};
下面是方法的实现`
ArrayList2::ArrayList2(){
this->elems=NULL;
this->count=0;
this->arraySize=0;
}
ArrayList2::ArrayList2(int n){
this->elems=new int[n];
this->count=n;
for(int i=0;i<n;i++)
this->elems[i]=0;
this->arraySize=n;
}
ArrayList2::ArrayList2(const ArrayList2& c){
this->elems=new int[c.capacity()];
for(int i=0;i< c.size();i++)
this->elems[i]=c.elems[i];
this->count=c.size();
this->arraySize=c.capacity();
}
ArrayList2::~ArrayList2(){
delete[] this->elems;
elems=NULL;
}
void ArrayList2::clear(){
for(int i=0;i<this->count;i++)
this->elems[i]=0;
this->count=0;
}
void ArrayList2::push_back(const T& x){
if(this->full()){
//判断是否数组已满
T *tmp=new int[this->arraySize+1];
for(int i=0;i<this->size();i++)
tmp[i]=elems[i];
delete []elems;
elems=tmp;
this->elems[count]=x;
count++;
this->arraySize++;}
else {
this->elems[count]=x;
count++;
}
}
void ArrayList2::insert(int position,const T &x ){
if(position>=0&&position<=this->size()){
//cout<<"count="<<this->count<<endl;
count++;
if(count>this->arraySize){
T *tmp=new int[count];
for(int i=0;i<this->size();i++)
tmp[i]=elems[i];
delete []elems;
elems=tmp;
//this->elems[count-1]=x;
this->arraySize++;
}
for(int i=this->count-1;i>position;i--){
this->elems[i]=this->elems[i-1];
}
this->elems[position]=x;
}
}
void ArrayList2::erase(int position ,T &x){
if(position>=0&&position<=this->size()-1){
x=this->elems[position];
for(int i=position;i<this->size()-1;i++){
this->elems[i]=this->elems[i+1];
}
this->count--;
}
}
void ArrayList2::erase(int position){
if(position>=0&&position<=this->size()-1){
//x=this->elems[position];
for(int i=position;i<this->size()-1;i++){
this->elems[i]=this->elems[i+1];
}
this->count--;
}
}
int ArrayList2::find( T& x)const {
for(int i=0 ; i<this->size(); i++ ){
if(x==this->elems[i]){
return i;
}
}
return -1;
}
void ArrayList2::retrieve(int position,T &x)const {
if(position>=0&&position<=this->size()-1)
x= this->elems[position];
}
void ArrayList2::traverse(void(*visit)(T&a)){
for(int i=0;i<this->size();i++)
(*visit)(elems[i]);
}
const ArrayList2& ArrayList2::operator =( ArrayList2 &c) {
count=c.size();
arraySize=c.capacity();
this->elems=new int[c.capacity()];
for(int i=0;i< count;i++)
this->elems[i]=c.elems[i];
return *this;
}
void ArrayList2::replace(int position,const T &x){
if(position>=0&&position<=this->size()-1)
this->elems[position]=x;
}
int ArrayList2::capacity() const{
return this->arraySize;
}
int ArrayList2::size() const {
return this->count;
}
bool ArrayList2::full()const {
return this->count==this->arraySize;
}
T& ArrayList2::operator [](int position){
if(position>=0&&position<size()){
return this->elems[position];
}
}
好了,使用数组实现list完成!!!
`