个人主页:
专栏:
目录
循环单链表
特点
循环单链表的表尾结点的next指针总是指向头结点。
所以在初始化循环单链表的时候,需要记得将头结点的next指针指向头结点自己。
判断循环单链表是否为空,只要判断头结点的next指针是否指向自己就可以了,因为循环链表的最后一个结点的next指针总是指向头结点的,如果为空,那就只能是头结点的next指针指向自己了。
判断结点是否是循环单链表的表尾结点,只需判断该结点的next指针是否指向头结点即可,因为循环链表的最后一个结点的next指针总是指向头结点的。
性质
1、从一个结点出发,无论这个结点位于链表的哪里,都可以找到其他任何一个结点,而单链表不行。
2、在一些情况下,我们需要频繁对链表的头部和尾部进行操作,此时使用循环单链表就很有用,原因是对于单链表,已知头结点,想找到最后一个结点的话,时间复杂度是O(n);但是对于循环单链表,我们可以一开始就把头指针指向尾结点,这样找到尾结点所需时间复杂度是O(1),因为尾结点的next指针总是指向头结点,所以找到头结点的时间复杂度也是O(1)。因此,循环单链表在这种情况下的表现明显优于普通的单链表。
约瑟夫问题
设计循环链表解决约瑟夫问题:给定一组输入数据 a0, a1, ...,aN(int 型、float 型或 char 型),选取 x(x>=1)作为计数上限,从 a0 开始按顺序报数,将第 x 个元素出列。x 之后的第一个元素继续从 1 开始报数,报到 x 的元素再出列,重复该过程 y 次(y<=数组长度)。
- 要求使用链表和泛型编程,输出每次出列元素的值。从最后一个出列元素的下一个元素开始,按循环顺序输出所有剩余元素。
输入
第一行输入数组的数据类型(int, float 或 char);
第二行输入数组的大小 N(N >= 0);
第三行输入数组的元素,元素之间采用单个空格分隔;
第四行输入 x y 的值
输出
第一行输出原始的输入数组;
第二行至第 y+1 行输出出列元素;
最后一行输出所有剩余元素。
样例
输入:
int
5 1 2 3 4 5
2 2输出:
1 2 3 4 5
2 4
解题步骤
结点类
该题需采用泛型编程,故定义结点类时需要使用模板
template <typename T>
struct Node
{
Node* next;
T value;
};
链表类
循环链表需要定义当前操作指针record,因为循环链表可以从尾部重新回到头部,构造函数只需让record指向空结点,析构函数则用来record指针,并且记录链表长度,在插入和删除时对长度进行维护
template <typename T>
class List
{ // 定义单向循环链表
public:
Node<T>* record; // 定义当前操作指针(null)
int length;
List() : record(nullptr), length(0) {}; // 初始化链表
~List() { delete record; }
insert函数
向循环链表中插入一个节点,值为value
在循环链表尾部插入结点,需要将新节点的next指向头部,当前尾部的next指向新节点,record指向新节点,以保证该链表为循环的,并且链表长度增加1
void insert(T value)
{
Node<T>* node = new Node<T>; // 新加入节点 node
node->value = value; // 新节点 node 赋值
node->next = nullptr; // 新节点的 next 节点
length = length + 1; // 链表长度增加
if (record == nullptr) {
record = node;
record->next = node;
}
else {
node->next = record->next;
record->next = node;
record = node;
}
}
printAll函数
遍历并输出循环链表
record->next指向的链表头部,故从record下一个结点开始遍历输出
void printAll()
{
Node<T>* p = record;
if (p == nullptr)
{ // 若 record 为空,则链表为空,输出为空
cout << endl;
return;
}
cout << p->next->value; // 否则输出头指针的值
for (int i = length; i > 1; i--)
{
p = p->next;
cout <<" " << p->next->value;
}
cout << endl;
}
};
pop函数
将position位置的节点进行出队,返回值为该节点
题目要求将指定位置的结点进行删除,故需要自定义一个pop函数,先通过循环使得指针指向被删除节点的前驱节点,再将前驱节点的next指向删除节点的next,实现元素删除,链表长度减1
T pop(int position)
*/
{
position = position + length;
for (int i = 1; i < position ; i++) // 通过顺序循环,将当前操作指针移动到被删除节点的前驱
{
record = record->next;
}
Node<T>* p = record->next;
record->next = p->next;
T temp = p->value;
delete p; // 智能指针删除节点
length = length - 1; // 元素出栈后,链表长度减一
if (length == 0)
record = nullptr; // 链表中没有元素,指向空指针
return temp;
}
test函数
调用函数完成题目要求
template <typename T>
void test()
{
int length, position, circle;
T value;
List<T> mylist;
cin >> length;
for (; length > 0; length--)
{
cin >> value;
mylist.insert(value);
}
mylist.printAll();
cin >> position;
cin >> circle;
for (int i = 0; i < circle; i++)
{
cout << mylist.pop(position) << endl;
}
mylist.printAll();
}
完整代码
#include <iostream>
using namespace std;
template <typename T>
struct Node
{ // 定义节点
Node* next;
T value;
};
template <typename T>
class List
{ // 定义单向循环链表
public:
Node<T>* record; // 定义当前操作指针(null)
int length;
List() : record(nullptr), length(0) {}; // 初始化链表
~List() { delete record; }
void insert(T value)
/*
函数名:insert
输入值:泛型 value
功 能:向循环链表中插入一个节点,值为value
*/
{
Node<T>* node = new Node<T>; // 新加入节点 node
node->value = value; // 新节点 node 赋值
node->next = nullptr; // 新节点的 next 节点
length = length + 1; // 链表长度增加
if (record == nullptr) {
record = node;
record->next = node;
}
else {
node->next = record->next;
record->next = node;
record = node;
}
}
T pop(int position)
/*
函数名:pop
输入值:整型 position
功 能:将position位置的节点进行出队,返回值为该节点
*/
{
position = position + length;
for (int i = 1; i < position ; i++) // 通过顺序循环,将当前操作指针移动到被删除节点的前驱
{
record = record->next;
}
Node<T>* p = record->next;
record->next = p->next;
T temp = p->value;
delete p; // 智能指针删除节点
length = length - 1; // 元素出栈后,链表长度减一
if (length == 0)
record = nullptr; // 链表中没有元素,指向空指针
return temp;
}
void printAll()
/*
函数名:printAll
输入值:无
功 能:遍历并输出队列
*/
{
Node<T>* p = record;
if (p == nullptr)
{ // 若 record 为空,则链表为空,输出为空
cout << endl;
return;
}
cout << p->next->value; // 否则输出头指针的值
for (int i = length; i > 1; i--)
{
p = p->next;
cout <<" " << p->next->value;
}
cout << endl;
}
};
template <typename T>
void test()
{
int length, position, circle;
T value;
List<T> mylist;
cin >> length;
for (; length > 0; length--)
{
cin >> value;
mylist.insert(value);
}
mylist.printAll();
cin >> position;
cin >> circle;
for (int i = 0; i < circle; i++)
{
cout << mylist.pop(position) << endl;
}
mylist.printAll();
}
int main()
{
string dtype;
cin >> dtype;
if (dtype == "int")
test<int>();
else if (dtype == "float")
test<float>();
else
test<char>();
return 0;
}