C++实现Vector、List

版权声明:允许转载,请注明文章出处 https://blog.csdn.net/Vickers_xiaowei/article/details/83153882

顺序表Vector

#include<assert.h>
#include<iostream>
using namespace std;

typedef int DataType;

class Vector{
public:
	Vector()
		:_first(NULL)
		,_finish(NULL)
		,_endof(NULL)
	{
	}
	~Vector()
	{
		if (_first)
		{
			delete[] _first;
			_first = _finish = _endof = NULL;
		}
	}
	Vector(const Vector& v)
	{
		_first = new DataType[v.Size()];
		_finish = _first + v.Size();
		_endof = _first + v.Size();

		memcpy(_first, v._first, sizeof(DataType)*v.Size());
	}
	size_t Size() const
	{
		return _finish - _first;
	}
	size_t Capacity() const
	{
		return _endof - _first;
	}
	//打印顺序表
	void Print()
	{
		DataType* cur = _first;
		while (cur != _finish)
		{
			cout << *cur << "	";
			cur++;
		}
		cout << endl;
	}
	void CheckCapacity()
	{
		if (_finish == _endof)
		{
			size_t capacity = Capacity();
			capacity>0 ? Expand(capacity * 2) : Expand(3);
		}
	}
	//增
	void Insert(size_t pos, DataType x)//在下标为pos的位置,插入数据x
	{
		CheckCapacity();
		DataType*cur = _finish;
		while (cur >_first + pos )
		{
			*cur = *(cur - 1);
			cur--;
		}
		*(_first + pos) = x;
		_finish++;
	}
	void PushBack(DataType x)
	{
		CheckCapacity();
		*_finish = x;
		++_finish;
	}
	//删
	void Erase(size_t n)//删除下标为n的数据
	{
		//assert(n > 0 && n < (size_t)_finish);
		if (_first+n<=_first||_first+n>_finish)//删除非法下标时候,抛异常
			throw  "删除了非法下标";
		DataType*cur = _first + n;
		while (cur < _finish)
		{
			*cur = *(cur + 1);
			cur++;
		}
		_finish--;
	}
	void PopBack()
	{
		if (_finish < _endof)
		{
			_finish--;
		}
	}
	//查询
	size_t Find(DataType x)//查找数据为x的数值的下标
	{
		size_t n = 0;
		while (_first+n < _finish)
		{
			if (*(_first + n) == x)
				return n;
			n++;
		}
		return -1;
	}
public:
	void Reverse(size_t n)//设置顺序表容量,数据长度不变
	{
		if (_first + n >= _finish)//新的容量大小必须不小于原来数据长度,否则可能丢失数据
		{
			Resize(n, Size());
		}
		else
		{
			return;
		}
	}
	void Resize(size_t n, DataType value)//设置顺序表数据长度(超出设置范围外的截断,丢弃)、容量
	{
		//开空间
		DataType* new_first = new DataType[n];
		DataType*new_endof = new_first + n;
		DataType* new_finish = new_first;

		//拷贝数据
		DataType*cur = _first;
		while (cur < _finish&&cur<_first+value)
		{
			*new_finish = *cur;
			cur++;
			new_finish++;
		}

		//释放旧空间
		delete[] _first;
		_first = _endof = NULL;

		_first = new_first;
		_finish = new_finish;
		_endof = new_endof;
	}
private:
	void Expand(size_t n)//扩容
	{
		if (n > Capacity())
		{
			Reverse(n);
		}
	}
private:
	DataType* _first;
	DataType* _finish;
	DataType* _endof;

};


