参考:http://www.imooc.com/learn/651
数组:查找效率高
链表:增删效率高
下面是我用模板类实现的单向链表:
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();
}
}
如有不足,欢迎指正!