模板实现顺序表和链表并栈和队列

本文介绍了自定义实现向量、链表、栈和队列等基本容器类的过程,包括类的设计、成员函数的功能及其实现细节。通过这些自定义容器类,读者可以深入理解容器内部的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vector.h

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

//类名 Vector
//类型 Vector<T>
template<class T>
class Vector
{
public:
	Vector()//构造函数(构造函数名与类名相同)
		:_start(NULL)
		, _finish(NULL)
		, _endofstorage(NULL)
	{}

	Vector(const Vector<T>& v)//拷贝构造函数
	{}//深拷贝
	Vector<T>& operator=(const Vector<T>& v)//赋值运算符
	{}

	~Vector()//析构函数
	{
		delete[] _start;
		_start = _finish = _endofstorage = NULL;
	}

	void PushBack(const T x)//尾插
	{
		Insert(Size(), x);
	}
	void PopBack()//尾删
	{
		Erase(Size() - 1);
	}
	void Insert(size_t pos,T x)//在pos位置上插入x
	{
		assert(pos <= Size());

		//查容
		if (Size() >= Capacity())
		{
			Expand(Capacity() * 2);
		}

		//搬移数据
		//int end = Size() - 1;
		//while (end >= (int)pos)//隐式类型转换
		//{
		//	_start[pos + 1] = _start[pos];
		//	end--;
		//}
		T* end = _finish - 1;
		while (end >= _start + pos)
		{
			*(end + 1) = *end;
			--end;
		}

		//插入数据
		_start[pos] = x;
		++_finish;
	}
	void Erase(size_t pos)//删除pos位置上的数据
	{
		assert(pos < Size());

		T* cur = _start + pos + 1;//后一个位置
		while (cur != _finish)
		{
			*(cur - 1) = *cur;
			--cur;
		}
		--_finish;
	}

	size_t Size()//求顺序表的长度
	{
		return _finish - _start;
	}

	size_t Capacity()
	{
		return _endofstorage - _start;
	}

	bool Empty()//判空
	{
		return _start == _finish;
	}
	size_t Find(const T& x)
	{
		T* cur = _start;
		while (cur != _finish)
		{
			if (*cur == x)
			{
				return cur - _start;
			}
			++cur;
		}
		return -1;
	}
	void print()
	{
		T* cur = _start;
		while (cur != _finish)
		{
			cout << *cur << " ";
			++cur;
		}
		cout << endl;
	}

	//适用于Stack和Queue
	T& Back()
	{
		return *(_finish - 1);
	}
	/*const T& Back()
	{
		return *(_finish - 1);
	}*/
	T& operator[](size_t index)
	{
		return _start[index];
	}
protected:
	void Expand(size_t n)//扩容
	{
		if (Empty())//原来没有空间
		{
			_start = new T[3];
			_finish = _start;
			_endofstorage = _start + 3;
		}
		else if (n > Capacity())
		{
			T* tmp = new T[n];
			/*memcpy(tmp, _start, sizeof(T)*Size());*/
			size_t size = Size();
			for (size_t i = 0; i < size; i++)
			{
				tmp[i] = _start[i];
			}

			delete[] _start;//释放旧空间
			_start = tmp;//_start指向新空间的起始位置
			_finish = _start + Size();
			_endofstorage = _start + n;
		}
	}
protected:
	T* _start;
	T* _finish;
	T* _endofstorage;
};

//template<class T>
//Vector<T>::Vector()
//	:_start(NULL)
//	, _finish(NULL)
//	, _endofstorage(NULL)
//{}


void TestVector()
{
	Vector<int> v1;
	v1.PushBack(1);
	v1.PushBack(2);
	v1.PushBack(3);
	v1.PushBack(4);
	v1.print();

	v1.PopBack();
	v1.print();

}

List.h

//模板双向带头链表
#include<iostream>
using namespace std;
#include<assert.h>

template<class T>
struct ListNode
{
	ListNode<T>* _next;
	ListNode<T>* _prev;
	T _data;

	ListNode(const T& x)
		:_data(x)
		,_next(NULL)
		,_prev(NULL)
	{}
};
template<class T>
class List
{
	typedef ListNode<T> Node;
public:
	List()//构造函数
	{
		_head = new Node(T());
		_head->_next = _head;
		_head->_prev = _head;
	}
	~List()//析构函数
	{
		Clear();
		delete _head;
		_head = NULL;
	}
	List(const List<T>& l);//拷贝构造
	List<T>& operator=(const List<T>& l);//赋值运算符

