C++实现线性链表

来源:互联网 发布:淘宝贷款分12期 编辑:程序博客网 时间:2024/06/05 23:51
template<typename T>struct LNode{T data;struct LNode<T> *next;};template<typename T>class LinkList{private:LNode<T>* m_pList;int m_ListLength;private:bool InitList();bool GetNode(int position, LNode<T>** node);public:LinkList();~LinkList();bool DestroyList();bool InsertNode(int beforeWhich, T data);bool DeleteNode(int position);bool IsEmpty();int GetLength();bool ClearList();bool SetNodeData(int position, T NewData);bool GetNodeData(int position, T& data);bool Reversal();};template<typename T>LinkList<T>::LinkList(){m_pList = NULL;m_ListLength = 0;InitList();}template<typename T>LinkList<T>::~LinkList(){if (!DestroyList()){DestroyList();}}template<typename T>bool LinkList<T>::InitList(){m_pList = new LNode<T>;if (!m_pList){return false;}m_pList->data = NULL;m_pList->next = NULL;return true;}template<typename T>bool LinkList<T>::DestroyList(){if (ClearList()){return false;}delete m_pList;return true;}//在链表中插入一个节点,插入之后在链表中的位置为beforeWhichtemplate<typename T>bool LinkList<T>::InsertNode(int beforeWhich, T data){LNode<T> *pPrevious = NULL;if (beforeWhich < 1 || beforeWhich > (m_ListLength + 1)){return false;}if (!GetNode(beforeWhich - 1, &pPrevious)){return false;}LNode<T> *newNode = new LNode<T>;newNode->data = data;newNode->next = pPrevious->next;pPrevious->next = newNode;m_ListLength++;return true;}//删除链接中的指定节点template<typename T>bool LinkList<T>::DeleteNode(int position){if (position < 1 || position > m_ListLength){return false;}LNode<T> *pPrevious = NULL;if (!(GetNode(position - 1, &pPrevious))){return false;}LNode<T> *pCurrent = pPrevious->next;pPrevious->next = pCurrent->next;delete pCurrent;m_ListLength--;return true;}//获取链表中指定的节点template<typename T>bool LinkList<T>::GetNode(int position, LNode<T>** node){if (position < 0 || position > m_ListLength){return false;}LNode<T> *pNext = m_pList;for (int i = 1; i <= position; i++){pNext = pNext->next;}*node = pNext;return true;}//判断链表是否为空template<typename T>bool LinkList<T>::IsEmpty(){if (m_pList->next == NULL){return true;}return false;}//获取链表中节点的个数template<typename T>int LinkList<T>::GetLength(){return m_ListLength;}//清空链表template<typename T>bool LinkList<T>::ClearList(){if (m_pList == NULL){return false;}LNode<T> *pNode = m_pList->next;LNode<T> *pTemp = NULL;while (pNode){pTemp = pNode;pNode = pNode->next;delete pTemp;}m_ListLength = 0;return true;}//设置链表中指定节点的数据template<typename T>bool LinkList<T>::SetNodeData(int position, T newData){LNode<T> *pNode = NULL;if (!GetNode(position, &pNode)){return false;}pNode->data = newData;return true;}//获取链表中指定节点的数据template<typename T>bool LinkList<T>::GetNodeData(int position, T& data){LNode<T> *pNode = NULL;if (!GetNode(position, &pNode)){return false;}data = pNode->data;return true;}//链表反转template<typename T>bool LinkList<T>::Reversal(){LNode<T> *pLast = m_pList;LNode<T> *pCurrent = m_pList->next;LNode<T> *pNext = pCurrent->next;while (pCurrent){pNext = pCurrent->next;pCurrent->next = pLast;pLast = pCurrent;pCurrent = pNext;}m_pList->next = pLast;return true;}

原创粉丝点击