SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdio.h> | |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | ||
5 | ||
6 | typedef struct node { | |
7 | struct node *next; | |
8 | int id; | |
9 | ||
10 | char title[64]; | |
11 | } node_t; | |
12 | ||
13 | int listAdd(node_t **, char *); | |
14 | int listSwapWithNext(node_t **, size_t); | |
15 | int listSort(node_t **); | |
16 | int listPrint(node_t **); | |
17 | ||
18 | int main() | |
19 | { | |
20 | node_t *lista = NULL; | |
21 | ||
22 | listAdd(&lista, "abcde"); | |
23 | listAdd(&lista, "abcd"); | |
24 | listAdd(&lista, "abc"); | |
25 | listAdd(&lista, "ab"); | |
26 | listAdd(&lista, "a"); | |
27 | ||
28 | listPrint(&lista); | |
29 | listSort(&lista); | |
30 | listPrint(&lista); | |
31 | ||
32 | return 0; | |
33 | } | |
34 | ||
35 | int listAdd(node_t **_list, char *_title) | |
36 | { | |
37 | node_t *pointer = *_list; | |
38 | node_t *newNode; | |
39 | ||
40 | newNode = malloc(sizeof(node_t)); | |
41 | newNode->next = NULL; | |
42 | newNode->id = 0; | |
43 | ||
44 | strcpy(newNode->title, _title); | |
45 | ||
46 | if(pointer) | |
47 | { | |
48 | while(pointer->next) | |
49 | pointer = pointer->next; | |
50 | ||
51 | newNode->id = pointer->id + 1; | |
52 | pointer->next = newNode; | |
53 | } | |
54 | else *_list = newNode; | |
55 | ||
56 | return 0; | |
57 | } | |
58 | ||
59 | int listSwapWithNext(node_t **_list, size_t first) | |
60 | { | |
61 | node_t *pointer = *_list; | |
62 | node_t *ptr_b; | |
63 | node_t *ptr_c; | |
64 | node_t *ptr_d; | |
65 | ||
66 | if(!first) | |
67 | ptr_b = pointer; | |
68 | ||
69 | for(size_t i = 0; pointer->next->next; i++, pointer = pointer->next) | |
70 | { | |
71 | if(i == first - 1 || first == 0) | |
72 | { | |
73 | if(first) | |
74 | ptr_b = pointer->next; | |
75 | ||
76 | ptr_c = ptr_b->next; | |
77 | ptr_d = ptr_c->next; | |
78 | ||
79 | if(first) | |
80 | pointer->next = ptr_c; | |
81 | else | |
82 | *_list = ptr_c; | |
83 | ||
84 | ptr_c->next = ptr_b; | |
85 | ptr_b->next = ptr_d; | |
86 | ||
87 | break; | |
88 | } | |
89 | } | |
90 | ||
91 | return 0; | |
92 | } | |
93 | ||
94 | size_t listSize(node_t **_list) | |
95 | { | |
96 | node_t *pointer = *_list; | |
97 | size_t _size; | |
98 | ||
99 | for(_size = 0; pointer; _size++) | |
100 | pointer = pointer->next; | |
101 | ||
102 | return _size; | |
103 | } | |
104 | ||
105 | int listSort(node_t ** _list) | |
106 | { | |
107 | int size = listSize(_list); | |
108 | int i; | |
109 | for(i = 0; i < size; i++) | |
110 | { | |
111 | node_t *pointer = *_list; | |
112 | int j; | |
113 | - | for(j = 0 ; j < size - 1 && pointer->next; j++) // here's additional protect condition!!! |
113 | + | for(j = 0 ; j < size - 1 /* && pointer->next*/; j++) // here's without additional protect condition!!! |
114 | { | |
115 | if(strcmp(pointer->title, pointer->next->title) > 0) | |
116 | listSwapWithNext(_list, j); | |
117 | ||
118 | printf("# %d %d\n", i, j); | |
119 | pointer = pointer->next; | |
120 | } | |
121 | printf("\n"); | |
122 | } | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
127 | int listPrint(node_t **_list) | |
128 | { | |
129 | node_t *pointer = *_list; | |
130 | ||
131 | for(int i = 0; pointer != NULL; i++) | |
132 | { | |
133 | printf("%d.\t%s\t%#p\t%#p\n", i, pointer->title, pointer, pointer->next); | |
134 | pointer = pointer->next; | |
135 | } | |
136 | ||
137 | return 0; | |
138 | } |