数据结构学习:单向链表的模板类实现
2017-04-16 15:34
429 查看
参考:http://www.imooc.com/learn/651
数组:查找效率高
链表:增删效率高
下面是我用模板类实现的单向链表:
1.首先要定义Node.h:
2.然后定义LinkedList.h:
如有不足,欢迎指正!
数组:查找效率高
链表:增删效率高
下面是我用模板类实现的单向链表:
1.首先要定义Node.h:
#pragma once #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; template<typename T> class Node { public: T data;//结点数据域 Node<T> *next;//结点指针域,指向下一个结点 void printNode(); }; #endif // !NODE_H template<typename T> void Node<T>::printNode() { cout << data << " "; }
2.然后定义LinkedList.h:
#pragma once #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "Node.h" #include <iostream> using namespace std; template<typename T> class LinkedList { private: Node<T> *m_pList;//链表头指针 int m_iLength;//链表长度 public: LinkedList();//创建链表 ~LinkedList();//销毁链表 void clearList();//清空链表 bool ListEmpty();//判空 int ListLength();//链表中结点个数 bool getElem(int index, Node<T> *pNode);//根据索引获取链表中的结点 int locateElem(Node<T> *pNode);//返回结点出现的位置 bool priorElem(Node<T> *pCurrentNode, Node<T> *pPreNode);//获取前驱结点 bool nextElem(Node<T> *pCurrentNode, Node<T> *pNextNode);//获取后续结点 void ListTraverse();//遍历链表 bool ListInsert(int index, Node<T> *pNode);//在特定位置插入结点 bool ListDelete(int index, Node<T> *pNode);//删除特定位置的结点 bool ListInsertHead(Node<T> *pNode);//在链表头插入结点 bool ListInsertTail(Node<T> *pNode);//在链表尾插入结点 }; #endif // !LINKEDLIST_H template<typename T> LinkedList<T>::LinkedList() { m_pList = new Node<T>; m_pList->data = 0; m_pList->next = NULL; m_iLength = 0; } template<typename T> LinkedList<T>::~LinkedList() { clearList(); delete m_pList; m_pList = NULL; } template<typename T> void LinkedList<T>::clearList() { Node<T> *currentNode = m_pList->next; while (currentNode != NULL) { Node<T> *temp = currentNode->next; delete currentNode; currentNode = temp; } m_pList->next = NULL; } template<typename T> bool LinkedList<T>::ListEmpty() { if (m_iLength == 0) return true; else return false; } template<typename T> int LinkedList<T>::ListLength() { return m_iLength; } template<typename T> bool LinkedList<T>::ListInsertHead(Node<T> *pNode) { Node<T> *temp = m_pList->next; Node<T> *newNode = new Node<T>; if (newNode == NULL) { return false; } newNode->data = pNode->data; m_pList->next = newNode; newNode->next = temp; m_iLength++; return true; } template<typename T> bool LinkedList<T>::ListInsertTail(Node<T> *pNode) { Node<T> *currentNode = m_pList; while (currentNode->next != NULL) { currentNode = currentNode->next; } Node<T> *newNode = new Node<T>; if (newNode == NULL) { return false; } newNode->data = pNode->data; newNode->next = NULL; currentNode->next = newNode; m_iLength++; return true; } template<typename T> bool LinkedList<T>::ListInsert(int index, Node<T> *pNode) { if (index<0 || index>m_iLength) { return false; } Node<T> *currentNode = m_pList; for (int k = 0; k < index; k++) { currentNode = currentNode->next; } Node<T> *newNode = new Node<T>; newNode->data = pNode->data; newNode->next = currentNode->next; currentNode->next = newNode; m_iLength++; return true; } template<typename T> bool LinkedList<T>::ListDelete(int index, Node<T> *pNode) { if (index<0 || index>=m_iLength) { return false; } Node<T> *currentNode = m_pList; Node<T> *currentNodeBefore = NULL; for (int k = 0; k <= index; k++) { currentNodeBefore = currentNode; currentNode = currentNode->next; } currentNodeBefore->next = currentNode->next; pNode->data = currentNode->data; delete currentNode; currentNode = NULL; m_iLength--; return true; } template<typename T> bool LinkedList<T>::getElem(int index, Node<T> *pNode) { if (index < 0 || index >= m_iLength) { return false; } Node<T> *currentNode = m_pList; for (int k = 0; k <= index; k++) { currentNode = currentNode->next; } pNode->data = currentNode->data; return true; } template<typename T> int LinkedList<T>::locateElem(Node<T> *pNode) { Node<T> *currentNode = m_pList; int count = 0; while (currentNode->next != NULL) { currentNode = currentNode->next; if (currentNode->data == pNode->data) { return count; } count++; } return -1; } template<typename T> bool LinkedList<T>::priorElem(Node<T> *pCurrentNode, Node<T> *pPreNode) { Node<T> *currentNode = m_pList; Node<T> *tempNode = NULL; while (currentNode->next != NULL) { tempNode = currentNode; currentNode = currentNode->next; if (currentNode->data == pCurrentNode->data) { if (tempNode == m_pList) { return false; } pPreNode->data = tempNode->data; return true; } } return false; } template<typename T> bool LinkedList<T>::nextElem(Node<T> *pCurrentNode, Node<T> *pNextNode) { Node<T> *currentNode = m_pList; while (currentNode->next != NULL) { currentNode = currentNode->next; if (currentNode->data = pCurrentNode->data) { if (currentNode->next == NULL) { return false; } pNextNode->data = currentNode->next->data; return true; } } return false; } template <typename T> void LinkedList<T>::ListTraverse() { Node<T> *currentNode = m_pList; while (currentNode->next != NULL) { currentNode = currentNode->next; currentNode->printNode(); } }
如有不足,欢迎指正!
相关文章推荐
- 数据结构学习之链表(单向、单循环以及双向)(递归实现)
- 数据结构学习之单向链表的基本操作(非递归实现)
- 数据结构学习系列三-单向循环链表(c++实现且应用模板)
- 数据结构的C实现_单向链表
- c++实现单向链表反转的学习总结
- 数据结构:线性表的链式存储(单向链表)--Java实现
- 《Delphi 算法与数据结构》学习与感悟[8]: 单向链表的添加、删除与遍历
- 数据结构学习笔记之链表分析与实现(一)
- 数据结构练习--单向链表的实现
- 数据结构与算法学习之路:简单的电话簿实现——单向链表
- 数据结构学习的简单问题(三):用链表实现多项式相加
- C++,数据结构,单向链表的实现及简单运用,运用模板
- 学习《数据结构》有感之以字符串形式输入链表实现多项式相加
- 数据结构学习----线性表的链式表示之升序排序的单链表(Java实现)
- 数据结构学习系列一--链表的建立(c语言实现)
- java数据结构之LinkedQueue(用链表实现的双端单向队列)
- C语言数据结构之单向链表(已经调试可以实现相应的功能了,可是还是有几个问题现在还是不大理解,希望大家能够一起探讨)
- 《Delphi 算法与数据结构》学习与感悟[6]: 一个简单的"单向链表"
- 【数据结构练习】单向链表实现、链表逆序实现
- 数据结构——单向链表的链式存储及实现