约瑟夫问题

最近在学数据结构过程中再次遇到了这个问题,所以把代码又重新敲了一遍,问题还不少。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef int DataType;

class Node
{

public:
	Node(DataType str=1,Node *ptr=NULL)
		:data(str),next(ptr){}

	void ShowData()
	{
		cout<<data<<" ";
	}
	void SetData(DataType &one)
	{
		data=one;
	}

private:
	DataType data;//数据成员
public:
	Node *next;//指向下一个节点
};

class CircleList
{
public:
	CircleList (int n=1);
	~CircleList();

	void DeleteNode(Node *ptr);
	Node* AppendNode(DataType &one);
	void ShowNode(Node *ptr)const;
	void SetData(Node *ptr,DataType &one);
	Node* GetHead()const;
	int GetSize()const;
	Node* GetFront(Node*ptr)const;
private:
	Node *head;//头指针

	Node *NewNode();
};

CircleList::CircleList (int n)
{
	Node *ptmp;
	Node *ptr=NULL;
	if(n<1)
	{
		cout<<"The length of the list can't be less one!"<<endl;
		exit(1);
	}
	head=NULL;
	for(int i=0;i<n;i++)
	{
		ptmp=new Node;
		if(head==NULL)
		{
			head=ptmp;
			ptr=head;
		}
		else
		{
			ptr->next=ptmp;
			ptr=ptmp;
		}
	}
	ptr->next =head;
}

CircleList ::~CircleList ()
{
	if(head)
	{
		Node *ptmp1,*ptmp2;
		ptmp1=head->next ;
		for(ptmp2=ptmp1->next ;ptmp2!=head;ptmp2=ptmp2->next )
		{
			delete(ptmp1);
			ptmp1=ptmp2;
		}
		delete(head);
		head=NULL;
	}
}

void CircleList::DeleteNode(Node *ptr)
{
	//if(ptr->next ==ptr)
	//{
	//	delete ptr;
	//	return ;
	//}
	//if(ptr->next ==head)
	//{
	//	ptr->next=head->next;
	//	delete(head);
	//	head=ptr->next ;
	//	return ;
	//}
	//Node *ptmp=ptr->next;
	//ptr->next=ptmp->next ;
	//delete ptmp;
	//return;
	if(GetSize ()<=1)
	{
		delete ptr;
		head=NULL;
		return;
	}
	if(ptr==head)
	{
		GetFront(ptr)->next =head->next ;
		head=head->next ;
		delete ptr;
		return;
	}
	GetFront(ptr)->next =ptr->next ;
	delete ptr;
	return ;

}

Node* CircleList::AppendNode(DataType &one)
{
	Node *ptmp;
	Node *pNew=NewNode ();
	for(ptmp=head;ptmp->next !=head;ptmp=ptmp->next)
	{}
	pNew->SetData (one);
	ptmp->next =pNew;
	pNew->next =head;
	return pNew;
}

void CircleList::ShowNode(Node *ptr)const
{
	ptr->ShowData ();
}

void CircleList::SetData(Node *ptr,DataType &one)
{
	ptr->SetData (one);
}

Node* CircleList::NewNode()
{
	Node *p=new Node;
	if(!p)
		exit(1);
	return p;
}

Node* CircleList::GetHead()const
{
	return head;
}

int CircleList ::GetSize ()const
{
	int i=0;
	if(!head)
		return i;
	for(Node*p=head;p->next !=head;p=p->next )
		i++;
	return ++i;
}

Node* CircleList ::GetFront (Node*ptr)const
{
	Node* p=head;
	for(;p->next!=ptr;p=p->next )
	{}
	return p;
}
int main()
{
	CircleList question(41);
	int i=1;
	Node*pi=question.GetHead ();//为头结点赋值
	pi->SetData (i);
	i++;
	pi=pi->next ;
	for(;pi!=question .GetHead ();pi=pi->next  )//为其他节点赋值
	{
		pi->SetData(i);
		i++;
	}
	//for(pi=question.GetHead ();pi->next !=question.GetHead ();pi=pi->next )
	//{
	//	pi->ShowData ();
	//	question .GetFront(pi)->ShowData ();
	//}
	Node* pj=question .GetHead ();
	int teamnum=41;
	Node* pk;
	while(teamnum>0)
	{
		pj=pj->next ->next;
		pj->ShowData ();
		pk=pj->next ;
		question.DeleteNode (pj);
		pj=pk;
		teamnum --;
	}
	return 0;
}


以上是源代码。这次遇到的问题有:

1.类的设计过程中忽视const函数作用,所有函数一开始全都采用非const类型

2.类的功能考虑不周全,总是在main函数实现中才意识到类需要其他的功能

3.在调试出现问题的时候问题不总是出现在类函数中,可能main()函数本身的逻辑有问题,在调用类接口的时候一定要明确接口的确切作用

如:void CircleList::DeleteNode(Node* ptr)这个函数作用一开始我定义为删除ptr指针对象的下一个对象,但是在main()函数使用中却误以为是删除ptr所指对象

4.new函数与operator new的区别(附:http://blog.sina.com.cn/s/blog_69e905cd0100k51b.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值