魔术师发牌 发发发~

约瑟夫环的变种 就是以约瑟夫的方式往里面插牌 还怪难理解的 emmm做一简单记录👧
还有拉丁方阵 也整起来~我又可以了 指针虐我千百遍,我待指针如初恋🤣

//约瑟夫环
void DispatchCard(Linklist *L)
{
	int count = 1;
	Node *CurrNode = new Node();
	CurrNode = L->head;
	CurrNode->data = 1;
	while (count< GetCircleLength(*L))
	{
		count++;
		int move = 0;
		while (move<count)
		{
			CurrNode = CurrNode->next;
			if (CurrNode->data==0)//往没牌的节点里插数
			{
				move++;
			}
		}
		CurrNode->data = count;
		PrintCircle(*L);

	}
}
void test07()
{
	Linklist L;
	InitialCircleList(&L);
	for (int i = 0; i <13; i++)
	{
		InsertCircle(&L, i, 0);
	}//插入13个空元素
	DispatchCard(&L);
	cout << "最终序列为:" << endl;
	PrintCircle(L);

}
//链表的基本函数
//循环链表
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;

}

int main()
{
	cout << "正在测试我的代码。。" << endl;
	srand((unsigned int)time(NULL));
	test07();
	system("pause");
}
---------------------------------------------
//拉丁方阵
void PrintCircle(Linklist L,int startPosition)//重载打印函数
{
	int count = 0;//显示数目
	int move = 0;//指针移动长度
	Node* currNode = new Node();
	currNode = L.head;
	while (move < startPosition)
	{
		currNode = currNode->next;
		move++;
	}

	while (count< GetCircleLength(L))
	{
		cout << currNode->data << " ";
		currNode = currNode->next;
		count++;
	}
	cout << endl;
}
void Latin()
{
	Linklist L;
	InitialCircleList(&L);
	for (int i = 0; i < 3; i++)//数量可以改 写个cin>>语句也可😁
	{
		InsertCircle(&L, i, i+1);
	}
	for (int i=0;i<3;i++)
	{
		PrintCircle(L,i);
	}
}

int main()
{
	cout << "正在测试我的代码。。" << endl;
	srand((unsigned int)time(NULL));
	Latin();
	system("pause");
}

忧桑😭数据结构啥时候是个头啊

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值