MyList 双链表

1.改进完善迭代器

2.了解xmonery这个类,改进自己内存管理。


#define ZB_MYLIST_
#ifdef ZB_MYLIST_
#include "debug.h"
#include <xstddef>

class AllocMyList
{
public:
	AllocMyList(){}
	~AllocMyList(){}
	void* Allocator(size_t len) { return new char[len]; }
	void Free(void *ptr, size_t len = 0) { delete[] ptr; }
	void Swip(){}

	template<class _Objty,
	class... _Types>
		void construct(_Objty *_Ptr, _Types&&... _Args)
	{	// construct _Objty(_Types...) at _Ptr
		::new ((void *)_Ptr) _Objty(_STD forward<_Types>(_Args)...);
	}
};

template<typename T, typename Alloc = AllocMyList>
class MyList;

template<typename T>
struct Node
{
	typedef T value_type;
	typedef Node<T>* node_printer;
public:
	Node()
		:Data(), pPrev(nullptr), pNext(nullptr){}

	Node(value_type data) 
		:Data(data), pPrev(nullptr), pNext(nullptr){}

	Node(value_type data, node_printer prev, node_printer next)
		:Data(data), pPrev(prev), pNext(next){}

	bool operator==(Node& Rigth)
	{
		return (this->Data == Rigth.Data &&
				this->pPrev == Rigth.pPrev &&
				this->pNext == Rigth.pNext);
	}

	bool operator!=(Node& Right)
	{
		return !(*this == Right);
	}

	value_type Data;
	node_printer pPrev;
	node_printer pNext;
};


template<typename T>
class TIterator
{
	typedef T value_type;
	typedef Node<T> node_type;
	typedef Node<T>* node_printer;
	typedef MyList<T>* hash_printer;
	typedef TIterator<T> iterator;
public:
	TIterator():m_pNode(nullptr), m_pSelf(nullptr) {}

	TIterator(node_printer pNode, hash_printer pList)
		:m_pNode(pNode), m_pSelf(pList){}

	node_printer GetNode()
	{
		return m_pNode;
	}

	value_type GetData()
	{
		return m_pNode->Data;
	}

	iterator& operator++()
	{
		ASSERT (m_pSelf->GetCount == 0 ||
			m_pNode->pNext == nullptr);

		this->m_pNode = this->m_pNode->pNext;
		return *this;
	}

	iterator operator++(int)
	{
		TIterator temp = this;
		++this;
		return temp;
	}

	iterator& operator--()
	{
		ASSERT(m_pSelf->size() == 0 ||
			m_pNode->pNext == nullptr);

		return *this;
	}

	iterator operator--(int)
	{
		iterator temp = this;
		--this;
		return temp;
	}
	bool operator==(TIterator& Right)
	{
		return (this->m_pNode == Right.m_pNode);
	}

	bool operator!=(TIterator& Right)
	{
		return !(*this == Right);
	}
private:
	node_printer m_pNode;
	hash_printer m_pSelf;
};

template<typename T, typename Alloc = AllocMyList>
class MyList
{
	typedef T value_type;
	typedef T* value_printer;
	typedef T& reference;
	typedef Node<T> node_type;
	typedef Node<T>* node_printer;
	typedef TIterator<T> iterator;
	typedef size_t size_type;
public:
	MyList()
	{ 
		MyHead = Buy();
		MySize = 0;
	}

	MyList(size_type nCount)
	{
		
	}
	
	MyList(size_type nCount, value_type Val)
	{
		Construct_n(nCount, Val);
	}

	// 返回当前对象个数  
	size_type size() const { return (this->MySize); }
	size_type max_size() const { return ((size_type)(-1) / sizeof(value_type)); }//*
	// 提供访问函数  
	reference front(){ return (*begin()); }
	reference back() { return (*(end() - 1)); }
	// 获取几种迭代器  
	iterator begin() const
	{ 
		return iterator(this->MyHead->pNext, this);
	}

	iterator end() const
	{ 
		return iterator(this->MyHead, this); 
	}

protected:
	static node_printer& Nextnode(node_printer pNode)
	{
		return ((node_printer&)pNode->pNext);
	}

	static node_printer& Prevnode(node_printer pNode)
	{
		return ((node_printer&)pNode->pPrev);
	}

	static reference Myval(node_printer pNode)
	{
		return ((reference)pNode->Data);
	}

private:
	void Construct_n(size_type nCount, value_type Val)
	{
		Insert_n(begin(), nCount, Val);
	}
	
