c++使用數組實現雙鏈表list

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完成!!!
`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章