鱼C论坛

 找回密码
 立即注册
查看: 2744|回复: 0

循环链表解决约瑟夫问题

[复制链接]
发表于 2015-9-3 11:00:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
#include<cstdlib>
using namespace std;

//循环链表结点类的定义与实现
class CirListNode
{
        int data;
        CirListNode *Next;
public:
        CirListNode():Next(NULL){}
        CirListNode(int Value):data(Value),Next(NULL){}
        ~CirListNode(){};
        void SetNext(CirListNode *next);
        int GetData();
        CirListNode *GetNext();
};

void CirListNode::SetNext(CirListNode *next)
{
        Next=next;
}

int CirListNode::GetData()
{
        return data;
}

CirListNode* CirListNode::GetNext()
{
        return Next;
}


//循环链表的定义与实现
class CirList
{
        CirListNode *head;
        CirListNode *tail;
        CirListNode *cur;          //记录着当前被访问结点的位置
public:
        CirList();
        ~CirList(){};
        void AddTail(int data);
        int GetCount();
        void SetBegin();
        void RemoveThis();           //删除cur所指向的结点
        void RemoveAll();
        int  ListGetNext();               //返回当前结点中的数据,并使cur顺序移动一个位置
};

CirList::CirList()
{
        head=tail=new CirListNode;
        cur=NULL;
        head->SetNext(head);            //创建初始循环
}

void CirList::AddTail(int data)
{
        CirListNode *add=new CirListNode(data);
        tail->SetNext(add);
        tail=tail->GetNext();
        tail->SetNext(head);
}

int CirList::GetCount()
{
        int count=0;
        CirListNode *here=cur;
        while(cur->GetNext()!=here)
        {
                ++count;
                cur=cur->GetNext();
        }
        cur=cur->GetNext();
        return count;
}

void CirList::SetBegin()
{
        cur=head;
}

void CirList::RemoveThis()
{
        if(cur==head)
        {
                cur=cur->GetNext();         //表头结点不可删除
        }
        CirListNode *precur=cur;             //使precur指向cur的前一结点
        for(int i=0;i<GetCount();i++)
        {
                precur=precur->GetNext();
        }
        precur->SetNext(cur->GetNext());
        //删除后cur顺序移动一个结点
        cur=precur->GetNext();
        precur=NULL;
}

void CirList::RemoveAll()
{
        SetBegin();
        int length=GetCount();
        for(int i=0;i<length;i++)
        {
                RemoveThis();
        }
        cur=head;
}

int CirList::ListGetNext()
{
        if(cur==head)
        {
                cur=cur->GetNext();
        }
        int num=cur->GetData();
        cur=cur->GetNext();
        return num;
}

//利用循环链表解决约瑟夫问题

int main()
{
        CirList jos;
       
        for(int i=1;i<16;i++)
        {
                jos.AddTail(i);
        }
        jos.SetBegin();
        int length=jos.GetCount();
        for(int i=1;i<length;i++)
        {
                for(int j=0;j<3;j++)
                {
                        jos.ListGetNext();
                }
                jos.RemoveThis();
        }
        cout<<jos.ListGetNext()<<endl;
       
        system("pause");
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-22 17:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表