-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0516043_hw7-2.c
53 lines (52 loc) · 1.06 KB
/
0516043_hw7-2.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
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int n;
struct Node *next;
}node;
node b[200];
int bi = 0;
void in(node* fr, int k) {
b[bi].n = k;
b[bi].next = NULL;
node *n = b[bi++];
n->next = fr->next;
fr->next = n;
fr = n;
}
void swap(node* i) {
node *n1 = i->next;
node *n2 = n1->next;
node *n3 = n2->next;
// i -> n1 -> n2 -> n3
i->next = n2;
n2->next = n1;
n1->next = n3;
// i -> n2 -> n1 -> n3
}
void prin(node *b) {
while (b != NULL) {
printf("%d ",b->n);
b = b->next;
}
}
int main() {
b[bi].n = -1;
b[bi].next = NULL;
node *head = b[bi++];
node *tail = head;
int a=48;
char c;
while (scanf("%d", &a) == 1)
in(tail, a);
scanf("%c", &c);
printf("Output:");
for (node *j = head->next ; j != NULL ; j = j->next)
for(node *i = head ; i->next->next != NULL ; i = i->next)
if (i->next->n < i->next->next->n)
swap(i);
prin(head->next);
printf("\n");
system("pause");
return 0;
}