单向循环链表
带有表头结构的循环链表
链表的最后一个节点指向第一个节点
第一个节点是表头结构,不保存数据的。做链表有两种做法,一种是带有表头结构,一种是不带表头结构的,那么循环链表就是使用带表头结构的,表头就是一个节点,但是节点里面没有数据。空循环链表自己指向自己,空循环链表里面加入数据时就是循环链表。
今天依然使用前面的做好的代码基础上写:
在VS2013中新建项目,在头文件中加入MyList.h在原文件中加入main.cpp
//MyList.h
#ifndef MYLIST_H
#define MYLIST_H
#include<iostream>
//循环链表
template<class Type> class List;
template<class Type> class ListIterator;
template<class Type>
class ListNode{
friend class List<Type>;
friend class ListIterator<Type>;
private:
Type data;
ListNode *link;
ListNode(Type);//构造函数
ListNode(){};
};
template<class Type>
class List{
friend class ListIterator<Type>;
public:
List(){ first = new ListNode<Type>; first->link = first; };
void Delete(Type);
void Insert(Type);
private:
ListNode<Type>*first;
};
template<class Type>
class ListIterator{
public:
ListIterator(const List<Type>& l) :list(l), current(l.first->link){};
bool NotNull();
bool NextNotNull();
Type* First();
Type* Next();
private:
const List<Type> &list;
ListNode<Type>* current;
};
template<class Type>
bool ListIterator<Type>::NotNull(){
if (current != list.first)return true;
else return false;
}
template<class Type>
bool ListIterator<Type>::NextNotNull(){
if (current->link != list.first)return true;
else return false;
}
template<class Type>
Type* ListIterator<Type>::First(){
if (current != list.first)return ¤t->data;
else return 0;
}
template<class Type>
Type* ListIterator<Type>::Next(){
current = current->link;
if (current == list.first) current = current->link;//当下一个是first时,让他在下一个
return ¤t->data;
}
//插入是往最前面插入
template<class Type>
void List<Type>::Insert(Type k){
ListNode<Type> *newnode = new ListNode<Type>(k);//k是存放节点里面的数据;
newnode->link = first->link;
first->link = newnode;
}
template<class Type>
ListNode<Type>::ListNode(Type element){
data = element;
link = 0;
}
template<class Type>
void List<Type>::Delete(Type k){
ListNode<Type> *previous = first;//前一个
ListNode<Type> *current;
for (current = first->link; (current != first) && current->data != k; previous = current, current = current->link){
;//什么都不做,空循环,找到要被删除的节点
}
if (current != first){
/*if (previous){
previous->link = current->link;
}
else{
first = first->link;
}*/
previous->link = current->link;
delete current;
}
}
#endif
//main.cpp
#include<iostream>
#include<list>//C++STL中的链表
#include "MyList.h"
using namespace std;
int main(){
cout << "测试:" << endl;
cout << "这是我的链表和迭代器:" << endl;
List<int> initList;
initList.Insert(5);
initList.Insert(15);
initList.Insert(25);
initList.Insert(35);
ListIterator<int>li(initList);
//迭代是一种手段,可以一个一个的处理数据,想怎么处理就怎么处理
if (li.NotNull()){
cout << *li.First();
while (li.NextNotNull())
cout << " -> " << *li.Next();
cout << endl;
}
cout << "测试循环" << endl;
cout << *li.First() << endl;
cout << *li.Next() << endl;
cout << *li.Next() << endl;
cout << *li.Next() << endl;
cout << *li.Next() << endl;
cout << *li.Next() << endl;
cout << *li.Next() << endl;
cout << *li.Next() << endl;
cout << *li.Next() << endl;
cout << *li.Next() << endl;
system("pause");
return 0;
}
总结:循环链表在特殊的算法中会用到,比如著名的约瑟夫问题就要用到循环链表来解决。