C++实现顺序表简单功能

实现代码:

typedef int DataType;

class SeqList
{
private:
	DataType* _a;
	size_t _size;
	size_t _capacity;

	void CheckCapcacity()
	{
		if (_size == _capacity)
		{
			size_t NewCapacity = _capacity ? _capacity * 2 : 3;
			_a = (DataType*)realloc(_a, NewCapacity*sizeof(DataType));
			assert(_a);
			_capacity = NewCapacity;
		}
	}

	
public:
	SeqList()
		:_a(NULL)
		, _size(0)
		, _capacity(0)
	{}
	SeqList(const SeqList& s)
	{
		_a = (DataType*)malloc(s._size*sizeof(DataType));
		memcpy(_a, s._a, sizeof(DataType)*s._size);
		_size = s._size;
		_capacity = s._capacity;
	}
	/*SeqList& operator=(const SeqList& s)
	{
		if (this != &s)
		{
			free(_a);
			_a = (DataType*)malloc(s._size*sizeof(DataType));
			memcpy(_a, s._a, sizeof(DataType)*s._size);
			_size = s._size;
			_capacity = s._capacity;
		}
		return *this;
	}*/
	SeqList& operator=(SeqList s)//若传引用会改变引用对象的值
	{
		swap(_a, s._a);
		swap(_size, s._size);
		swap(_capacity, s._capacity);
		return *this;
	}
	~SeqList()
	{
		if (_a)
		{
			free (_a);
			_a = NULL;
		}
	}

	
	void PushBack(DataType x)
	{
		CheckCapcacity();
		_a[_size++] = x;
	}
	void PopBack()
	{
		if (_size > 0)
		{
			_size--;
		}
		else
		{
			cout << "顺序表为空" << endl;
		}
	}
	void PushFront(DataType x)
	{
		/*CheckCapcacity();
		DataType end = (DataType)_size;
		for (; end >= 0;--end)
		{
			_a[end + 1] = _a[end];
		}
		_a[0] = x;
		_size++;*/
		Insert(0, x);
	}
	void PopFront()
	{
		Erase(0);
		/*if (_size > 0)
		{
			DataType i = 0;
			for (; i < (int)_size;i++)
			{
				_a[i] = _a[i+1];
			}
			_size--;
		}
		else
		{
			cout << "顺序表为空" << endl;
		}*/
	}
	void Insert(size_t pos, DataType x)
	{
		CheckCapcacity();
		if (pos<0 || pos>_size)
		{
			return;
		}
		DataType end = (DataType)_size-1;
		for (; end >=(DataType)pos; end--)
		{
			_a[end+1] = _a[end];
		}
		_a[pos] = x;
		_size++;

	}
	void Erase(size_t pos)
	{
		if (_size > 0)
		{
			if (pos<0 || pos>_size)
			{
				return;
			}
			else
			{
				DataType i = (DataType)pos;
				for (; i < (DataType)_size; i++)
				{
					_a[i] = _a[i + 1];
				}
				_size--;
			}
		}
		else
		{
			cout << "顺序表为空" << endl;
		}

	}
	DataType& operator[](size_t pos)
	{
		assert(pos < _size);
		return _a[pos];
	}
		
		void Print()
		{
			if (_size)
			{
				DataType i = 0;
				for (i = 0; i < (int)_size; i++)
				{
					cout << _a[i] << " ";
				}
				cout << endl;
			}
			else
			{
				cout << "顺序表为空" << endl;
			}
		}

};

测试代码:

#include"Seqlist.h"

void Test()
{
	SeqList s1;
	s1.PushBack(1);
	s1.PushBack(2);
	s1.PushBack(3);
	s1.PushBack(4);
	s1.Print();

	//s1.Insert(2, 6);
	//s1.Print();

	/*s1.PushFront(1);
	s1.PushFront(2);
	s1.PushFront(3);
	s1.PushFront(4);
	s1.Print();*/

	/*s1.Erase(1);
	s1.Print();*/

	s1.PopFront();
	s1.Print();

	/*s1.PopBack();
	s1.PopBack();
	s1.PopBack();
	s1.PopBack();
	s1.Print();
*/
	/*SeqList s2(s1);
	s2.Print();
	SeqList s3;
	s3 = s1;
	s3.Print();*/
}
int main()
{
	Test();
	system("pause");
	return 0;
}


使用c++实现顺序表:多文件编程,层次清晰,函数有注释 SeqList();//构造函数,存储的元素个数设为0 bool setLength(size_t length);//设置已经存储的元素个数 bool addElement(ElemType element);//把某个元素添加到顺序表末尾 bool addElement(ElemType element , size_t n);//插入一个元素,使其成为第n个元素,其余元素后移 bool delElement();//删除所有的元素 bool delElement(size_t n);//删除第n个元素 bool delElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,把这个元素删除 bool replaceElement(ElemType element , size_t n);//使用一个元素,替换掉第n个元素 bool swapElement(size_t n1 , size_t n2);//把第n1个元素和第n2个元素交换 ElemType* getElement();//得到数组头的指针 ElemType* getElement(size_t n);//得到第n个元素的指针 size_t getLength();//得到存储的元素个数 size_t getMaxSize();//得到顺序表容量 bool showElementDetail();//输出所有的元素细节 bool showElementDetail(size_t n);//输出第n个元素的细节 bool showElementDetail(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,输出元素所有细节 size_t findElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素位置 static int inputAInt(int min = 0,int max = 9,int defaultValue = -1);//从键盘读取,限制为一个min到max间的整数,非法情况返回defaultValue void startControlLoop();//打开控制界面 ~SeqList();//析构函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值