	void PushBack(const T x)//尾插
	{
		Insert(_head, x);//_head->prev是最后一个节点
	}
	void PopBack()//尾删
	{
		Erase(_head->_prev);
	}
	void PushFront(const T x)//头插
	{
		Insert(_head->_next, x);
	}
	void PopFront()//头删
	{
		Erase(_head->_next);
	}
	void Insert(Node* pos, T x)//在pos位置上插入x
	{
		assert(pos);//断言pos位置是否正确

		Node* prev = pos->_prev;//找pos位置
		Node* newnode = new Node(x);//创建新结点

		prev->_next = newnode;
		newnode->_prev = prev;
		newnode->_next = pos;
		pos->_prev = newnode;
	}

	void Erase(Node* pos)//删除位置pos上的数据
	{
		assert(pos && pos != _head);

		Node* prev = pos->_prev;
		Node* next = pos->_next;

		delete pos;
		prev->_next = next;
		next->_prev = prev;
	}
	Node* Find(const T& x)//查找
	{
		Node* cur = _head->_next;
		while (cur != _head)
		{
			if (cur->_data == x)
			{
				return cur;
			}
			cur = cur->_next;
		}
		return NULL;
	}
	void print()
	{
		Node* cur = _head->_next;
		while (cur != _head)
		{
			cout << cur->_data << " ";
			cur = cur->_next;
		}
		cout << endl;
	}
	bool Empty()//判空
	{
		return _head->_next == _head;
	}
	size_t Size()
	{
		size_t count = 0;
		Node* cur = _head->_next;
		while (cur != _head)
		{
			cur = cur->_next;
			count++;
		}
		return count;
	}
	T& Back()
	{
		return _head->_prev->_data;
	}
	/*const T& Back()
	{
		return _head->_prev->_data;
	}*/
	T& Front()
	{
		return _head->_next->_data;
	}
	/*const T& Front()
	{
		return _head->_next->_data;
	}*/
protected:
	void Clear()//置空
	{
		Node* cur = _head->_next;
		while (cur != _head)
		{
			Node* next = cur->_next;
			delete cur;
			cur = next;
		}
	}
protected:
	Node* _head;//头结点
};
void TestList()
{
	//List<string> l1;
	//l1.PushBack("111");
	//l1.PushBack("222");
	//l1.PushBack("333");
	//l1.PushBack("444");
	//l1.print();

	List<int> l2;
	l2.PushBack(1);
	l2.PushBack(2);
	l2.PushBack(3);
	l2.PushBack(4);
	l2.print();
}

Stack.h

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

template<class T, class container = Vector<T>>//默认Vector
class Stack
{
public:
	void Push(const T& x)
	{
		_con.PushBack(x);
	}
	void Pop()
	{
		_con.PopBack();
	}

	T& Top()//取栈顶元素
	{
		return _con.Back();
	}
	/*const T& Top()const
	{
		return _con.Back();
	}*/
	size_t Size()//求长度
	{
		return _con.Size();
	}
	bool Empty()//判空
	{
		return _con.Empty();
	}
protected:
	container _con;
};
void TestStack()
{
	Stack<int,List<int>>  s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	while (!s1.Empty())
	{
		cout << s1.Top() << " ";
		s1.Pop();
	}
	cout << endl;
}

Queue.h

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

template<class T,class container = List<T>>//默认Vector
class Queue
{
public:
	void Push(const T& x)
	{
		_con.PushBack(x);
	}
	void Pop()
	{
		_con.PopFront();
	}
	T& Front()//取队头元素
	{
		return _con.Front();
	}
	//const T& Front()//取队头元素
	//{
	//	return _con.Front();
	//}
	size_t Size()//求长度
	{
		return _con.Size();
	}
	bool Empty()//判空
	{
		return _con.Empty();
	}
protected:
	container _con;
};

void TestQueue()
{
	Queue<int, List<int>>  q1;
	q1.Push(1);
	q1.Push(2);
	q1.Push(3);
	q1.Push(4);
	while (!q1.Empty())
	{
		cout << q1.Front() << " ";
		q1.Pop();
	}
	cout << endl;
}

test.cpp

#include"Vector.h"
#include"List.h"
#include"Stack.h"
#include"Queue.h"

int main()
{
	//TestVector();
	//TestList();
	//TestStack();
	TestQueue();

	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值