	void Insert_n(iterator Where, size_type nCount, value_type Val)
	{
		size_type nSaveCount = nCount;
		_TRY_BEGIN
		for (; nCount > 0; --nCount)
		{
			Inser(Where, Val);
		}
		_CATCH_ALL
		for (;nCount < nSaveCount; ++nCount)
		{
			iterator Before = Where;
			uncheck_erase(--Before);
		}
		_RERAISE;
		_CATCH_END
	}

	void uncheck_erase(iterator Where)
	{
		node_printer pNode = Where.GetNode();
		this->Nextnode(this->Prevnode(pNode)) = this->Nextnode(pNode);
		this->Prevnode(this->Nextnode(pNode)) = this->Prevnode(pNode);
		this->MySize--;
	}

	void Inser(iterator Where, value_type Val)
	{
		node_printer pNode = Where.GetNode();
		node_printer new_node = BuyNode(Prevnode(pNode), pNode, Val);
		IncSize(1);
		this->Prevnode(pNode) = new_node;
		this->Nextnode(this->Prevnode(new_node)) = new_node;
	}

	Alloc& Getal()
	{
		return (this->alloc);
	}

	Alloc& Getal() const
	{
		return (this->alloc);
	}

	node_printer BuyNode(node_printer prev, node_printer next, value_type data)
	{
		node_printer temp = static_cast<node_type*>(this->Getal().Allocator(sizeof(data)));
		if (*next == node_type())
		{
			prev = temp;
			next = temp;
		}
		_TRY_BEGIN
		this->Getal().construct(std::addressof(Prevnode(temp)), prev);
		this->Getal().construct(std::addressof(Nextnode(temp)), next);
		_CATCH_ALL
		this->Getal().Free(temp);
		_RERAISE;
		_CATCH_END
		return temp;
	}

	void IncSize(size_type nCount)
	{
		if (max_size() - this->MySize - 1 < nCount)
			ASSERT("Mylist<T> is too long");
		this->MySize += nCount;
	}
private:
	Alloc alloc;
	node_printer MyHead;
	size_type MySize;
};

#endif//ZB_MYLIST_



### C++ STL 中双向链表 `list` 的实现与用法 #### 1. 基本概念 C++ 标准模板库(STL)中的 `std::list` 是一种基于双向链表的容器。它允许在常数时间内于任意位置插入或删除元素,但由于其内部结构特点,在随机访问方面性能较差[^3]。 #### 2. 数据结构特性 `std::list` 使用节点来存储数据,每个节点包含三个部分:前驱指针、当前数据以及后继指针。这种设计使得可以在 O(1) 时间复杂度下完成插入和删除操作,但在查找特定元素时需要遍历整个列表,时间复杂度为 O(n)[^4]。 #### 3. 主要功能函数 以下是 `std::list` 提供的一些常用成员函数及其作用: - **构造与销毁** - 默认构造函数创建一个空列表。 - 构造指定数量相同值的元素或者从另一个序列初始化。 - **容量** - `empty()` 判断列表是否为空。 - `size()` 返回列表中元素的数量。 - **元素访问** - 不支持直接通过索引访问元素,因为这是顺序容器而非随机存取容器。 - **修改器** - 插入方法如 `push_front()`, `emplace_back()`, 和 `insert()` 等用于向头部、尾部或其他位置添加新项。 - 删除方法像 `pop_front()`, `remove_if(predicate)` 能够移除符合条件的项目。 - **迭代器支持** - 支持双向迭代器意味着可以向前也可以向后移动游标,并且能够应用一系列标准算法比如排序(`sort`)、反转(`reverse`)等[^1]。 #### 4. 示例代码展示如何使用 `std::list` 下面给出一段简单的例子演示了基本的操作流程: ```cpp #include <iostream> #include <list> int main(){ // 创建并填充 list std::list<int> myList = {7, 5, 16, 8}; // 添加元素到前面 myList.push_front(9); // 追加单个元素到最后面 myList.emplace_back(10); // 输出所有元素 for(auto it=myList.begin();it!=myList.end();++it){ std::cout << *it << ' '; } std::cout<<'\n'; // 移除第一个匹配的元素 myList.remove(5); return 0; } ``` 上述程序展示了怎样构建初始列表、前后两端增删节点以及打印全部内容的过程。 ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值