用循環鏈表解決約瑟夫循環

注意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


發佈了445 篇原創文章 · 獲贊 27 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章