上篇对单链表进行了详细说明,本篇接下来说说单循环链表
单循环链表就是在单链表基础上,尾巴元素指向了头,形成了圆圈,操作时只要注意尾部元素链接头就OK,废话不多说,直接上代码
单循环链表定义类 CircleLinkedList.h
#ifndef CIRCLE_LINKEDLIST_H
#define CIRCLE_LINKEDLIST_H
#include <iostream>
#include <cstdlib>
using std::cin;
using std::cout;
using std::endl;
using std::istream;
using std::ostream;
using std::cerr;
template <class T>
struct LinkNode
{
T data;//值域
LinkNode<T> *link;//指针域
LinkNode(LinkNode<T> *ptr = nullptr)
{
link = ptr;
}
LinkNode(const T& item,LinkNode<T> *ptr = nullptr)
{
data = item;
link = ptr;
}
};
template <class T>
class CircleList
{
public:
CircleList()//构造函数
{
first = new LinkNode<T>;
first->link = first;//循环起来,围成圆圈
length = 0;
}
~CircleList()//析构函数
{
clear();
delete first;
}
int Length()const;//返回链表长度
LinkNode<T> *getHead()const{
return first;
}
LinkNode<T> *Search(const T &x);//查找x是否存在链表中
void clear();//清空链表,仅余头结点
LinkNode<T> *locate(int i) const;//返回第i个元素,元素下标从1开始
bool insert(int i,T& t);//在第i个位置插入元素
bool remove(int i);//移除第i个位置插入元素
bool isEmpty()const{
return (first->link == first)?true:false;
}
friend istream& operator >> (istream &in,CircleList<T> &list)
{
list.clear();//首先list清空
LinkNode<T> *first,*last;
first = list.first;
last = first;
while(!in.eof())
{
T val;
in >> val;
LinkNode<T> *node = new LinkNode<T>(val);
last->link = node;
list.length++;
last = node;
}
last->link = first;//尾结点指向头成圆
return in;
}
friend ostream& operator << (ostream& out,const CircleList<T> &list)
{
LinkNode<T> *p = list.first->link;
int i = 1;
while(p != list.first)
{
cout <<"#" << i <<":" << p->data << endl;
p = p->link;
++i;
}
return out;
}
protected:
LinkNode<T> *first;//记录链表头指针
int length;//记录链表长度
};
template <class T>
void CircleList<T>::clear()
{
LinkNode<T> *p = first->link;
LinkNode<T> *q;
while(p != this->first)
{
q = p;
p = p->link;//p向后移
first->link = p;//重新与头结点建立联系
delete q;//删除结点
length--;
}
}
template <class T>
LinkNode<T> * CircleList<T>::Search(const T &x)
{
LinkNode<T> *p = this->first->link;
while(p != this->first)
{
if(p->data == x)
{
return p;
}
p = p->link;
}
return p;
}
template <class T>
LinkNode<T> * CircleList<T>::locate(int i) const
{
if(i == 0)
{
return first;
}
if(i < 0 || i > length)
{
return first;
}
LinkNode<T> *p = first->link;
for(int j = 1; j < i; j++)
{
p = p->link;
}
return p;
}
template <class T>
bool CircleList<T>::insert(int i,T& t)
{
LinkNode<T> *current = locate(i-1);
if(current == first)
{
return false;
}
LinkNode<T> *node = new LinkNode<T>(t);
if(node == nullptr)
{
cerr << "memory malloc failed!" << endl;
exit(1);
}
node->link = current->link;
current->link = node;
++length;
return true;
}
template <class T>
bool CircleList<T>::remove(int i)
{
LinkNode<T> *current = locate(i-1);
if(current == first)
{
return false;
}
LinkNode<T> *p = current->link;
current->link = p->link;
delete p;
length--;
return true;
}
#endif
测试类 main.cpp
#include "CircleLinkedList.h"
#include <fstream>
using std::ifstream;
using std::ios;
int main()
{
CircleList<int> list;
//list.init();
ifstream in = ifstream("list.txt",ios::in);
in >> list;
cout << "The initial list in the file is:\n" << list << endl;
/*cout << "input the number you want search:";
int number;
cin >> number;
LinkNode<int> *p = list.Search(number);
if(p == list.getHead())
{
cout << number << " not exsited" << endl;
}
else
{
cout << number << " exsited" << endl;
}*/
/*list.clear();
cout << "The cleaded list in the file is:\n" << list << endl;*/
/*cout << "input the index you want locate:";
int index;
cin >> index;
LinkNode<int> *p = list.locate(index);
if(p == list.getHead())
{
cout << "element at " << index << " not exsited" << endl;
}
else
{
cout << "element at " << index << " is " << p->data <<endl;
}*/
/*cout << "input the index、value you want insert ,split by space ";
int index;
int value;
cin >> index >> value;
if(list.insert(index,value))
{
cout << list << endl;
}*/
cout << "input the index you want delete ";
int index;
cin >> index ;
if(list.remove(index))
{
cout << list << endl;
}
in.close();
system("pause");
return 0;
}
/*
int main()
{
ifstream in = ifstream("list.txt",ios::in);
int num;
if(!in)
{
cout << "文件打不开" << endl;
}
else
{
while(in >> num)
{
cout << num;
}
}
in.close();
system("pause");
return 0;
}*/
数据输入文件 list.txt
1 2 3 4 56 7 890 34 56 24 578 1246 45