前言

学校布置的单链表上机作业,代码质量太低了就不push到Github上了,正文就是完整代码。
有能力的同学给我Github点个Star,非常感谢!

因为本人C语言学得比C++好,所以主要是用C实现,用到了少量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
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<iostream>
typedef struct LNode{ //声明
int data;
struct LNode* next;
} *LinkList;
bool InitList(LinkList& L)//定义有头节点的单链表
{
L = (LNode*)malloc(sizeof(LNode));
if (L == NULL)
return false;
L->next = NULL;
return true;
}
LinkList List_TailInsert(LinkList& L)//尾插法添加数据(有头节点)
{
int x;
LNode* s=NULL;LNode* p = L;
scanf("%d", &x);
while (x != -9999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
p->next = s;
p = s;
scanf("%d", &x);
}
p->next = NULL;
return L;
}
int List_Value_Destroy(LinkList& L,int value)///按值删除
{
LNode* x = L, *y = L->next;
int z = 0;
while (y!= NULL)
{
if (y->data == value)
{
x->next = y->next;
free(y);
y = x->next;
z = 1;
}
else
{
x = y;
y = y->next;
}
}
return z;
}
int List_Even_Statistic(LinkList &L)//单链表偶数个数
{
LNode* x = L->next;
int count = 0;
while (x != NULL)
{
if (x->data % 2 == 0)
count++;
x = x->next;
}
return count;
}

void List_Separation(LinkList& HeadA, LinkList& HeadB)
{
LNode* p = HeadA;
LNode* q = HeadA->next; // 初始化q为第一个节点
LNode* r = HeadB;
LNode* s = NULL; // 初始化s为NULL

while (q != NULL)
{
if (q->data % 2)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = q->data;
r->next = s;
r = s;
p->next = q->next;
free(q);
q = p->next; // 删除一个节点后更新q以正确移动到下一个节点
}
else
{
p = q;
q = q->next;
}
}
r->next = NULL;
}

bool Print_LinkList(LinkList& L)//打印单链表
{
if (L->next == NULL)
return false;
for (LNode* p = L->next;p != NULL;p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
int main()
{
LinkList L,G;
int x;
if(InitList(L)&&InitList(G))
printf("定义完成,THAT'S ALL ALREADY!\n");
system("pause");
List_TailInsert(L);
printf("请输入想要删除的值");
scanf("%d", &x);
if (List_Value_Destroy(L, x))
printf("Delete Successfully!\n");
else
printf("Failed!\n");
system("pause");
printf("%d\n", List_Even_Statistic(L));
system("pause");
List_Separation(L, G);
Print_LinkList(L);
Print_LinkList(G);
return 0;
}

碎碎念

一个暑假没更新博客,太懈怠了,争取这个学期能写几篇高质量的博客,还有最重要的是把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
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>

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;
}


#include <stdio.h>
#include <stdlib.h>

#define MAX_PEOPLE 100

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;
}