-
Notifications
You must be signed in to change notification settings - Fork 0
/
Joseph.c
118 lines (93 loc) · 2.07 KB
/
Joseph.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
#include<stdio.h>
#include<malloc.h>
#define m 2
#define n 5
typedef struct People
{
int number;
struct People *next;
}People;
People* Create_List(People* L) //建立循环链表
{
People *p = NULL;
People *curr;
int i = 1; //i为计数工具
while(i != n+1)
{
curr = (People *)malloc(sizeof(People));
if(i == 1)
{
p = curr;
L = curr;
}
else
{
p->next = curr;
p = curr;
}
curr->number = i;
i += 1;
}
p->next = L;
printf("creating:%x\n",L);
return L;
}
void Print_List(People *L)
{
int i;
People *u;
u =(People *)malloc(sizeof(People));
for(i = 1; i<n+1;i++)
{
printf("%d\n",L->number);
L = L->next; //stop running
}
L = u;
printf("printing:%x\n",L);
}
void List_kind(People* L)
{ //L为n个人构成的链表,报数报到m的人出列
People *q = NULL;
People *p = NULL;
People *x = NULL;
People *k = NULL;
int i;
p = L; //p指向当前结点,q指向p的前驱
while(p->next != p) //当链表只剩下一个结点,即报数剩下一个人的时候结束。
{
for(i = 1;i < m; i++)
{ //for循环实现功能:使p指向要出列的结点,q指向p的前驱
q=p;
p=p->next;
}
printf("%d\n",p->number);
q->next = p->next; //将要出列的结点从链表“剔除”
free(p);
p = q->next; //p指向出列的下一个结点,重新开始报数
}
printf("%d\n",q->number); //输出最后一个人
free(q);
/*-----------------测试地址---------------------*/
/* x = (int *)malloc(2*sizeof(int));
printf("x:%x\n",x);
k = (double *)malloc(sizeof(double));
printf("k:%x\n",k);
L = k;
printf("choosing:%x\n",L);
*/
}
void main()
{
People *L = NULL;
printf("first locatin:%x\n",L);
L = Create_List(L); //transport the address
printf("give value:%x\n",L);
printf("RETURN:%x\n",L);
printf("after create:%x\n",L);
Print_List(L); //形参 People *L 实参L 结论 打印不会影响到L的地址值
printf("after printing:%x\n",L);
printf("-----------------------出圈顺序-----------------------\n");
List_kind(L);
printf("at last:%x\n",L);
getchar();
}