栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

使用C/C++ 仿真实现 .NET LinkedList

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用C/C++ 仿真实现 .NET LinkedList

linkedlist.h

CXX99(native*)

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include 

template
class LinkedList;

template
struct LinkedListNode
{
public:
	LinkedListNode* Previous;
	LinkedListNode* Next;
	T Value;
	LinkedList* LinkedList;
};

template
class LinkedList
{
private:
	LinkedListNode* m_first;
	LinkedListNode* m_last;
	int m_count;

public:
	LinkedList()
	{
		this->m_count = 0;
		this->m_first = NULL;
		this->m_last = NULL;
	}
	LinkedListNode* First();
	LinkedListNode* Last();
	int Count();
	bool AddFirst(LinkedListNode* value);
	bool AddLast(LinkedListNode* value);
	bool AddAfter(LinkedListNode* node, LinkedListNode* value);
	bool AddBefore(LinkedListNode* node, LinkedListNode* value);
	bool RemoveFirst();
	bool RemoveLast();
	bool Remove(LinkedListNode* node);
	LinkedListNode* Find(T value);
	void Clear();
};
#endif

template
inline LinkedListNode* LinkedList::First()
{
	return this->m_first;
}

template
inline LinkedListNode* LinkedList::Last()
{
	return this->m_last;
}

template
inline int LinkedList::Count()
{
	return this->m_count;
}

template
inline bool LinkedList::AddFirst(LinkedListNode* value)
{
	if (value == NULL)
	{
		return false;
	}
	value->LinkedList = NULL;
	value->Next = NULL;
	value->Previous = NULL;

	if (this->m_last == NULL)
	{
		this->m_last = value;
		this->m_first = value;
		this->m_count = 0;
	}
	else
	{
		LinkedListNode* current = this->m_first;
		value->Next = current;
		current->Previous = value;
		this->m_first = value;
	}
	this->m_count++;
	value->LinkedList = this;
	return true;
}

template
inline bool LinkedList::AddLast(LinkedListNode* node)
{
	if (node == NULL)
	{
		return false;
	}
	node->LinkedList = NULL;
	node->Next = NULL;
	node->Previous = NULL;

	if (this->m_last == NULL)
	{
		this->m_first = node;
		this->m_last = node;
		this->m_count = 0;

		this->m_count++;
		node->LinkedList = this;
		return true;
	}
	else
	{
		return this->AddAfter(this->m_last, node);
	}
}

template
inline bool LinkedList::AddAfter(LinkedListNode* node, LinkedListNode* value)
{
	if (node == NULL || value == NULL)
	{
		return false;
	}
	value->LinkedList = NULL;
	value->Next = NULL;
	value->Previous = NULL;
	LinkedListNode* current = node->Next;
	node->Next = value;
	if (current != NULL)
	{
		current->Previous = value;
	}
	value->Previous = node;
	value->Next = current;
	if (node == this->m_last)
	{
		this->m_last = value;
	}
	this->m_count++;
	value->LinkedList = this;
	return true;
}

template
inline bool LinkedList::AddBefore(LinkedListNode* node, LinkedListNode* value)
{
	if (node == NULL || value == NULL)
	{
		return false;
	}
	value->LinkedList = NULL;
	value->Next = NULL;
	value->Previous = NULL;
	LinkedListNode current = node->Previous;
	if (current == NULL)
	{
		return this->AddFirst(value);
	}
	current.Next = value;
	node->Previous = value;
	value->Next = node;
	value->Previous = current;
	if (node == this->m_first)
	{
		this->m_first = value;
	}
	this->m_count++;
	value->LinkedList = this;
	return true;
}

template
inline bool LinkedList::RemoveFirst()
{
	LinkedListNode* first = this->m_first;
	if (first == NULL)
	{
		return false;
	}
	LinkedListNode* current = first->Next;
	first->Previous = NULL;
	first->LinkedList = NULL;
	first->Next = NULL;
	if (current != NULL)
	{
		current->Previous = NULL;
	}
	this->m_count--;
	if (this->m_count <= 0)
	{
		this->m_count = 0;
		this->m_first = NULL;
		this->m_last = NULL;
		current = NULL;
	}
	this->m_first = current;
	return true;
}

