使用动态数组实现列表 C++

数据结构与算法分析C++语言描述》   使用动态数组实现列表

类头文件DList.h

/*
 * DList.h
 *
 *  Created on: 2015-11-17
 *      Author: John
 *
 *      这个头文件定义了用于处理列表的DList数据类型
 *      基本操作包括:
 *      构造函数
 *      析构函数
 *      复制构造函数
 *      赋值运算符
 *      empty:		检查列表是否为空
 *      intert:		插入一个项
 *      erase:		删除一个项
 *      display:	输出列表
 *      <<:			输错操作符
 *
 */
#include<iostream>
using namespace std;

#ifndef DList_H_
#define DList_H_

typedef int ElementType ;

class DList {
public:
	/********* 函数成员 *********/
	/***** 类构造函数 *****/
	DList(int maxSize = 1024);
	/*---------------------------------------------------
	 * 创建一个DList对象
	 *
	 * 前置条件: maxSize是一个正整数,默认值为1024	 *
	 * 后置条件:创建一个空DList对象;myCapacity == maxSize(默认值1024)
	 * 			 myArray指容量为myCapacity的动态数组;mySize为0
	 * ---------------------------------------------------*/

	/**** 类析构函数 ****/
	~DList();
	/*---------------------------------------------------
	 * 销毁一个DList对象
	 * 前置条件:DList对象的生命周期已经结束
	 * 后置条件:在构造函数中为myArray指向的数组所动态分配
	 * 的内存空间返还给堆
	 * */
	/**** 复制构造函数 ****/
	DList(const DList & origDList);
	/*---------------------------------------------------
	 * 创建DList对象的一份复制。
	 * 前置条件:需要origDList的一份复制;origDList是一个常引用形参
	 * 后置条件:创建了origDList的一份复制
	 * ---------------------------------------------------*/
	/**** 赋值运算符 ****/
	const DList & operator=(const DList &rightHandSide);
	/*---------------------------------------------------
	 * 将DList对象的一份复制赋给当前对象
	 *
	 * 前置条件:无
	 * 后置条件:rightHandSide的一份复制被赋给当前对象。返回当前列表
	 * 			的一个常引用。
	 * ---------------------------------------------------*/

	bool empty() const;
	void insert(ElementType item, int pos);
	/*在列表中给定位置插入一个值
	 * 前置条件:item是将被插入的值;数组中有足够的空间(mySize<myCapacity);
	 *			并且插入的位置满足0<= pos <= mySize
	 * 后置条件:item被插入到列表中由pos限定的位置
	 * */
	void erase(int pos);
	/* 删除给定位置的值
		 * 前置条件:item是将被插入的值;数组中有足够的空间(mySize<myCapacity);
		 *			并且插入的位置满足0<= pos <= mySize
		 * 后置条件:item被插入到列表中由pos限定的位置
		 * */
	void display(ostream & out) const;
	/* 显示一个列表
	 * 前置条件: ostream out 已经被打开
	 * 后置条件:这个DList对象所表示的列表被插入到out中
	 * */
private:
	int mySize;							// 当前存储在myArray中的列表的大小
	int myCapacity;						// 数组的容量
	ElementType *myArray;				// 动态数组首地址
}; // DList类声明结束

// 输出操作符的原型
ostream & operator<< (ostream & out, const DList & aDList);

#endif /* DList_H_ */

类实现文件DList.cpp

/*
 * DList.cpp
 *
 *  Created on: 2015-11-17
 *      Author: John
 */
#include <cassert>
#include <new>
using namespace std;

#include "DList.h"

// ---- 类构造函数的定义

DList::DList(int maxSize) :mySize(0),myCapacity(maxSize){
	// TODO Auto-generated constructor stub
	myArray = new (nothrow) ElementType[maxSize];
	assert(myArray!=0);
}

// ---- 类析构函数的定义
DList::~DList(){
	delete [] myArray;
}

// ---- 复制构造函数的定义
DList::DList(const DList & origDList)
: mySize(origDList.mySize), myCapacity(origDList.myCapacity){
	// ---- 为复制获取新数组
	myArray = new (nothrow) ElementType[mySize];
	if(myArray!=0){  // 与assert(myArray!=0);效果相同
		// ---- 将origDList的元素复制到这个新数组中
		for(int i=0;i<mySize;++i){
			myArray[i] = origDList.myArray[i];
		}
	}else{
		cerr<<"Error\n";
		exit(1);
	}
}

// ---- 复制运算符的定义
const DList & DList::operator =(const DList & rightHandSide){
	if(this!=&rightHandSide){ // 不是自我赋值
		if(myCapacity!=rightHandSide.myCapacity){ // 如果容量不够,则重新分配
			delete[] myArray;
			myCapacity = rightHandSide.myCapacity;
			myArray = new (nothrow)ElementType[myCapacity];
			if(myArray!=0){
				// ---- 将rightHandSide的列表元素复制到这个新数组中
				mySize = rightHandSide.mySize;		// 这个才是元素的个数
			}else{
				cerr<<"Error\n";
				exit(1);
			}
		}
	}
	mySize = rightHandSide.mySize;
	for(int i=0;i<mySize;++i){
		myArray[i] = rightHandSide.myArray[i];
	}
	return *this;
}

// 判断是否为空empty() 的定义
bool DList::empty() const{
	return mySize ==0;
}

void DList::display(ostream &out) const
{
	for(int i=0;i < mySize;++i){
		out << myArray[i]<<"  ";
	}
}
void DList::insert(ElementType item,int pos){
	if(mySize == myCapacity){
		cerr << "NO SPACE\n";
		exit(1);
	}
	if(pos <0 || pos>mySize){
		cerr << "Illegal location\n";
		return;
	}

	// 从右往左将所有在pos前面的元素向右移动
	for(int i = mySize; i> pos; --i){
		myArray[i] = myArray[i-1];
	}
	// 将item插入到pos位置处,自增列表大小
	myArray[pos] = item;
	mySize++;
}

void DList::erase(int pos){
	if(mySize ==0){
		cerr<<"DList is empty\n";
		return;
	}
	if(pos<0|| pos>=mySize){
		cerr<<"Illegal location\n";
		return;
	}
	// 从左至右,将pos右方所有的值覆盖存储到左侧
	for(int i = pos; i<mySize;++i){
		myArray[i] = myArray[i+1];
	}
	mySize--;
}

ostream &operator<< (ostream & out, const DList & aDList){
	aDList.display(out);
	return out;
}



你可能感兴趣的:(数据结构,C++,动态数组,列表)