c++ 双向链表操作总结
第一、包含DoubleLinkNode 模板类和DoubleLinkList 模板类
#pragma once #include<iostream> using namespace std; template <typename T> class DoubleLinkList;//这里必须声明 template<typename T> class DoubleLinkNode { public: T getData() { return m_data; } private: DoubleLinkNode() :m_pprior(NULL), m_pnext(NULL) {} DoubleLinkNode(const T item, DoubleLinkNode<T>* prior, DoubleLinkNode<T>* pnext) :m_data(item), m_pprior(prior), m_pnext(pnext) {} ~DoubleLinkNode() { m_pprior = NULL; m_pnext = NULL; } friend class DoubleLinkList<T>; DoubleLinkNode* m_pprior; DoubleLinkNode* m_pnext; T m_data; }; template <typename T> class DoubleLinkList { public: DoubleLinkList() { head = new DoubleLinkNode<T>(); } ~DoubleLinkList() { delete head; } void cleanDoubleLink(); bool insertNode(T data); bool insertNode(T data,int n); void printAll(); bool deleteNode(T data); DoubleLinkNode<T>* findNode(int n); DoubleLinkNode<T>* findData(T data); bool deleteNodeByIndex(int i); T getData(int i); private: friend class DoubleLinkNode<T>; DoubleLinkNode<T> *head; }; template <typename T> void DoubleLinkList<T>::cleanDoubleLink() { DoubleLinkNode<T> *pmove = head->m_pnext,*pdel; while (pmove != NULL) { pdel = pmove; pmove = pmove->m_pnext; delete pdel; } head->m_pnext = NULL; } template<typename T> bool DoubleLinkList<T>::insertNode(T data) { DoubleLinkNode<T> *pmove = head; while (pmove->m_pnext!=NULL) { pmove = pmove->m_pnext; } DoubleLinkNode<T> *newNode = new DoubleLinkNode<T>(data,pmove,NULL); pmove->m_pnext = newNode; return true; } template<typename T> bool DoubleLinkList<T>::insertNode(T item,int n) { DoubleLinkNode<T> *pmove = head; for (int i = 1; i < n; i++) { pmove = pmove->m_pnext; if (pmove==NULL && i<n-1) { cout << "超出链表长度" << endl; return false; } } DoubleLinkNode<T> *newNode = new DoubleLinkNode<T>(); newNode->m_data = item; newNode->m_pnext = pmove->m_pnext; newNode->m_pprior = pmove->m_pprior; if (pmove->m_pnext!=NULL) { pmove->m_pnext->m_pprior = newNode; } pmove->m_pnext = newNode; return true; } template<typename T> void DoubleLinkList<T>::printAll() { DoubleLinkNode<T>* pmove = head->m_pnext; while (pmove!=NULL) { cout << pmove->m_data << " "; pmove = pmove->m_pnext; } cout << endl; } template<typename T> bool DoubleLinkList<T>::deleteNode(T item) { DoubleLinkNode<T>* pmove = head; while (pmove->m_pnext!=NULL) { if (pmove->m_data==item) { break; } pmove = pmove->m_pnext; } if (pmove==NULL) { cout << "没有找到该item" << endl; return false; } pmove->m_pprior->m_pnext = pmove->m_pnext; if (pmove->m_pnext!=NULL) { pmove->m_pnext->m_pprior = pmove->m_pprior; } delete pmove; return true; } template<typename T> DoubleLinkNode<T>* DoubleLinkList<T>::findNode(int n) { if (n<1) { cout << "指定位置无效" << endl; exit(1); } DoubleLinkNode<T>* pmove = head; for (int i = 1; i <= n; i++) { pmove = pmove->m_pnext; if (pmove==NULL) { cout << "指定节点不存在!" << endl; exit(1); } } return pmove; } template<typename T> DoubleLinkNode<T>* DoubleLinkList<T>::findData(T item) { DoubleLinkNode<T>* pmove = head; while (pmove->m_pnext != NULL) { pmove = pmove->m_pnext; if (pmove == NULL) { cout << "没有找到所查节点" << endl; exit(1); } if (pmove->m_data == item) { break; } return pmove; } } template<typename T> bool DoubleLinkList<T>::deleteNodeByIndex(int n) { DoubleLinkNode<T>*pmove = head->m_pnext; for (int i = 1; i < n; i++) { if (pmove == NULL && i < (n - 1)) { cout << "没有找到该位置,删除失败" << endl; return false; } pmove = pmove->m_pnext; } if (pmove->m_pnext!=NULL) { pmove->m_pprior->m_pnext = pmove->m_pnext; pmove->m_pnext->m_pprior = pmove->m_pprior; } else { pmove->m_pprior->m_pnext = NULL; } delete pmove; return true; } template<typename T> T DoubleLinkList<T>::getData(int n) { DoubleLinkNode<T>* pmove = head->m_pnext; for (int i = 1; i < n; i++) { if (pmove==NULL && i<(n-1)) { cout << "没有找到" << endl; exit(1); } pmove = pmove->m_pnext; } if (pmove==NULL) { cout << "节点不存在" << endl; } return pmove->getData(); }
个人签名:天行健,君子以自强不息