-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
188 lines (166 loc) · 3.59 KB
/
queue.c
File metadata and controls
188 lines (166 loc) · 3.59 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define true 1
#define false 0
typedef struct node {
struct node* next;
int val;
}Node;
typedef struct queue {
Node* head;
Node* tail;
}Queue;
typedef struct radixQ {
Queue* index[10];
}RadixQ;
RadixQ* radixQ();
Queue* listFromRadixQ(RadixQ* rq, Queue *q);
Queue* queue();
Queue* randList(int count);
Queue* sort(Queue* q);
Node* node(int val);
Node* pop(Queue* q);
int append(Queue* q, Node* newNode);
void printQ(Queue* q);
void interface();
void freeradixq(RadixQ* rq);
void freeQueue(Queue* q);
int main(void)
{
interface();
}
void interface()
{
int n,m;
Queue *q;
printf("Input Your n,m : ");
scanf("%d %d",&n, &m);
getchar();
for (int i = 0 ; i < m ; i++) {
printf("===================[%d] random sort simulater ====================\n\n", i+1);
q = randList(n);
printf("prev set : \n");
printQ(q);
printf("sort set : \n");
q = sort(q);
printQ(q);
freeQueue(q);
}
}
void printQ(Queue* q)
{
int sortText = 0, sortFlag = 10;
for (Node* s = q->head->next ; s->val != -1 ; s = s->next, sortText++) {
printf("%-5d ", s->val);
if (sortText % sortFlag == sortFlag-1)
printf("\n");
}
printf("\n");
return;
}
void freeQueue(Queue* q)
{
Node* checkN;
while((checkN = pop(q)))
free(checkN);
free(q->head);
free(q->tail);
free(q);
}
void freeradixq(RadixQ* rq)
{
Node* checkN;
for (int i = 0 ; i < 10 ; i++)
freeQueue(rq->index[i]);
free(rq);
}
Queue* randList(int count)
{
static int upcount;
srand((unsigned int)(time(NULL) + upcount++));
Queue* q = queue();
for (int i = 0 ; i < count ; i++) {
int val = rand() % 10000;
if(!append(q, node(val)))
i--;
}
return q;
}
Queue* sort(Queue* q)
{
RadixQ* rq = radixQ();
Node* popNode;
int baseCounter = 1;
for (int i = 0 ; i < 4 ; i++) {
while ((popNode = pop(q))) {
int idx = (popNode->val / baseCounter) % 10;
append(rq->index[idx], popNode);
}
q = listFromRadixQ(rq, q);
baseCounter *= 10;
}
freeradixq(rq);
return q;
}
Queue* listFromRadixQ(RadixQ* rq, Queue* q)
{
if (rq == NULL)
return NULL;
if (q == NULL)
q = queue();
Node* tmp;
for (int i = 0 ; i < 10 ; i++) {
while((tmp = pop(rq->index[i])))
append(q, tmp);
}
return q;
}
RadixQ* radixQ()
{
RadixQ* newRq = (RadixQ*)calloc(1, sizeof(RadixQ));
for (int i = 0 ; i < 10 ; i++)
newRq->index[i] = queue();
return newRq;
}
Node* pop(Queue* q)
{
if (!q || !q->head || q->head->next->val == -1) {
if (q->head->next)
q->tail = q->head->next;
return NULL;
}
Node* retNode = q->head->next;
q->head->next = retNode->next;
retNode->next = NULL;
return retNode;
}
int append(Queue* q, Node* newNode)
{
if (!q || !q->tail || !newNode)
return false;
if (q->tail->val == -1) {
q->head->next = newNode;
newNode->next = q->tail;
q->tail = newNode;
}
else {
newNode->next = q->tail->next;
q->tail->next = newNode;
q->tail = newNode;
}
return true;
}
Node* node(int val)
{
Node* newNode = (Node*)calloc(1, sizeof(Node));
newNode->val = val;
return newNode;
}
Queue* queue()
{
Queue* nq = (Queue*)calloc(1, sizeof(Queue));
nq->head = node(-1), nq->tail = node(-1);
nq->head->next = nq->tail;
return nq;
}