C++实现顺序表及双向链表

顺序表

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

class SeqList
{
public:
	//默认的构造函数,并初始化列表
	SeqList()
		:_array(new DataType[3])//直接开辟空间
		, _size(0)
		, _capacity(3)
	{}
	//已有信息的构造函数,并拷贝信息到新空间上
	SeqList(DataType* array, size_t size)
		:_array(new DataType[size])
		, _size(size)
		, _capacity(size)
	{
		for (size_t i = 0; i < size; i++)
		{
			_array[i] = array[i];
		}
	}

	void PushBack(DataType data)
	{
		CheckCapacity();
		_array[_size++] = data;
	}

	void PopBack()
	{
		assert(_size);  //断言,当元素为0,再尾删进行报错
		_size--;
	}

	//增容,开辟新空间,拷贝元素,释放旧空间
	void CheckCapacity()
	{
		if (_size == _capacity)
		{
			DataType* temp = new DataType[_capacity << 1];
			if (temp == NULL)
			{
				perror("temp");
				exit(EXIT_FAILURE);
			}
			for (int i = 0; i < _size; i++)
			{
				temp[i] = _array[i];
				//temp[i++] = _array[i++];严重错误代码,i加了两次
			}
			delete[] _array;
			_array = temp;
			_capacity <<= 1; // 左移一位相当于乘以二
		}
	}

	//指定位置(下标)插入一个元素
	void Insert(size_t pos, DataType data)
	{
		assert(pos <= _size);
		CheckCapacity();
		for (int i = _size; i > pos; i--)
		{
			_array[i] = _array[i - 1];
		}
		_array[pos] = data;
		_size++;//插入一个元素,_size增大
	}

	//指定位置删除
	void Erase(size_t pos)
	{
		assert(pos < _size);
		for (int i = pos; i < _size - 1; i++)
		{
			_array[i] = _array[i + 1];
		}
		_size--;
	}

	size_t Size()const
	{
		cout << endl;
		cout << _size << endl;
		return _size;
	}

	size_t Capacity()const
	{
		cout << _capacity << endl;
		return _capacity;
	}

	//判断顺序表是否为空
	bool Empty()const
	{
		if (_size == 0)
		{
			cout << 0;
			return false;
		}
		else
		{
			cout << 1;
			return true;
		}

	}

	//输入一个下标,返回下标对应的数据(又能读又能写)
	DataType& operator[](size_t index)
	{
		return _array[index];
	}

	//与上一个函数成对出现,思考?什么场景下使用此函数(只读)
	const DataType& operator[](size_t index)const
	{
		return _array[index];
	}

	//返回顺序表第一个元素
	DataType& Front()
	{
		return _array[0];
	}

	const DataType& Front()const
	{
		return _array[0];
	}

	//返回顺序表最后一个元素
	DataType& Back()
	{
		return _array[_size - 1];
	}

	const DataType& Back()const
	{
		return _array[_size - 1];
	}

	void Display()
	{
		if (_size != 0)
		{
			for (int i = 0; i < (int)_size; i++)
			{
				cout << _array[i] << " ";
			}
		}
		cout << "" << endl;
	}

	void Clear()
	{
		_size = 0;
	}

	//逆序顺序表
	void Reserve()
	{
		DataType* left = _array;
		DataType* right = _array + _size - 1;
		while (left <= right)
		{
			DataType tmp = *left;
			*left = *right;
			*right = tmp;
			left++;
			right--;
		}
	}

	//将元素表中的个数调整为newSize
	void ReSize(size_t newSize, const DataType& data = DataType())
	{
		if (newSize <= _size)
		{
			_size = newSize;
		}
		else if (newSize <= _capacity)
		{
			for (int i = _size; i < newSize; ++i)
			{
				_array[i] = data;
			}
			_size = newSize;
		}
		else
		{
			for (int i = _size; i < newSize; i++)
			{
				CheckCapacity();
				_array[i] = data;
				_size++;  //当比容量大的时候,让_size自增,到达容量时,自增
			}
		}
	}

