简单双向链表(C++模版技术实现)
下面代码仅供本人复习数据结构所用,实用性N低,各位飘过吧~~哈哈:>
// // C++ 模版技术实现简单双向链表示例. // #include <cstdlib> #include <iostream> #include <iomanip> #include <stdexcept> // 双向链表类模版前置声明 template <typename T> class DoublyLinkedList; // // 双向链表节点类模版. // template <typename T> class Node { friend class DoublyLinkedList<T>; private: T _value; Node<T> *_pPrior, *_pNext; public: Node(void) : _pPrior(NULL) , _pNext(NULL) { NULL; } explicit Node(const T &val) : _value(val) , _pPrior(NULL) , _pNext(NULL) { NULL; } T& getValue(void) { return _value; } Node<T>* getPrior(void) { return _pPrior; } Node<T>* getNext(void) { return _pNext; } }; // // 双向链表类模版 // template <typename T> class DoublyLinkedList { private: Node<T> *_pHead; public: DoublyLinkedList(void); ~DoublyLinkedList(void); void clear(void); size_t length(void) const; T& visit(const size_t pos); Node<T>* add(const T &val); Node<T>* insert(const size_t pos, const T &val); Node<T>* search(const T &val) const; Node<T>* remove(const size_t pos); }; template <typename T> DoublyLinkedList<T>::DoublyLinkedList(void) { _pHead = new Node<T>(); } template <typename T> DoublyLinkedList<T>::~DoublyLinkedList(void) { clear(); delete _pHead; } // // 清空链表中插入的所有节点,头节点除外. // template <typename T> void DoublyLinkedList<T>::clear(void) { for (Node<T> *pDel = _pHead->_pNext; NULL != pDel; pDel = _pHead->_pNext) { _pHead->_pNext = pDel->_pNext; #if !defined(NDEBUG) std::cout << "删除值:" << pDel->_value << std::endl; #endif delete pDel; } } // // 求节点总数,排除头结点. // template <typename T> size_t DoublyLinkedList<T>::length(void) const { size_t len = 0; for (Node<T> *pTemp = _pHead->_pNext; NULL != pTemp; ++len) { pTemp = pTemp->_pNext; } return len; } // // 在指定位置 pos 插入节点,若 pos 过大,则在表尾插入.返回插入节点指针. // template <typename T> T& DoublyLinkedList<T>::visit(const size_t pos) { Node<T> *pVisit = _pHead->_pNext; for (size_t i = 0; NULL != pVisit && i < pos; ++i, pVisit = pVisit->_pNext) { NULL; } if (NULL == pVisit) { throw std::overflow_error("访问链表节点越界 !"); } return pVisit->_value; } // // 在链表头节点后插入新节点. // template <typename T> Node<T>* DoublyLinkedList<T>::add(const T &val) { Node<T> *pNew = new Node<T>(val); pNew->_pNext = _pHead->_pNext; _pHead->_pNext = pNew; return pNew; } // // 在指定位置 pos 插入节点,若 pos 过大,则在表尾插入.返回插入节点指针. // template <typename T> Node<T>* DoublyLinkedList<T>::insert(const size_t pos, const T &val) { Node<T> *pPrev = _pHead; for (size_t i = 0; NULL != pPrev->_pNext && i < pos; ++i) { pPrev = pPrev->_pNext; } Node<T> *pNew = new Node<T>(val); pNew->_pNext = pPrev->_pNext; pPrev->_pNext = pNew; #if !defined(NDEBUG) std::cout << "插入节点:" << pNew->_value << std::endl; #endif return pNew; } // // 根据指定的数据值查找节点,找到返回节点指针,否则返回 NULL. // template <typename T> Node<T>* DoublyLinkedList<T>::search(const T &val) const { Node<T> *pTemp = _pHead->_pNext; for (NULL; NULL != pTemp && val != pTemp->_value; pTemp = pTemp->_pNext) { NULL; } return pTemp; } // // 删除指定位置节点,并返回前一节点指针, // 若 pos 过大则不删除,返回最后一个节点指针. // template <typename T> Node<T>* DoublyLinkedList<T>::remove(const size_t pos) { Node<T> *pPrev = _pHead; for (size_t i = 0; NULL != pPrev->_pNext && i < pos; ++i) { pPrev = pPrev->_pNext; } if (NULL != pPrev->_pNext) { Node<T> *pDel = pPrev->_pNext; pPrev->_pNext = pDel->_pNext; #if !defined(NDEBUG) std::cout << "删除节点:" << pDel->_value << std::endl; #endif delete pDel; } return pPrev; } // // 在 main 函数结束后调用,防止控制台一闪而过. // void calledAfterMain(void) { system("pause"); } // // 测试链表 // int main(void) { const size_t MAX_LIST_SIZE = 10; DoublyLinkedList<int> linkedList; for (size_t i = 0; i < MAX_LIST_SIZE; ++i) { // linkedList.insert(i, i); linkedList.add(i); } std::cout << "链表长度: " << linkedList.length() << std::endl; try { for (size_t i = 0; i < MAX_LIST_SIZE + 1; ++i) { std::cout << std::setw(3) << linkedList.visit(i); } std::cout << std::endl; } catch (const std::exception &e) { std::cout << std::endl << e.what() << std::endl; } atexit(calledAfterMain); return EXIT_SUCCESS; }