用循环链表解决约瑟夫循环

本文详细阐述了如何使用简单循环链表结构来解决约瑟夫环问题,包括节点定义、链表操作及约瑟夫环算法的具体实现步骤。通过实例演示,帮助读者理解链表在特定问题场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意1在getElemPtr的时候的position的范围是0到Length()
2在约瑟夫循环中应为出去的位置是position-1而出去了之后紧接着原来的元素位置被后面一个元素补上,所以position—1的位置

#include <iostream>
#include "Node.h"
#include "SimpleCircLinkList.h"
using namespace std;
void Josephus(int n,int m)
{
	SimpleCircLinkList<int> la;
	for(int i=1;i<=n;i++)
		la.Insert(i,i);
	cout<<"length"<<la.Length()<<endl;
	int curPosition=1;
	int elem;
	while(la.Length()>1)
	{
		for(int i=0;i<m;i++)
		{
			if(curPosition>la.Length())
				curPosition=1;
			curPosition++;
		}
		curPosition--;//应为出去的位置是position-1而出去了之后紧接着原来的元素位置被后面一个元素补上,所以position—1的位置
		la.Delete(curPosition,elem);
		cout<<"outer is"<<elem<<endl;
	}
	la.GetElem(1,elem);
	cout<<"winner is "<<elem<<endl;
}

void main()
{
	Josephus(8,3);
}


#ifndef _NODE_H_
#define _NODE_H_
#include <iostream>
using namespace std;

template<class ElemType>
class Node
{
public:
	ElemType data;
	Node<ElemType> *next;

	Node();
	Node(ElemType e,Node<ElemType>* link);
};

template<class ElemType>
Node<ElemType>::Node()
{
	next=NULL;
}

template<class ElemType>
Node<ElemType>::Node(ElemType e,Node<ElemType>* link)
{
	data=e;
	next=link;
}

#endif

#ifndef _SIMPLECIRCLINKLIST_H_
#define _SIMPLECIRCLINKLIST_H_
#include <iostream>
#include "Node.h"
using namespace std;
enum StatusCode{SUCCESS,RANGE_ERROR};

template<class ElemType>
class SimpleCircLinkList
{
	protected:
		Node<ElemType> *head;

		void Init();
		Node<ElemType>* GetElemPtr(int position)const;

	public:
		SimpleCircLinkList();
		virtual ~SimpleCircLinkList();

		void Clear();
		int Length()const;
		bool Empty()const;


		StatusCode GetElem(int position,ElemType &e)const;
		StatusCode SetElem(int position,const ElemType &e);
		StatusCode Delete(int position,ElemType &e);
		StatusCode Insert(int position,const ElemType &e);

		SimpleCircLinkList(const SimpleCircLinkList<ElemType> &copy);
		SimpleCircLinkList<ElemType>& operator = (const SimpleCircLinkList<ElemType> &copy);
};

template<class ElemType>
SimpleCircLinkList<ElemType>::SimpleCircLinkList()
{
	head=new Node<ElemType>;
	head->next=head;
	Init();
}

template<class ElemType>
void SimpleCircLinkList<ElemType>::Init()
{
	Clear();
	head->next=head;
}

template<class ElemType>
void SimpleCircLinkList<ElemType>::Clear()
{
	ElemType Elem;
	while(Length()>0)
		Delete(1,Elem);
}

template<class ElemType>
SimpleCircLinkList<ElemType>::~SimpleCircLinkList()
{
	Clear();
	delete head;
}

template<class ElemType>
bool SimpleCircLinkList<ElemType>::Empty()const 
{
	return head->next==head;
}

template<class ElemType>
int SimpleCircLinkList<ElemType>::Length()const
{
	int count=0;
	for(Node<ElemType> *ptr=head->next;ptr!=head;ptr=ptr->next)
		count++;
	return count;
}

template<class ElemType>
Node<ElemType>* SimpleCircLinkList<ElemType>::GetElemPtr(int position)const
{
	if(position<0||position>Length())//注意在getElemPtr的时候的position的范围是0到Length()
		return NULL;
	else
	{
		for(Node<ElemType> *ptr=head;position>0;position--)
			ptr=ptr->next;
		return ptr;
	}
}

template<class ElemType>
StatusCode SimpleCircLinkList<ElemType>::GetElem(int position,ElemType &e)const
{
	if(position<1||position>Length())
		return RANGE_ERROR;
	else
	{
		//cout<<"e="<<e<<endl;
		e=GetElemPtr(position)->data;
		return SUCCESS;
	}
}

template<class ElemType>
StatusCode SimpleCircLinkList<ElemType>::SetElem(int position,const ElemType &e)
{
	if(position<1||position>Length())
		return RANGE_ERROR;
	else
	{
		GetElemPtr(position)->data=e;
		return SUCCESS;
	}
}

template<class ElemType>
StatusCode SimpleCircLinkList<ElemType>::Insert(int position,const ElemType &e)
{
	
	if(position<1||position>Length()+1)
		return RANGE_ERROR;
	else
	{
		cout<<"insert"<<endl;
		Node<ElemType> *ptr=GetElemPtr(position-1);
		Node<ElemType> *ptrNext;
		ptrNext=new Node<ElemType>(e,(ptr->next));
		ptr->next=ptrNext;
		return SUCCESS;
	}
}

template<class ElemType>
StatusCode SimpleCircLinkList<ElemType>::Delete(int position,ElemType &e)
{
	if(position<1||position>Length())
		return RANGE_ERROR;
	else
	{
		Node<ElemType> *ptr=GetElemPtr(position-1);
		Node<ElemType> *ptrNext=ptr->next;
		e=ptrNext->data;
		ptr->next=ptrNext->next;
		delete ptrNext;
		return SUCCESS;
	}
}

template<class ElemType>
SimpleCircLinkList<ElemType>::SimpleCircLinkList(const SimpleCircLinkList<ElemType> &copy)
{
	head=new Node<ElemType>;
	Init();
	for(Node<ElemType> *ptrCopy=copy.head->next;ptrCopy!=head;ptrCopy=ptrCopy->next)
	{
		Node<ElemType> *ptr=Node<ElemType>(ptrCopy->e,NULL);
		ptr=ptr->next;
	}
	ptr->next=head;
}

template<class ElemType>
SimpleCircLinkList<ElemType>& SimpleCircLinkList<ElemType>::operator = (const SimpleCircLinkList<ElemType> &copy)
{
	if(&copy!=this)
	{
		head=new Node<ElemType>;
		Init();
		for(Node<ElemType> *ptrCopy=copy.head->next;ptrCopy!=head;ptrCopy=ptrCopy->next)
		{
			Node<ElemType> *ptr=Node<ElemType>(ptrCopy->e,NULL);
			ptr=ptr->next;
		}
		ptr->next=head;
	}
	return *this;
}
#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值