一个简单的约瑟夫环代码

动态链表的课后作业。直接上代码:

// My accepted code of Q2
#include 
#include 
#include 
using namespace std;

typedef struct _LinkNode //开链表
{
	int value;
	struct _LinkNode* next;
}LinkNode, *LinkNodePtr;

LinkNodePtr createCycle(int total)
{
	int index = 1;
	LinkNodePtr head = NULL, curr = NULL, prev = NULL;//链表初始化
	head = (LinkNodePtr)malloc(sizeof(LinkNode));
	head->value = index;
	prev = head;

	while (--total > 0)//判断满链表?
	{
		curr = (LinkNodePtr)malloc(sizeof(LinkNode));
		curr->value = ++index;
		prev->next = curr;
		prev = curr;
	}
	curr->next = head;
	return head;
}

void run(int total, int tag)
{
	LinkNodePtr node = createCycle(total);
	LinkNodePtr prev = NULL;
	int start = 1;
	int index = start;
	while (node && node->next)
	{
		if (index == tag)
		{
			printf("n%d", node->value);
			if (tag == start)
			{
				prev = node->next;
				node->next = NULL;
				node = prev;
			}
			else
			{
				prev->next = node->next;
				node->next = NULL;
				node = prev->next;
			}
			index = start;
		}
		else
		{
			prev = node;
			node = node->next;
			index++;
		}
	}
}
int main(int argc, char* argv[])
{
	int peo;
	while (1)
	{
		cout > peo;
		if (peo == 1)
			break;
		else if (peo 

	

《一个简单的约瑟夫环代码》上有1条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注