	//拷贝构造函数(深拷贝)
	SeqList(const SeqList& s)
		: _array(new DataType[s._size])
		, _size(s._size)
		, _capacity(s._capacity)
	{
		for (size_t i = 0; i < s._size; i++)
		{
			_array[i] = s._array[i];
		}
	}

	//赋值运算符的重载
	SeqList& operator=(const SeqList& s)
	{
		if (this != &s)
		{

			for (size_t i = 0; i < s._size; i++)
			{
				_array[i] = s._array[i];
			}
			_size = s._size;
			_capacity = s._capacity;
		}
		return *this;
	}

	friend ostream& operator<<(ostream& cout, const SeqList& s)
	{
		for (int i = 0; i < s._size; i++)
		{
			cout << s._array[i]<<" ";
		}
		cout << s._size << " " << s._capacity << endl;
		return cout;
	}

	~SeqList()
	{
		if (_array)
		{
			delete[] _array;
			_array = NULL;
			_size = 0;
			_capacity = 0;
		}
	}

private:
	DataType* _array;
	size_t _size;
	size_t _capacity;
};

void Funtest1()
{
	int k[5] = { 1, 2, 3, 4, 5 };
	SeqList d1;
	SeqList d2(k,5);
	d1.PushBack(1);
	d1.PushBack(2);
	d1.PushBack(3);
	d1.Insert(2, 5);
	d1.Insert(1, 3);
	d1.Display();
	SeqList d3(d2);
	d3.Display();
	SeqList d4 = d3;
	d4.Display();
	/*d1.Erase(2);
	d1.Display();
	d1.Size();
	d1.Capacity();
	d1.Empty();*/
	//d2.Display();
	//SeqList d3(d2);
	cout << d4;
}

void Funtest2()
{
	int k[5] = { 1, 2, 3 };
	SeqList d1(k, 3);
	d1[0] = 2;
	d1.Display();
	cout << d1.Front() << endl;
	cout << d1.Back() << endl;
	d1.Reserve();
	d1.Display();
	d1.ReSize(7,7);
	d1.Display();
	


}

int main()
{
	Funtest1();
	//Funtest2();
	getchar();
	return 0;
}

双向链表:

1.在Find中,查找到后返回当前节点的下一个节点;

2.在Erase中,需要知道pos节点的数据,接收Find查找到数据的地址,作为pos。

#include <iostream>
#include <assert.h>
using namespace std;
typedef int DataType;
//双向链表

struct Node
{
	Node(const DataType& data)
	: _pNext(NULL)
	, _pPre(NULL)
	, _data(data)
	{}
	Node* _pNext;
	Node* _pPre;
	DataType _data;
};

class List
{
public:
	List()
		:_pHead(NULL)
	{}

	List(DataType* array, size_t size)
	{
		for (size_t i = 0; i < size; ++i)
			PushBack(array[i]);
	}

	~List()
	{
		if (_pHead == NULL)
		{
			return;
		}
		Node* _head = _pHead;
		Node* _tail = NULL;
		while (_head->_pNext)
		{
			_head = _head->_pNext;
		}
		while (_head != _pHead)  //删除最后一个节点,_tail往前移
		{
			_tail = _head->_pPre;
			delete _head;
			_head = _tail;
		}
		_pHead = NULL;
	}

	Node* BuyNode(const DataType data)
	{
		return new Node(data);
	}

	void PushBack(const DataType data)
	{
		if (_pHead == NULL)
		{
			_pHead = BuyNode(data);
			_pHead->_pPre = NULL;
			_pHead->_pNext = NULL;
			_pHead->_data = data;
		}
		else
		{
			Node* _head = _pHead;
			while (_head->_pNext)
			{
				_head=_head->_pNext;
			}
			_head->_pNext = BuyNode(data);
			_head->_pNext->_pPre = _head;
			_head->_pNext->_pNext = NULL;
		}
	}

	void PopBack()
	{
		if (_pHead == NULL)
		{
			return;
		}
		if (_pHead->_pNext == NULL)
		{
			_pHead= NULL;
			return;
		}
		Node* _head = _pHead;
		while (_head->_pNext)
		{
			_head = _head->_pNext;
		}
		_head->_pPre->_pNext = NULL;
	}

