单链表的基础运用
前言
学校布置的单链表上机作业,代码质量太低了就不push到Github上了,正文就是完整代码。
有能力的同学给我Github点个Star
,非常感谢!
因为本人C语言学得比C++好,所以主要是用C实现,用到了少量C++特性,例如在函数中&的引用
正文
1 |
|
碎碎念
一个暑假没更新博客,太懈怠了,争取这个学期能写几篇高质量的博客,还有最重要的是把CDN部署一下,网站加载太慢了,还有就是早点把合订本网页版肝出来(已经新建文件夹了咕咕咕)
补档
补第二次上机作业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
void decimalToBaseR(int decimalNum, int R) {
char stack[1000];
int top = -1;
while (decimalNum > 0) {
int remainder = decimalNum % R;
if (remainder < 10) {
stack[++top] = remainder + '0';
}
else if(remainder >= 10&& remainder < 36) {
stack[++top] = remainder - 10 + 'A';
}
else {
stack[++top] = remainder - 36 + 'a';
}
decimalNum /= R;
}
if (top == -1) {
printf("0");
}
else {
printf("进制-%d: ", R);
while (top >= 0) {
printf("%c", stack[top--]);
}
}
printf("\n");
}
int main() {
int decimalNum = 42;
int R = 100;
puts("输入数字以及进制");
scanf("%d%d", &decimalNum, &R);
decimalToBaseR(decimalNum, R);
return 0;
}
typedef struct {
int data[MAX_PEOPLE];
int front, rear;
} CircularQueue;
void initializeQueue(CircularQueue* q) {
q->front = q->rear = -1;
}
int isEmpty(CircularQueue* q) {
return q->front == -1;
}
void enqueue(CircularQueue* q, int item) {
if ((q->rear + 1) % MAX_PEOPLE == q->front) {
printf("Queue is full. Cannot enqueue.\n");
}
else {
if (isEmpty(q)) {
q->front = q->rear = 0;
}
else {
q->rear = (q->rear + 1) % MAX_PEOPLE;
}
q->data[q->rear] = item;
}
}
int dequeue(CircularQueue* q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return -1;
}
else {
int item = q->data[q->front];
if (q->front == q->rear) {
initializeQueue(q);
}
else {
q->front = (q->front + 1) % MAX_PEOPLE;
}
return item;
}
}
void josephus(int n, int k) {
CircularQueue queue;
initializeQueue(&queue);
for (int i = 1; i <= n; i++) {
enqueue(&queue, i);
}
printf("Order:\n");
while (!isEmpty(&queue)) {
for (int i = 1; i < k; i++) {
int item = dequeue(&queue);
enqueue(&queue, item);
}
int removed = dequeue(&queue);
printf("%d ", removed);
}
printf("\n");
}
int main() {
int n, k;
printf("n,k\n");
scanf("%d%d", &n, &k);
josephus(n, k);
return 0;
}