温馨提示:
若把类的声明放在.h文件中,将具体的方法实现放在.cpp文件中时,在使用的类中需要将.h 和.cpp都同时导入,不然无法正常运行。并可根据自己的需要对结构体chainNode进行运算符重载(==,!=,<<等)。
(1)LinkList.h文件
//
// Created by 微光 on 2019/5/16.
//
#ifndef LINKLIST_LINKLIST_H
#define LINKLIST_LINKLIST_H
#include <iostream>
//#include <cstddef>
template<class T>
struct chainNode
{
//数据成员
T element;
chainNode<T> *next;
//构造方法
chainNode(){}
chainNode(const T &element)
{
this->element=element;
}
chainNode(const T &element,chainNode<T> *next)
{
this->element=element;
this->next=next;
}
bool operator==(chainNode theNode){
return (element==theNode.element);
}
bool operator!=(chainNode theNode)
{
return (element!=theNode.element);
}
};
template<class T>
class LinkList//线性表Chain类
{
public:
class iterator//用来做迭代器的类
{
public:
friend class Chain;
iterator(chainNode<T>* theNode=NULL)//构造函数
{node=theNode;}
//操作符重载
T& operator*()const
{
return node->element;
}
T* operator->()const
{
return &node->element;
}
//加法操作
iterator& operator++()//前加加,++i
{
node=node->next;
return *this;
}
iterator operator++(int)//后加加,i++
{
iterator old=*this;
node=node->next;
return old;
}
bool operator!=(const iterator right)const//检验是否不相等
{
return node!=right.node;
}
bool operator==(const iterator right)const//检验是否相等
{
return node==right.node;
}
protected:
chainNode<T>* node;
};
public:
LinkList();//构造函数
void insertHead(T&);//表头插入
void push_back(T&);//表尾插入
void deleteNode(T&);//删除指定元素
void clear();//清空链表
int find(const T&)const;//根据关键字查找链表元素位置
void insert(int theIndex,const T& theElement);//在索引theIndex插入元素theElement
void output();//输出链表内容
iterator begin(){return iterator(firstNode);}
iterator end(){return iterator(NULL);}
private:
chainNode<T>* firstNode;
int listSize;//链表节点数
};
#endif //LINKLIST_LINKLIST_H
(2)LinkList.cpp文件
//
// Created by 微光 on 2019/5/16.
//
#include "LinkList.h"
template<class T>
LinkList<T>::LinkList()//构造函数
{
firstNode=NULL;
listSize=0;
}
template<class T>
void LinkList<T>::insert(int theIndex,const T& theElement)//在索引theIndex插入元素element
{
if(theIndex==0)//表头插入
firstNode=new chainNode<T>(theElement,firstNode);
else
{
//寻找新元素的前驱
chainNode<T>* currentNode=firstNode;
for(int i=0;i<theIndex-1;i++)
currentNode=currentNode->next;
//在currentNode之后插入
currentNode->next=new chainNode<T>(theElement,currentNode->next);
}
listSize++;
}
template<class T>
void LinkList<T>::insertHead(T& theElement)//表头插入
{
firstNode=new chainNode<T>(theElement,firstNode);//表头插入元素theElement的节点
listSize++;
}
template<class T>
void LinkList<T>::push_back(T& theElement)//表尾插入
{//在链表尾端插入元素theElement的节点
if(firstNode==NULL)//链表为空
{
firstNode=new chainNode<T>(theElement,firstNode);
}
else
{
chainNode<T>* currentNode=firstNode;
while(currentNode->next!=NULL)
{
currentNode=currentNode->next;
}
currentNode->next=new chainNode<T>(theElement,NULL);//在表尾插入
}
listSize++;
}
template<class T>
void LinkList<T>::deleteNode(T& theElement)//删除所有element==theElement的元素
{
chainNode<T>* deleteNode;//用来指向需要删除的元素
while(firstNode->element==theElement&&firstNode!=NULL)//如果需要删除的元素为头结点
{
deleteNode=firstNode;//保存需要删除的节点
firstNode=firstNode->next;
listSize--;
delete deleteNode;//删除deleteNode节点指向的节点
}
chainNode<T>* currentNode=firstNode;//用来遍历链表
while(currentNode!=NULL&¤tNode->next!=NULL)//遍历链表找到需要删除的元素
{
if(currentNode->next->element==theElement)//找到需要删除的元素
{
deleteNode=currentNode->next;//指向需要删除的节点
if(currentNode->next->next!=NULL)//如果currentNode->next->next不为空
{
currentNode->next=currentNode->next->next;
delete deleteNode;//删除deleteNode节点指向的节点
}
else//如果currentNode->next->next为空
currentNode->next=NULL;
listSize--;
}
else//需要判断现在的节点currentNode->next是否需要删除
currentNode=currentNode->next;
}
if(currentNode->element==theElement)//判断尾节点是否需要删除
currentNode=NULL;
}
template<class T>
void LinkList<T>::clear()//清空链表
{
chainNode<T>* deleteNode;//用来指向需要删除的节点
while(firstNode!=NULL)//清空当前链表
{
deleteNode=firstNode;
firstNode=firstNode->next;
delete deleteNode;
}
listSize=0;//链表节点个数为0
}
template<class T>
int LinkList<T>::find(const T& theElement)const//根据关键字查找链表元素位置
{
chainNode<T>* currentNode=firstNode;//用来遍历链表
int theIndex=1;//用来返回元素theElement的位置
while(currentNode!=NULL&¤tNode->element!=theElement)//遍历链表,查找元素
{
currentNode=currentNode->next;
theIndex++;
}
if(currentNode==NULL)
return 0;//元素theElement不存在,返回0
else
return theIndex;//元素theElement存在,返回位置theIndex
}
//输出链表内容
template<class T>
void LinkList<T>::output() {
chainNode<T>* currentNode=firstNode;
while (currentNode!=NULL){
std::cout<<currentNode->element<<"\t";
currentNode=currentNode->next;
}
std::cout<<std::endl;
}