-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrs.c
More file actions
213 lines (188 loc) · 4.12 KB
/
rs.c
File metadata and controls
213 lines (188 loc) · 4.12 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define true 1
#define false 0
typedef struct node {
int val;
struct node *next;
struct node *prev;
}Node;
typedef struct Queue {
Node *tail[10];
Node *head[10];
int total_count;
}Queue;
Queue* queue();
Node* sort(Node* list);
Node* node(int val);
Node* pop(Queue* q, int idx);
Node* allpop(Queue* q);
int append (Queue* q, int idx, Node* newNode);
Node* randSet(int n);
void printNode(Node* n);
void printNode2(Node* n);
int interface();
void freeList(Node* n);
void freeQueue(Queue* q);
int main(void)
{
while(interface());
return 0;
}
void freeList(Node* n)
{
Node *del;
for (Node* s = n ; n != NULL ;) {
del = n;
n = n->next;
free(del);
}
}
void freeQueue(Queue* q)
{
if (q == NULL)
return;
for (int i = 0 ; i < 10 ; i++) {
if (q->head[i])
free(q->head[i]);
if (q->tail[i])
free(q->tail[i]);
}
}
int interface()
{
int d = 0;
printf(" your n size (Want Quit enter -1) : ");
scanf("%d", &d);
getchar();
if (d == -1) {
printf("Quit!!\n");
return false;
}
else if (d < 0) {
printf("Retry your number (%d)\n", d);
return true;
}
Node* n = randSet(d);
printf("Prev Set : \n");
printNode(n);
n = sort(n);
printf("\n");
printf("Sorted Set : \n");
printNode(n);
freeList(n);
printf("===============================\n\n");
return true;
}
void printNode(Node* n)
{
if (n == NULL)
return;
int count = 0;
int printFlag = 10;
for (Node* p = n ; p != NULL && p->val != -1; p = p->next, count++) {
printf("%-5d ", p->val);
if (count % printFlag == printFlag-1)
printf("\n");
}
printf("\n");
}
void printNode2(Node* n)
{
if (n == NULL)
return;
for (Node* p = n->next ; p != NULL; p = p->next) {
printf("%d ", p->val);
}
printf("\n");
}
Node* sort(Node* list)
{
Queue* q = queue();
Node* s=list, *delNode;
int baseCounter = 1;
for (int i = 0 ; i < 4 ; i++) {
while (s != NULL && s->val != -1) {
Node* tmp = s->next;
int iidx = (s->val / baseCounter) % 10;
append(q, iidx, s);
s = tmp;
}
s = allpop(q);
delNode = s;
s = s->next;
free(delNode);
baseCounter *= 10;
}
freeQueue(q);
return s;
}
Node* randSet(int n)
{
srand((unsigned int)time(NULL));
int val;
Node* head = node(-1); //dummy head
Node* ptr = head;
for (int i = 0 ; i < n ; i++) {
val = rand() % 10000;
Node* newNode = node(val);
newNode->next = ptr;
ptr = newNode;
}
return ptr;
}
Node* pop(Queue* q, int idx)
{
if (!q || !q->head[idx] || q->head[idx]->next == q->tail[idx])
return NULL;
Node* retNode = q->head[idx]->next;
q->head[idx]->next = retNode->next;
retNode->next->prev = q->head[idx];
retNode->next = retNode->prev = NULL;
q->total_count--;
return retNode;
}
// list->a1, a2 ... -> NULL
Node* allpop(Queue* q)
{
if (!q)
return NULL;
Node* list = node(-1), *ptr = list, *pNode;
for (int idx = 0 ; idx < 10 ; idx++) {
while ((pNode = pop(q, idx))) {
ptr->next = pNode;
ptr = pNode;
}
}
return list;
}
int append (Queue* q, int idx, Node* newNode) {
if (!q || !q->head[idx])
return false;
newNode->prev = q->tail[idx]->prev;
newNode->next = q->tail[idx];
newNode->prev->next = newNode;
q->tail[idx]->prev = newNode;
q->total_count++;
return true;
}
Node* node(int val) {
Node* newNode = (Node*)calloc(1, sizeof(Node));
newNode->val = val;
return newNode;
}
/** head <-> data, data, data <-> tail*/
Queue* queue()
{
Queue *nq = (Queue*)calloc(1, sizeof(Queue));
for (int i = 0 ; i < 10 ; i++) {
Node* head = node(-1), *tail = node(-1);
head->next = tail;
tail->prev = head;
nq->head[i] = head;
nq->tail[i] = tail;
}
return nq;
}