模板实现顺序表和链表

顺序表的实现:"Seqlist.h" 

#pragma once

template<class T>
class Seqlist
{
private:
	T* _array;
	size_t _size;
	size_t _capacity;
public:
	Seqlist() :_array(NULL), _size(0), _capacity(0)//构造函数
	{

	}
	Seqlist(const Seqlist<T>& s)   //拷贝构造函数
	{
		if (s._size != 0)
		{
			_array = new T[s._size];
			for (size_t i = 0; i < s._size; i++)
			{
				_array[i] = s._array[i];
			}
		}
		else
		{
			_array = NULL;
		}
		_size = s._size;
		_capacity = s._capacity;
	}
	Seqlist<T>& operator= (Seqlist<T> s) //赋值运算符重载
	{
		swap(_array, s._array);
		swap(_size, s._size);
		swap(_capacity, s._capacity);
		return *this;
	}
	~Seqlist()                           //析构函数
	{
		if (_array)
		{
			delete[] _array;
			_array = NULL;
			_size = 0;
			_capacity = 0;
		}
	}
	void PushBack(const T& x)
	{
		Insert(_size, x);
	}
	void PopBack()
	{
		Erase(_size - 1);
	}
	void Insert(size_t pos,const T& x)
	{
		if (pos > _size)
		{
			return;
		}
		if (_size == _capacity)
		{
			size_t _newcapacity = 0;
			if (_capacity == 0)
				_newcapacity = 3;
			else
				_newcapacity = _capacity * 2;
			T* _newarray = new T[_newcapacity];
			for (size_t i = 0; i < _size; i++)
			{
				_newarray[i] = _array[i];
			}
			delete[] _array;
			_array = _newarray;
			_capacity = _newcapacity;
		}
		for (size_t i = _size; i>pos; i--)
		{
			_array[i] = _array[i - 1];
		}
		_array[pos] = x;
		++_size;
	}
	void Erase(size_t pos)
	{
		if (pos >= _size)
		{
			return;
		}
		for (size_t i = pos; i < _size - 1; i++)
		{
			_array[i] = _array[i + 1];
		}
		--_size;
	}
	size_t Find(const T& x)
	{
		size_t i = 0;
		while (i < _size)
		{
			if (_array[i] == x)
			{
				return i;
			}
		}
	}
	size_t Size()
	{
		return _size;
	}
	bool Empty()
	{
		return Size() == 0;
	}
	T& operator[](size_t pos)
	{
		return _array[pos];
	}
	void Print()
	{
		for (size_t i = 0; i < _size; i++)
		{
			cout << _array[i] << " ";
		}
		cout << endl;
	}

};

void TestSeqlist()
{
	Seqlist<int> s1;
	s1.PushBack(1);
	s1.PushBack(2);
	s1.PushBack(3);
	s1.PushBack(4);
	s1.PushBack(5);
	s1.Print();
	s1.Insert(3, 10);
	s1.Print();
	//s1.Erase(3);
	//s1.Print();
	std::cout << s1[3] << std::endl;
	Seqlist<int> s2(s1);
	s2.Print();

	Seqlist<int> s3;
	s3 = s1;
	s3.Print();
}

链表的实现:"list.h"

#pragma once
template<class T>
struct ListNode
{
	T _data;
	ListNode<T>* _next;
	ListNode<T>* _prev;
};
template<class T>
class list
{
	typedef ListNode<T> Node;
public:
	list()
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
	}
	~list()
	{
		Node* cur = _head->_next;
		while (cur != _head)
		{
			_head->_next = cur->_next;
			delete cur;
			cur = _head->_next;
		}
		delete _head;
		_head = NULL;
	}
	list(const list<T>& lt)    //拷贝构造函数
	{
		Node* cur = lt._head->_next;
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
		while (cur!=lt._head)
		{
			PushBack(cur->_data);
			cur = cur->_next;
		}
	}
	list<T>& operator=(list<T> lt)
	{
		swap(_head, lt._head);
		return *this;
	}
	void PushBack(const T& x)
	{
		Insert(_head, x);
	}
	void PopBack()
	{
		Erase(_head->_prev);
	}
	void PushFront(const T& x)
	{
		Insert(_head->_next, x);
	}
	void PopFront()
	{
		Erase(_head->_next);
	}
	void Insert(Node* pos, const T& x)
	{
		Node* prev = pos->_prev;
		Node* newnode = new Node;
		newnode->_data = x;
		prev->_next = newnode;
		newnode->_prev = prev;
		newnode->_next = pos;
		pos->_prev = newnode;
	}
	void Erase(Node* pos)
	{
		pos->_prev->_next = pos->_next;
		pos->_next->_prev = pos->_prev;
		delete pos;
		pos = NULL;
	}
	Node* Find(const T& x)
	{
		Node* cur = _head->_next;
		while (cur != _head)
		{
			if (cur->_data == x)
				return cur;
			else
				cur = cur->_next;
		}
		return NULL;
	}
	size_t Size()
	{
		size_t size = 0;
		Node* cur = _head->_next;
		while (cur != _head)
		{
			++size;
			cur = cur->_next;
		}
		return size;
	}
	bool Empty()
	{
		return Size() == 0;
	}
	void Print()
	{
		Node* cur = _head->_next;
		while (cur != _head)
		{
			cout << cur->_data << " ";
			cur = cur->_next;
		}
		cout << endl;
	}
	void Test()
	{
		Node* cur = Find(3);
		Erase(cur);
	}
private:
	Node* _head;
};
void Testlist()
{
	list<int> L;
	L.PushBack(1);
	L.PushBack(2);
	L.PushBack(3);
	L.PushBack(4);
	L.PushBack(5);
	L.Print();
	//L.PopBack();
	//L.Print();
	//L.PopFront();
	//L.Print();
	L.Test();
	L.Print();
	std::cout << L.Size() << std::endl;
	//list<int> L2(L);
	//L2.Print();

	/*list<int> L3;
	L3 = L;
	L3.Print();*/
}

 main.cpp:

#include<iostream>
#include"Seqlist.h"
#include"list.h"

using namespace std;
int main()
{
	TestSeqlist();
	Testlist();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/smx_dd/article/details/83096307
今日推荐