程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 自己寫的c++雙向鏈表

自己寫的c++雙向鏈表

編輯:C++入門知識

自己寫的c++雙向鏈表


嘗試使用lambda和模板寫一個鏈表

 

 

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

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


 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved