Skip to content

Instantly share code, notes, and snippets.

@myisjon
Last active May 31, 2019 03:26
Show Gist options
  • Save myisjon/688b2fa0514e288fbeaf to your computer and use it in GitHub Desktop.
Save myisjon/688b2fa0514e288fbeaf to your computer and use it in GitHub Desktop.
# include "stdafx.h"
# include "iostream"
using namespace std
typedef int DataType
typedef struct Node
{
DataType data
struct Node * next
}RingNode
//初始化
RingNode * RingNode_Init()
{
return (RingNode *)malloc(sizeof(RingNode))
}
//建立建立循环链表
void RingNode_Insert(RingNode * head, int n)
{
RingNode * q, *p = head
DataType i = 1
q = new RingNode
q->data = i
head = q
while(i < n + 1)
{
i + +
p->next = q
p = q
q = new RingNode
q->data = i
}
p->next = head
}
//输出约瑟夫环
void RingNode_delete(RingNode * head, int m)
{
int count = 1
RingNode * p = head ->next
RingNode * q = p
while(p->next != p)
{
if(count % m)
{
q = p
p = p->next
}
else
{
q->next = p->next
cout << p->data << " "
free(p)
p = q->next
}
count + +
}
cout << p->data << endl
}
int _tmain(int argc, _TCHAR * argv[])
{
RingNode * p = RingNode_Init();
int n, m;
cout << "请输入人数:";
cin >> n;
cout << "请输入停止数:";
cin >> m;
RingNode_Insert(p, n);
RingNode_delete(p, m);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment