一、说明
二、代码
main.h
#include <iostream>
#include "CircleList.h"
using namespace std;
int main()
{
cout << "循环链表" << endl;
CircleList<int> intCircleList;
intCircleList.Insert(5);
intCircleList.Insert(15);
intCircleList.Insert(25);
intCircleList.Insert(35);
CircleListIterator<int> li(intCircleList);
if(li.NotNull())
{
cout<<*li.First();
while(li.NextNotNull())
{
cout<<"->"<<*li.Next();
}
cout<<endl;
}
cout<<endl<<"测试循环"<<endl;
CircleListIterator<int> iter(intCircleList);
cout<<*iter.First()<<endl;
cout<<*iter.Next()<<endl;
cout<<*iter.Next()<<endl;
cout<<*iter.Next()<<endl;
cout<<*iter.Next()<<endl;
cout<<*iter.Next()<<endl;
cout<<*iter.Next()<<endl;
cout<<*iter.Next()<<endl;
cout<<*iter.Next()<<endl;
cout<<*iter.Next()<<endl;
cout<<*iter.Next()<<endl;
return 0;
}
CircleList.h
#ifndef _CIRCLELIST_H
#define _CIRCLELIST_H
#include <iostream>
using namespace std;
//循环链表
template <class Type> class CircleList;
template <class Type> class CircleListIterator;
/*节点类CircleListNode*/
template <class Type>
class CircleListNode
{
friend class CircleList<Type>;
friend class CircleListIterator<Type>;
private:
Type data;
CircleListNode *link;
CircleListNode(Type);//私有构造函数,只有由友元类List调用
CircleListNode(){};//默认的构造函数
} ;
/*循环链表类CircleList*/
template <class Type>
class CircleList
{
friend class CircleListIterator<Type>;
public:
CircleList()//构造函数。建立一个仅含有头指针的空链表。表头结构
{
first = new CircleListNode<Type>;//创建一个新的节点
first->link = first;
};
void Insert(Type);//链表插入节点函数
void Delete(Type);//链表删除节点函数
private:
CircleListNode<Type> *first;//头指针
};
/**循环链表迭代器也是一个类*/
template <class Type>
class CircleListIterator
{
public:
CircleListIterator(const CircleList<Type> &l):circleList(l),current(l.first->link){};//构造函数有个参数->链表。l.first->link:因为first是空的
bool NotNull();//判断链表迭代器所指向的链表里的当前节点是不是空的函数
bool NextNotNull();//判断当前节点的下一个节点是不是空的函数
Type *First();//返回链表的第一个节点的指针函数
Type *Next();//返回链表当前节点下一个节点的指针函数
private:
const CircleList<Type> &circleList;//链表。这个迭代器ListIterator就是这个循环链表circleList的迭代器
CircleListNode<Type> *current;//私有的数据成员,指针current
};
/*迭代器函数:判断链表迭代器所指向的链表里的当前节点是不是空的*/
template <class Type>
bool CircleListIterator<Type>::NotNull()
{
if(current != circleList.first)
{
return true;
}
return false;
}
/*迭代器函数:判断当前节点的下一个节点是不是空的函数*/
template <class Type>
bool CircleListIterator<Type>::NextNotNull()
{
if(current->link != circleList.first)
{
return true;
}
return false;
}
/*迭代器函数:返回链表的第一个节点的指针函数*/
template <class Type>
Type *CircleListIterator<Type>::First()
{
if(current != circleList.first)//如果当前节点不是空的
{
return ¤t->data;//返回current节点的数据
}
return 0;
}
/*迭代器函数:返回链表当前节点下一个节点的指针函数*/
template <class Type>
Type *CircleListIterator<Type>::Next()
{
current = current ->link;
if(current ==circleList.first)//当current为first空指针时需要往下移动一位
{
current = current ->link;
}
return ¤t->data;
}
/*ListNode的构造函数*/
template <class Type>
CircleListNode<Type>::CircleListNode(Type element)
{
data = element;
link = 0;
}
/*链表插入节点函数*/
template <class Type>
void CircleList<Type>::Insert(Type k){
CircleListNode<Type> *newNode = new CircleListNode<Type>(k);
newNode->link = first->link;
first->link = newNode;
}
/*链表删除节点函数*/
template <class Type>
void CircleList<Type>::Delete(Type k)
{
CircleListNode<Type> *previous = first;//previous用来保存被删除节点的前一个节点
CircleListNode<Type> *current;//用来遍历整个链表
/*first为空节点,令current节点为first节点的下一个节点;
当current节点不是first节点且其值不为k时;
将前一个节点(previous)指向current,其current指向后一个节点。
以此实现对链表的整个遍历*/
for(current = first->link;(current != first)&¤t->data != k;previous =current,current = current->link)
{
//空循环。目的:找到要被删除的节点
}
if(current != first)//不等于first表示找到了
{
previous->link = current -> link;
delete current;//current指向first,first是new出来的。所以current需要delete操作
}
}
#endif // _CIRCLELIST_H
输出结果: