-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask14.c
78 lines (68 loc) · 1.28 KB
/
task14.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
} node;
void sort(node **book) {
node *prev, *curr, *tmp;
int swapped;
do {
swapped = 0;
for (prev = NULL, curr = *book; curr != NULL && curr->next != NULL; prev = curr, curr = curr->next) {
if (curr->val < curr->next->val) {
tmp = curr->next->next;
curr->next->next = curr;
if (curr == *book) {
*book = curr->next;
}
if (prev != NULL) {
prev->next = curr->next;
}
curr->next = tmp;
swapped = 1;
}
}
} while (swapped);
}
int main() {
node *book = NULL, *curr, *prev;
int i, arg;
do {
scanf("%d", &i);
switch(i) {
case 1:
scanf("%d", &arg);
curr = book;
book = malloc(sizeof(node));
book->val = arg;
book->next = curr;
break;
case 2:
scanf("%d", &arg);
for (prev = NULL, curr = book; curr != NULL; prev = curr, curr = curr->next) {
if (arg == curr->val) {
if (prev != NULL) {
prev->next = curr->next;
} else {
book = curr->next;
}
free(curr);
break;
}
}
break;
case 4:
sort(&book);
break;
case 5:
for (curr = book; curr != NULL; curr = curr->next) {
printf("%d\n", curr->val);
}
break;
default:
break;
}
} while (i != 6);
return 0;
}