C++单向循环链表实现

        单向循环链表与单向链表十分相似,具有关单向链表详细理论与实现过程可见“最详细的C++单向链表实现”。 实际上单向循环链表与单向链表仅仅有尾部node指针域指向不同的差别,对于单向链表尾部node由于没有后续node,其指针域需指向NULL。而单向循环链表将尾部node的指针域指向头部node,首位相连构成单向循环链表,具体形式为:

 具体实现代码为:

1、链表定义

//定义节点
class circle_node
{
public:
	int data;
	circle_node* next;
};
//定义循环链表
class circle_list
{
public:
	circle_node* head;
	int size;
};

//链表初始化
circle_list* list_init()
{
	circle_list* L =new circle_list;
	L->size=0;
	L->head=new circle_node;
	L->head->data=NULL;
	L->head->next=L->head;  //头指针初始指向自己
	return L;
}

这里与单向链表唯一的不同就是,头指针L->head->next=L->head指向自己而不是指向NULL。

2、链表插入

//插入链表
void list_insert(circle_list *L,int pos,int data)
{
	//创建新指针new_node;
	circle_node* new_node=new circle_node;
	new_node->data=data;
	new_node->next=NULL;
	//根据位置查找pos的前一个节点
	circle_node* pcurrent=L->head;
	for (int i = 0; i < pos; i++)
	{
		pcurrent=pcurrent->next;
	}
	//将new_node插入
	new_node->next=pcurrent->next;
	pcurrent->next=new_node;
	L->size++;
}

这里的插入方式与单向链表完全相同。

3、链表打印(注意打印过程是跳过头节点head的)

//打印链表,按num自由指定打印数据个数
void list_print(circle_list *L,int num)
{
	circle_node* pcurrent=L->head->next;
	for (int i = 0; i <num; i++)
	{
        if (pcurrent==L->head)
		{
			pcurrent=L->head->next;
		}
		cout<<pcurrent->data<<"\t";
		pcurrent=pcurrent->next;
	}
	cout<<endl;
}

由于单向循环链表首尾相连,因此打印链表时可根据需要打印node的数量指定num。

4、实验:

int main()
{
	//创建单向循环链表
	circle_list* L=list_init();
	//插入数据0~9
	for (int i = 0; i < 10; i++)
	{
		list_insert(L,i,i);
	}
	cout<<"链表中数据为:"<<endl;
	list_print(L,10);
	cout<<"链表中2倍数据为:"<<endl;
	list_print(L,20);
	system("pause");
	return 0;
}

结果为:

  • 8
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以帮你解答这个问题。 首先,让我们来了解一下约瑟夫问题的背景。约瑟夫问题是一个经典的问题,据传是由古代历史学家约瑟夫斯(Josephus)提出的。问题的具体描述如下: 有n个人围成一圈,从第一个人开始报数,报到m的人出圈,剩下的人继续从1开始报数,直到剩下最后一个人。求出最后一个出圈的人的编号。 为了解决这个问题,我们可以使用单向循环链表实现。下面是一个简单的C++单向循环链表实现,包含了三个文件:Node.h、List.h和List.cpp。 首先是Node.h文件,用来定义节点类: ```cpp // Node.h #ifndef NODE_H #define NODE_H class Node { public: int data; Node* next; Node(int data) { this->data = data; next = nullptr; } }; #endif ``` 然后是List.h文件,用来定义链表类: ```cpp // List.h #ifndef LIST_H #define LIST_H #include "Node.h" class List { public: List(); ~List(); void insert(int data); void remove(Node* node); Node* getHead(); Node* getNext(Node* node); private: Node* head; }; #endif ``` 最后是List.cpp文件,用来实现链表类的方法: ```cpp // List.cpp #include "List.h" List::List() { head = nullptr; } List::~List() { Node* curr = head; while (curr != nullptr) { Node* temp = curr; curr = curr->next; delete temp; } } void List::insert(int data) { Node* node = new Node(data); if (head == nullptr) { head = node; head->next = head; } else { Node* curr = head; while (curr->next != head) { curr = curr->next; } curr->next = node; node->next = head; } } void List::remove(Node* node) { Node* curr = head; if (node == head) { head = head->next; } else { while (curr->next != node) { curr = curr->next; } curr->next = node->next; } delete node; } Node* List::getHead() { return head; } Node* List::getNext(Node* node) { return node->next; } ``` 现在我们就可以使用这个单向循环链表来解决约瑟夫问题了。下面是一个简单的解决方案: ```cpp #include <iostream> #include "List.h" int josephus(int n, int m) { List list; for (int i = 1; i <= n; i++) { list.insert(i); } Node* curr = list.getHead(); while (n > 1) { for (int i = 1; i < m; i++) { curr = list.getNext(curr); } Node* temp = curr; curr = list.getNext(curr); list.remove(temp); n--; } return list.getHead()->data; } int main() { int n = 5; int m = 3; int result = josephus(n, m); std::cout << "The last person is " << result << std::endl; return 0; } ``` 这个程序将输出: ``` The last person is 4 ``` 这就是使用C++单向循环链表解决约瑟夫问题的一个简单的例子。希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值