template
inline bool LinkedList::RemoveLast()
{
	LinkedListNode* last = this->m_last;
	if (last == NULL)
	{
		return false;
	}
	LinkedListNode* current = last->Previous;
	last->Previous = NULL;
	last->LinkedList = NULL;
	last->Next = NULL;
	if (current != NULL)
	{
		current->Next = NULL;
	}
	this->m_count--;
	if (this->m_count <= 0)
	{
		this->m_count = 0;
		this->m_first = NULL;
		this->m_last = NULL;
		current = NULL;
	}
	this->m_last = current;
	return true;
}

template
inline bool LinkedList::Remove(LinkedListNode* node)
{
	if (node == NULL)
	{
		return false;
	}
	if (node == this->m_first)
	{
		return this->RemoveFirst();
	}
	if (node == this->m_last)
	{
		return this->RemoveLast();
	}
	LinkedListNode* previous = node->Previous;
	LinkedListNode* next = node->Next;
	previous->Next = next;
	next->Previous = previous;
	this->m_count--;
	if (this->m_count <= 0)
	{
		this->m_count = 0;
		this->m_first = NULL;
		this->m_last = NULL;
	}
	node->Next = NULL;
	node->Previous = NULL;
	node->LinkedList = NULL;
	return true;
}

template
inline LinkedListNode* LinkedList::Find(T value)
{
	LinkedListNode* i = this->m_first;
	while (i != NULL)
	{
		if (i->Value == value)
		{
			return i;
		}
		i = i->Next;
	}
	return NULL;
}

template
inline void LinkedList::Clear()
{
	LinkedListNode* i = this->m_first;
	while (i != NULL)
	{
		LinkedListNode* j = i->Next;
		{
			i->LinkedList = NULL;
			i->Next = NULL;
			i->Previous = NULL;
		}
		i = j;
	}
	this->m_first = NULL;
	this->m_count = 0;
	this->m_last = NULL;
}

CXX11(std::shared_ptr<....>)

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include 

template
class LinkedList;

template
struct LinkedListNode
{
public:
	std::shared_ptr > Previous;
	std::shared_ptr > Next;
	T Value;
	LinkedList* LinkedList_;
};

template
class LinkedList
{
private:
	std::shared_ptr > m_first;
	std::shared_ptr > m_last;
	int m_count;

public:
	LinkedList()
	{
		this->m_count = 0;
		this->m_first = NULL;
		this->m_last = NULL;
	}
	std::shared_ptr > First();
	std::shared_ptr > Last();
	int Count();
	bool AddFirst(std::shared_ptr > value);
	bool AddLast(std::shared_ptr > value);
	bool AddAfter(std::shared_ptr > node, std::shared_ptr > value);
	bool AddBefore(std::shared_ptr > node, std::shared_ptr > value);
	bool RemoveFirst();
	bool RemoveLast();
	bool Remove(std::shared_ptr > node);
	std::shared_ptr > Find(T value);
	void Clear();
};
#endif

template
inline std::shared_ptr > LinkedList::First()
{
	return this->m_first;
}

template
inline std::shared_ptr > LinkedList::Last()
{
	return this->m_last;
}

template
inline int LinkedList::Count()
{
	return this->m_count;
}

template
inline bool LinkedList::AddFirst(std::shared_ptr > value)
{
	if (value == NULL)
	{
		return false;
	}
	value->LinkedList_ = NULL;
	value->Next = NULL;
	value->Previous = NULL;

	if (this->m_last == NULL)
	{
		this->m_last = value;
		this->m_first = value;
		this->m_count = 0;
	}
	else
	{
		std::shared_ptr > current = this->m_first;
		value->Next = current;
		current->Previous = value;
		this->m_first = value;
	}
	this->m_count++;
	value->LinkedList_ = this;
	return true;
}

