forked from zhuxinquan/Data-Structure-And-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JosephusOperate.c
141 lines (131 loc) · 3.01 KB
/
JosephusOperate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*************************************************************************
> File Name: JosephusOperate.c
> Author: zhuxinquan
> Mail: [email protected]
> Created Time: 2015年09月10日 星期四 21时31分46秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef struct NodeType
{
int id;
int password;
struct NodeType *next;
}NodeType;
void CreatList(NodeType **, int);
NodeType * GetNode(int, int);
void Print_List(NodeType *);
int IsEmptyList(NodeType *);
void JosephusOperate(NodeType **, int);
int main(void)
{
int n = 0, m = 0;
NodeType *phead = NULL;
do{
if(n > MAX)
{
printf("人数过多,请重新输入!");
}
printf("请输入人数n(最多%d个)", MAX);
scanf("%d", &n);
}while(n > MAX);
printf("请输入初始密码m:");
scanf("%d", &m);
CreatList(&phead, n);
Print_List(phead);
JosephusOperate(&phead, m);
return 0;
}
void CreatList(NodeType **phead, int n)
{
int i = 0;
int ipassword = 0;
NodeType *pnew = NULL;
NodeType *pcur = NULL;
for(i = 1; i <= n; i++)
{
printf("请输入第%d个人的密码:", i);
scanf("%d", &ipassword);
pnew = GetNode(i, ipassword);
if(*phead == NULL)
{
*phead = pcur = pnew;
pcur->next = *phead;
}
else{
pnew->next = pcur->next;
pcur->next = pnew;
pcur = pnew;
}
}
printf("单向链表创建完成!\n");
}
NodeType *GetNode(int id, int ipassword)
{
NodeType *pnew = NULL;
pnew = (NodeType *)malloc(sizeof(NodeType));
if(!pnew)
{
printf("malloc error!\n");
exit(-1);
}
pnew->id = id;
pnew->password = ipassword;
pnew->next = NULL;
return pnew;
}
void Print_List(NodeType *phead)
{
NodeType *pcur = phead;
if(!IsEmptyList(phead))
{
printf("id password\n");
do{
printf("%3d, %7d\n", pcur->id, pcur->password);
pcur = pcur->next;
}while(pcur != phead);
}
}
int IsEmptyList(NodeType *phead)
{
if(!phead)
{
printf("the list is empty!\n");
return 1;
}
return 0;
}
void JosephusOperate(NodeType **phead, int ipassword)
{
int icount = 0;
int iflag = 1;
NodeType * prv = NULL;
NodeType * pcur = NULL;
NodeType * pdel = NULL;
prv = pcur = *phead;
while(prv->next != *phead)
{
prv = prv->next;
}
while(iflag)
{
for(icount = 1; icount < ipassword; icount++)
{
prv = pcur;
pcur = pcur->next;
}
if(prv == pcur)
{
iflag = 0;
}
pdel = pcur;
prv->next = pcur->next;
pcur = pcur->next;
ipassword = pdel->password;
printf("第%d个人出列(密码:%d)\n", pdel->id, pdel->password);
free(pdel);
}
*phead = NULL;
getchar();
}