尝试使用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; }); };
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-14 14:59:37