	void PushFront(const DataType data)
	{
		if (_pHead == NULL)
		{
			_pHead = BuyNode(data);
			_pHead->_pNext = NULL;
			_pHead->_pPre = NULL;
			_pHead->_data = data;
		}
		else
		{
			_pHead->_pPre = BuyNode(data);
			_pHead->_pPre->_pNext = _pHead;
			_pHead = _pHead->_pPre;
			_pHead->_pPre = NULL;
		}
	}

	void PopFront()
	{
		if (_pHead == NULL)
		{
			return;
		}
		if (_pHead->_pNext == NULL)
		{
			_pHead = NULL;
			return;
		}
		_pHead = _pHead->_pNext;
		_pHead->_pPre = NULL;
	}

	//指定位置删除,没有节点,删头结点,删尾节点,非头非尾,返回pos的后一个节点
	Node* Erase(Node* pos)
	{
		assert(pos);
		if (_pHead == NULL)
			return NULL;
		if (pos == _pHead)
		{
			PopFront();
		}
		else
		{
			Node* _head = _pHead;
			while (_head->_pNext)
				_head = _head->_pNext;
			if (pos == _head)
				PopBack();
		}
		Node* _head = _pHead;
		while (_head->_pNext)
		{
			_head = _head->_pNext;
			if (_head==pos)
			{
				pos->_pPre->_pNext = pos->_pNext;
				pos->_pNext->_pPre = pos->_pPre;
				delete pos;
				return _head->_pNext;
			}
		}
	}

	Node* Insert(Node* pos, const DataType& data)
	{
		assert(pos);
		if (pos == _pHead)
		{
			PushFront(data);
			return pos->_pNext;
		}
		Node* newnode=BuyNode(data);//既不是头结点也不是尾节点
		pos->_pPre->_pNext = newnode;
		newnode->_pNext = pos;
		newnode->_pPre = pos->_pPre;
		pos->_pPre = newnode;
		newnode->_data = data;
		return pos->_pNext;
	}

	Node* Find(const DataType& data)
	{
		Node* _head = _pHead;
		if (_head == NULL)
		{
			return NULL;
		}
		while (_head->_pNext)
		{
			if (_head->_data == data)
			{
				return _head;
			}
			_head = _head->_pNext;
		}
		if (_head->_data == data)
		{
			return _head;
		}
		return NULL;
	}

	size_t Size()
	{
		int count=1;
		if (_pHead == NULL)
		{
			return 0;
		}
		else
		{
			while (_pHead->_pNext!= NULL)
			{
				_pHead = _pHead->_pNext;
				++count;
			}
		}
		return count;
	}

	//从后往前删,找出最后节点的前一个节点为tail,删除最后一个节点
	void Clear()
	{
		if (_pHead == NULL)
		{
			return;
		}
		Node* _head = _pHead;
		Node* _tail = NULL;
		while (_head->_pNext)
		{
			_head = _head->_pNext;
		}
		while (_head!= _pHead)  //删除最后一个节点,_tail往前移
		{
			_tail = _head->_pPre;
			delete _head;
			_head = _tail;
		}
		_pHead = NULL;
	}

	void Display()
	{
		if (_pHead == NULL)
		{
			return;
		}
		Node* head = _pHead;
		while (head)// 最后一个节点的下一指针指向空
		{

			cout << head->_data << " " ;
			head=head->_pNext;
		}
		cout << endl;
	}

	bool Empty()
	{
		if (_pHead == NULL)
		{
			return true;
		}
		return false;
	}

private:
	Node* _pHead;
};

void Funtest()
{
	List L1;
	L1.PushBack(1);
	L1.PushBack(2);
	L1.PushBack(3);
	/*L1.PushBack(4);
	L1.PushFront(2);
	L1.PushFront(3);
	L1.PushFront(4);*/
	//L1.Display();
	/*L1.PopFront();
	L1.PopFront();
	L1.PopFront();*/
	//L1.Clear();
	L1.Display();
	/*L1.Erase(L1.Find(3));*/
	L1.Insert(L1.Find(3),4);
	L1.Display();
	int arr[] = { 1, 2, 3, 4, 5 };
	List L2(arr, 5);
	L2.Display();
}

int main()
{
	Funtest();
	getchar();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值