template
inline bool LinkedList::AddLast(std::shared_ptr > node)
{
	if (node == NULL)
	{
		return false;
	}
	node->LinkedList_ = NULL;
	node->Next = NULL;
	node->Previous = NULL;

	if (this->m_last == NULL)
	{
		this->m_first = node;
		this->m_last = node;
		this->m_count = 0;

		this->m_count++;
		node->LinkedList_ = this;
		return true;
	}
	else
	{
		return this->AddAfter(this->m_last, node);
	}
}

template
inline bool LinkedList::AddAfter(std::shared_ptr > node, std::shared_ptr > value)
{
	if (node == NULL || value == NULL)
	{
		return false;
	}
	value->LinkedList_ = NULL;
	value->Next = NULL;
	value->Previous = NULL;
	std::shared_ptr > current = node->Next;
	node->Next = value;
	if (current != NULL)
	{
		current->Previous = value;
	}
	value->Previous = node;
	value->Next = current;
	if (node == this->m_last)
	{
		this->m_last = value;
	}
	this->m_count++;
	value->LinkedList_ = this;
	return true;
}

template
inline bool LinkedList::AddBefore(std::shared_ptr > node, std::shared_ptr > value)
{
	if (node == NULL || value == NULL)
	{
		return false;
	}
	value->LinkedList_ = NULL;
	value->Next = NULL;
	value->Previous = NULL;
	LinkedListNode current = node->Previous;
	if (current == NULL)
	{
		return this->AddFirst(value);
	}
	current.Next = value;
	node->Previous = value;
	value->Next = node;
	value->Previous = current;
	if (node == this->m_first)
	{
		this->m_first = value;
	}
	this->m_count++;
	value->LinkedList_ = this;
	return true;
}

template
inline bool LinkedList::RemoveFirst()
{
	std::shared_ptr > first = this->m_first;
	if (first == NULL)
	{
		return false;
	}
	std::shared_ptr > current = first->Next;
	first->Previous = NULL;
	first->LinkedList_ = NULL;
	first->Next = NULL;
	if (current != NULL)
	{
		current->Previous = NULL;
	}
	this->m_count--;
	if (this->m_count <= 0)
	{
		this->m_count = 0;
		this->m_first = NULL;
		this->m_last = NULL;
		current = NULL;
	}
	this->m_first = current;
	return true;
}

template
inline bool LinkedList::RemoveLast()
{
	std::shared_ptr > last = this->m_last;
	if (last == NULL)
	{
		return false;
	}
	std::shared_ptr > current = last->Previous;
	last->Previous = NULL;
	last->LinkedList_ = NULL;
	last->Next = NULL;
	if (current != NULL)
	{
		current->Next = NULL;
	}
	this->m_count--;
	if (this->m_count <= 0)
	{
		this->m_count = 0;
		this->m_first = NULL;
		this->m_last = NULL;
		current = NULL;
	}
	this->m_last = current;
	return true;
}

template
inline bool LinkedList::Remove(std::shared_ptr > node)
{
	if (node == NULL)
	{
		return false;
	}
	if (node == this->m_first)
	{
		return this->RemoveFirst();
	}
	if (node == this->m_last)
	{
		return this->RemoveLast();
	}
	std::shared_ptr > previous = node->Previous;
	std::shared_ptr > next = node->Next;
	previous->Next = next;
	next->Previous = previous;
	this->m_count--;
	if (this->m_count <= 0)
	{
		this->m_count = 0;
		this->m_first = NULL;
		this->m_last = NULL;
	}
	node->Next = NULL;
	node->Previous = NULL;
	node->LinkedList_ = NULL;
	return true;
}

template
inline std::shared_ptr > LinkedList::Find(T value)
{
	std::shared_ptr > i = this->m_first;
	while (i != NULL)
	{
		if (i->Value == value)
		{
			return i;
		}
		i = i->Next;
	}
	return NULL;
}

template
inline void LinkedList::Clear()
{
	std::shared_ptr > i = this->m_first;
	while (i != NULL)
	{
		std::shared_ptr > j = i->Next;
		{
			i->LinkedList_ = NULL;
			i->Next = NULL;
			i->Previous = NULL;
		}
		i = j;
	}
	this->m_first = NULL;
	this->m_count = 0;
	this->m_last = NULL;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/911311.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号