码迷,mamicode.com
首页 > 编程语言 > 详细

自己写的c++双向链表

时间:2015-07-15 19:17:24      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:c++   lambda   template   

尝试使用lambda和模板写一个链表


#include "stdafx.h"
template<class T>
struct Node{
	T Value;
	struct Node * pNext;
	struct Node * pPrev;
};
template<class T>
class List{
private:
	Node<T> * m_pHead;
	int m_len;
	Node<T>* List<T>::NewNode(Node<T>* prev, T value);
	Node<T>* List<T>::DeleteNode(Node<T>* node, std::function<void(T)> pAction);
	void List<T>::MoveNext(Node<T>* last, std::function<Node<T>*(Node<T>*)> pAction);
	Node<T>* List<T>::MoveTo(T value);
	Node<T>* List<T>::MoveToAt(int index);
public:
	List();
	~List();
	int GetLength();
	bool isEmpty();
	bool Insert(T value);
	bool Remove(T value, std::function<void(T)> pAction);
	bool RemoveAt(int index, std::function<void(T)> pAction);
	T ElementAt(int index);
	void Dispose(std::function<void(T)> pAction);
	void Dispose();
	void ActionAsc(std::function<void(T)> pAction);
	void ActionDesc(std::function<void(T)> pAction);
};

#include "stdafx.h"
#include "List.h"
template<class T>
List<T>::List()
{
	this->m_len = 0;
	this->m_pHead = NULL;
}
template<class T>
List<T>::~List()
{
	if (this->m_len > 0)
	{
		this->Dispose();
	}
}
template<class T>
Node<T>* List<T>::NewNode(Node<T>* prev, T value)
{
	Node<T>* temp = new Node<T>();
	temp->pNext = NULL;
	temp->pPrev = prev;
	temp->Value = value;
	return temp;
};
template<class T>
Node<T>* List<T>::DeleteNode(Node<T>* node, std::function<void(T)> pAction)
{
	bool headFlag = node == m_pHead;
	Node<T>* result = NULL;
	T tempValue = node->Value;
	node->Value = NULL;
	if (pAction != NULL)
		pAction(tempValue);
	if (this->m_len != 1)
	{
		Node<T>* prev = node->pPrev;
		Node<T>* next = node->pNext;
		prev->pNext = next;
		next->pPrev = prev;
		result = node->pNext;
	}
	delete node;
	this->m_len--;
	if (headFlag)
	{
		m_pHead = result;
	}
	return result;
}
template<class T>
Node<T>* List<T>::MoveTo(T value)
{
	if (m_pHead == NULL)
		return NULL;
	Node<T>*p = m_pHead;
	do
	{
		if (p->Value == value)
		{
			return p;
		}
		else{
			p = p->pNext;
		}
	} while (p == NULL ? false : p != this->m_pHead);
	return NULL;
}
template<class T>
Node<T>* List<T>::MoveToAt(int index)
{
	if (m_pHead == NULL &&this->m_len <= index)
		return NULL;
	Node<T>*p = m_pHead;
	int tempIndex = -1;
	do
	{
		tempIndex++;
		if (index == tempIndex)
		{
			return p;
		}
		else{
			p = p->pNext;
		}
	} while (p == NULL ? false : p != this->m_pHead);
	return NULL;
}
template<class T>
void List<T>::MoveNext(Node<T>* last,std::function<Node<T>*(Node<T>*)> pAction)
{
	if (last == NULL || pAction == NULL)
		return;
	Node<T>*p = last;
	do
	{
		p = pAction(p);
	} while (p == NULL ? false : p != last);
}
template<class T>
int List<T>::GetLength()
{
	return this->m_len;
};
template<class T>
bool List<T>::isEmpty()
{
	return this->GetLength() == 0;
};
template<class T>
bool List<T>::Insert(T value)
{
	Node<T> * temp = this->NewNode(NULL, value);
	if (this->m_pHead == NULL)//head is empty 
	{
		temp->pPrev = temp;
		this->m_pHead = temp;
	}
	else
	{
		Node<T> * p = this->m_pHead;
		p = p->pPrev;
		p->pNext = temp;
		temp->pPrev = p;
		this->m_pHead->pPrev = temp;
	}
	temp->pNext = m_pHead;
	this->m_len++;
	return true;
};
template<class T>
bool List<T>::Remove(T value, std::function<void(T)> pAction)
{
	Node<T>*p = this->MoveTo(value);
	if (p != NULL)
	{
		this->DeleteNode(p, pAction);
	}
	return p != NULL; 
};
template<class T>
bool List<T>::RemoveAt(int index, std::function<void(T)> pAction)
{
	Node<T>*p = this->MoveToAt(index);
	if (p != NULL)
	{
		this->DeleteNode(p, pAction);
	}
	return p != NULL;
};
template<class T>
T List<T>::ElementAt(int index)
{
	Node<T>*p = this->MoveToAt(value);
	return p != NULL?p->Value:NULL;
};
template<class T>
void List<T>::Dispose(std::function<void(T)> pAction)
{
	this->MoveNext(this->m_pHead, [=](Node<T>* p){return this->DeleteNode(p, pAction); });
}; 
template<class T>
void List<T>::Dispose()
{
	this->Dispose([](T t){});
}
template<class T>
void List<T>::ActionAsc(std::function<void(T)> pAction)
{
	this->MoveNext(this->m_pHead,[=](Node<T>* p){pAction(p->Value); return p->pNext; });
};
template<class T>
void List<T>::ActionDesc(std::function<void(T)> pAction)
{
	this->MoveNext(this->m_pHead->pPrev, [=](Node<T>* p){pAction(p->Value); return p->pPrev; });
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

自己写的c++双向链表

标签:c++   lambda   template   

原文地址:http://blog.csdn.net/qq6648208281/article/details/46896947

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!