循环链表-约瑟夫环

一直在跟着小甲鱼学习,然后讲到了约瑟夫环的问题,具体问题就不描述了,贴一下自己写的代码 虽然很辣鸡 但是能跑😂看见有用递归方法做的,还没有学到递归,争取早日能mark递归的方法把!!!

//以下为约瑟夫主函数
void Josef(Linklist *L)
{
	Node *CurrNode = new Node();

	CurrNode = L->head;
	int count = 1;
	while (GetCircleLength(*L)>0)//这个数量随便改就👌
	{
		if (count%3==2)
		{
			
			Node *n = new Node();
			n=CurrNode->next;

			if (GetCircleLength(*L)==1)
			{
				//只有一个头节点了 把他删光
				InitialCircleList(L);
				return;
			}
			if (L->head==CurrNode->next)//考虑头节点 不然把头节点删掉 链表就崩溃掉了
			{
				L->head = CurrNode->next->next;
			}
			CurrNode->next = CurrNode->next->next;
			cout << n->data<<" ";
			delete n;

		}
		else
		{
			CurrNode = CurrNode->next;
		}
		count++;
	}
	cout << endl;
}

void test06()
{
	Linklist L;
	InitialCircleList(&L);
	for (int i = 0; i < 41; i++)
	{
		InsertCircle(&L, i, i+1);
	}
	Josef(&L);
	cout << "约瑟夫完事之后的链表:" << endl;
	PrintCircle(L);
}
int main()
{
	cout << "正在测试我的代码。。" << endl;
	srand((unsigned int)time(NULL));
	test06();

	system("pause");
}
//以下为循环链表相关函数:查找 长度 删除 初始化等
void InitialCircleList(Linklist *L)
{
	Node *n = new Node();
	n->next = n;
	L->head = n;
}
int GetCircleLength(Linklist L)
{
	int count = 1;
	Node *current = new Node();
	current = L.head;
	while (current->next !=L.head)
	{
		count++;
		current = current->next;
	}
	//delete current;
	return count;
}
void InsertCircle(Linklist *L, int i, int elem)
{
	Node *n = new Node();
	n->data = elem;
	n->next = NULL;
	if (i<0 )
	{
		return;
	}
	int count = 0;
	if (i==0)
	{
		
		n->next = n ;
		L->head = n;
		return;
	}
	Node *CurrNode = new Node();
	CurrNode = NULL;
	CurrNode = L->head;
	while ((CurrNode->next != L->head) &&(count<i-1))
	{
		CurrNode = CurrNode->next;
		count++;
	}
	if (count==i-1)
	{
		

		n->next= CurrNode->next;
		CurrNode->next = n;
		L;
	}
	//delete CurrNode;
}
void PrintCircle(Linklist L)
{
	int index = 0;
	if (GetCircleLength(L)==0)
	{
		return;
	}
	
	Node *CurrNode = new Node();
	CurrNode = L.head;
	while (index< GetCircleLength(L))
	{
		cout << CurrNode->data<<" ";
		CurrNode = CurrNode->next;
		index++;
	}
	cout << endl;

}
void DeleteCircleByIndex(Linklist *L, int i)
{
	if (i<0||i>GetCircleLength(*L))
	{
		return;
	}
	Node *n = new Node();
	Node *CurrNode = new Node();
	CurrNode = L->head;
	int count = 0;
	while (CurrNode->next!= L->head && count<i-1)
	{
		CurrNode = CurrNode->next;
		count++;
	}
	n=CurrNode->next;
	CurrNode->next = CurrNode->next->next;
	delete n;
}
void SerachElemByIndex(Linklist L, int i)//i从0开始算
{
	if (i < 0||i>GetCircleLength(L))
	{
		cout << "链表查找越界" << endl;
		return;
	}
	Node *CurrNode = new Node();
	CurrNode = L.head;
	int count = 0;
	while (CurrNode->next!=CurrNode && count<i-1)
	{
		CurrNode = CurrNode->next;
		count++;
	}
	cout << "您要查找的第"<<i<<"个元素为:"<<CurrNode->next->data << endl;

}

艾玛,每天进步一丢丢,我可以的👧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值