void TestV()
{
	Vector v1;
	v1.PushBack(1);
	v1.PushBack(2);
	v1.PushBack(3);
	v1.PushBack(4);
	v1.Print();
	v1.Resize(8, 2);
	cout << "v1.Size():" << v1.Size() << endl;
	cout << "v1.Capacity():" << v1.Capacity() << endl;
	v1.Reverse(3);
	cout << "v1.Size():" << v1.Size() << endl;
	cout << "v1.Capacity():" << v1.Capacity() << endl;


	Vector v2(v1);
	v2.Print();
	try{
		v2.Erase(8);
	}
	catch (const char* msg)
	{
		cout << msg << endl;
	}
	v2.Print();
	v2.Insert(2, 3);
	v2.Insert(3, 4);
	v2.Print();
	cout << "找到顺序表v2中的数据3,它的下标是:"<<v2.Find(3) << endl;
}
1       2       3       4
v1.Size():2
v1.Capacity():8
v1.Size():2
v1.Capacity():3
1       2
删除了非法下标
1       2
1       2       3       4
找到顺序表v2中的数据3,它的下标是:2
请按任意键继续. . .

List带头双向链表(无死角)

#include<iostream>
using namespace std;
typedef int DataType;
struct Node{
	DataType _value;
	Node* _next;
	Node* _prev;
};
class List{
public:
	List()
	{
		_head = new Node(Node());//构造函数开辟头结点,头结点给个匿名对象
		_head->_next = _head;
		_head->_prev = _head;
	}
	~List()
	{
		SetEmpty();
		delete _head;
	}
	List(const List&l)
	{
		_head = new Node(Node());//构造函数开辟头结点,头结点给个匿名对象
		_head->_next = _head;
		_head->_prev = _head;

		Node*cur = l._head->_next;
		while (cur != l._head)
		{
			PushBack(cur->_value);
			cur = cur->_next;
		}
	}
	void SetEmpty()//将链表置空
	{
		if (_head)
		{
			Node* cur = _head->_prev;
			while (cur != _head)
			{
				Node*prev = cur->_prev;
				delete cur;
				cur = prev;
			}
		}
		_head->_next = _head;
		_head->_prev = _head;
	}
	List& operator=(List& l)
	{
		SetEmpty();
		Node*cur = l._head->_next;
		while (cur != l._head)
		{
			PushBack(cur->_value);
			cur = cur->_next;
		}
	}

	//增
	void Insert(Node* pos, DataType x)//在pos位置后添加数据x结点
	{
		Node*next = pos->_next;
		Node*node = new Node(Node());
		node->_value = x;

		pos->_next = node;
		node->_prev = pos;
		node->_next = next;
		next->_prev = node;
	}
	void PushBack(DataType x)
	{
		Insert(_head->_prev, x);
	}
	void PushFront(DataType x)
	{
		Insert(_head, x);
	}

	//删
	void Erase(Node*pos)//删除pos结点
	{
		Node*prev = pos->_prev;
		Node*next = pos->_next;

		delete pos;
		prev->_next = next;
		next->_prev = prev;
	}
	void PopBack()
	{
		Erase(_head->_prev);
	}
	void PopFront()
	{
		Erase(_head->_next);
	}
	

	//查找
	Node* Find(DataType x)
	{
		Node*cur = _head->_next;
		while (cur != _head)
		{
			if (cur->_value == x)
				return cur;
			cur = cur->_next;
		}
		return NULL;
	}
	//改
	void Change(Node*pos, DataType x)//把pos结点数据更换成x
	{
		pos->_value = x;
	}
	void Print()
	{
		Node*cur = _head->_next;
		while (cur != _head)
		{
			cout << cur->_value << "	";
			cur = cur->_next;
		}
		cout << endl;
	}
private:
	Node* _head;
};

void TestL()
{
	List l1;
	l1.PushBack(2);
	l1.PushFront(1);
	l1.PushBack(3);
	l1.PushBack(4);
	l1.Print();
	List l2(l1);
	l2.PopBack();
	l2.Print();
	List l3 = l2;
	l3.PopFront();
	l3.Print();
	l3.Change(l3.Find(2), 8);
	l3.Print();
}
1       2       3       4
1       2       3
2       3
8       3
请按任意键继续. . .

猜你喜欢

转载自blog.csdn.net/Vickers_xiaowei/article/details/83153882