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;}
阅读全文
0 0
- 线性链表(C语言实现)
- C语言实现线性顺序链表
- 线性链表的c语言实现
- C语言实现线性表
- 线性表 C语言实现
- c语言实现线性表
- 数据结构--线性表C实现
- 线性链表基本操作实现(C/C++)
- 线性链表C
- 线性链表--顺序链表实现(c语言)
- 线性链表--链式链表实现(c语言)
- (续)线性表之双向链表(C语言实现)
- C 类LinkList实现线性链表的一般操作
- 线性链表的表示以及实现(C语言编写)
- 数据结构C语言实现——线性链表
- 【C++】双向线性链表容器的实现
- C语言实现线性链表的19个功能
- 简易实现c 线性表(单链)
- 深度学习
- Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > com.android.bui
- POI导出EXCEL经典实现
- 解决ora-01555
- 解决source insight的context窗口丢失的方法
- C++实现线性链表
- android自定义View探索5(onMeasure深入分析二LayoutParams)
- jupyter软件的安装
- Android异常-AndroidRuntimeException: You cannot combine custom titles with other title features
- 写写Android的微信支付!
- TCP相关信息
- 静态网页与动态网页
- 判断CString里面的字符串是否全为数字构成
- React Native 连接sqlite数据库