阉割的List
实在忍不住,模仿STL写了个阉割版的List,还没加迭代器,等搞完STL源码再说吧。
#pragma once
#include <stdexcept>
namespace vlyf
{
template <typename Key>
struct Node
{
using pointer = Node*;
Key key;
Node* prev{ nullptr };
Node* next{ nullptr };
Node() = default;
Node(Key const& k) : key(k) {}
};
template <typename T>
class List
{
public:
typedef typename Node<T>::pointer linkType;
private:
linkType head{ new Node<T> };
public:
List()
{
head->prev = head;
head->next = head;
}
~List() = default;
linkType Begin() const { return head->next; }
linkType End() const { return head; }
linkType Search(T const&) const;
linkType Erase(linkType position);
void Insert(linkType position, T const& x);
void PushBack(T const& x) { Insert(End(), x); }
void PushFront(T const& x) { Insert(Begin(), x); }
void PopFront() { Erase(Begin()); }
void PopBack()
{
linkType tmp = End()->prev;
Erase(tmp);
}
void Clear();
void Remove(T const& x);
void Unique();
void Delete(T const& x);
protected:
void Transfer(linkType position, linkType first, linkType last);
};
template <typename T>
inline typename List<T>::linkType List<T>::Search(T const& k) const
{
if (!head)
throw std::out_of_range("List is empty");
linkType p = head;
while (p->key != k)
{
p = p->next;
}
return p;
}
template <typename T>
inline void List<T>::Insert(List<T>::linkType position, T const& x)
{
linkType node = new Node<T>(x);
node->next = position;
node->prev = position->prev;
position->prev->next = node;
position->prev = node;
}
template <typename T>
inline void List<T>::Delete(T const& x)
{
if (!x->prev)
x->prev->next = x->next;
else
head->next = x->next;
if (!x->next)
x->next->prev = x->prev;
delete x;
}
template <typename T>
inline typename List<T>::linkType List<T>::Erase(typename List<T>::linkType position)
{
linkType nextNode = position->next;
linkType prevNode = position->prev;
prevNode->next = nextNode;
nextNode->prev = prevNode;
delete position;
return nextNode;
}
template <typename T>
inline void List<T>::Clear()
{
linkType cur = head->next;
while (cur != head)
{
linkType tmp = cur;
cur = cur->next;
delete tmp;
}
head->next = head;
head->prev = head;
}
template <typename T>
inline void List<T>::Remove(T const& x)
{
linkType first = Begin();
linkType last = End();
while (first != last)
{
linkType tmp = first;
tmp = tmp->next;
if (x == first->key)
{
Erase(first);
}
first = tmp;
}
}
template <typename T>
inline void List<T>::Unique()
{
linkType first = Begin();
linkType last = End();
if (first == last)
return;
linkType next = first->next;
while (next != last)
{
if (next->key == first->key)
Erase(next);
else
first = next;
next = first->next;
}
}
template <typename T>
inline void List<T>::Transfer(linkType position,
linkType first,
linkType last)
{
last->prev->next = position;
first->prev->next = last;
position->prev->next = first;
linkType tmp = position->prev;
position->prev = last->prev;
last->prev = first->prev;
first->prev = tmp;
}
} // namespace vlyf
__EOF__

本文作者:刘一凡
本文链接:https://www.cnblogs.com/vlyf/p/12040882.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/vlyf/p/12040882.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
标签:
data structure
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人