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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
| #include <stdio.h>
#include <stdlib.h>
typedef unsigned char TYPE_RETURN;
typedef unsigned int STACKED_TYPE;
typedef enum e_return_value {
RETURN_ERROR = 0,
RETURN_OK,
RETURN_STACK_IS_EMPTY
} RETURN_VALUE;
typedef struct s_stack_element {
STACKED_TYPE value;
struct s_stack_element* next;
} Stack_Element;
typedef struct s_one_stack {
Stack_Element* bottom;
Stack_Element* top;
} One_Stack;
#define CLEAN_ALL \
printf("\n***** Empty *****\n"); \
\
stack_empty(&one_stack); \
\
printf("\n***** Destroy *****\n"); \
\
stack_destroy(&another_stack);
#define TEST_RETURN(ret, msg, value) \
if (ret == RETURN_OK) { \
printf("%s %d - OK\n", msg, value); \
} else if (ret == RETURN_ERROR) { \
printf("%s - ERROR\n", msg); \
\
CLEAN_ALL \
\
return 1; \
} else if (ret == RETURN_STACK_IS_EMPTY) { \
printf("%s - stack is empty\n", msg); \
}
#define stack_init(one_stack) \
one_stack.bottom = NULL; \
one_stack.top = NULL;
// Private macros
#define STACK_HAS_AT_LEAST_2_ELEMENTS(one_stack) ((one_stack.top != one_stack.bottom) && (one_stack.bottom != NULL))
//#define STACK_HAS_ONLY_ONE_ELEMENT(one_stack) ((one_stack.top == one_stack.bottom) && (one_stack.bottom != NULL))
#define STACK_IS_EMPTY(one_stack) (one_stack.top == NULL)
//#define STACK_IS_EMPTY(one_stack) ((one_stack.top == NULL) && (one_stack.bottom == NULL))
#define STACK_IS_NOT_EMPTY(one_stack) ((one_stack.top != NULL) && (one_stack.bottom != NULL))
TYPE_RETURN stack_create_init(One_Stack** one_stack) {
TYPE_RETURN ret;
if (one_stack != NULL) {
One_Stack* new_stack = (One_Stack*) malloc( sizeof(One_Stack) );
if (new_stack != NULL) {
stack_init( (*new_stack) )
(*one_stack) = new_stack;
ret = RETURN_OK;
} else {
(*one_stack) = NULL;
ret = RETURN_ERROR;
}
} else {
ret = RETURN_ERROR;
}
return ret;
}
// XXX : synchronize function stack_destroy and function stack_empty
TYPE_RETURN stack_destroy(One_Stack** one_stack) {
TYPE_RETURN ret;
if (one_stack != NULL) {
if ((*one_stack) != NULL) {
Stack_Element* element = (*one_stack)->top;
Stack_Element* next;
while (element != NULL) {
if (element != (*one_stack)->bottom) {
printf("Destroy : %u\n", element->value);
} else {
printf("Destroy : %u <- bottom\n", element->value);
}
next = element->next;
free(element);
element = next;
}
free( (*one_stack) );
(*one_stack) = NULL;
ret = RETURN_OK;
} else {
ret = RETURN_ERROR;
}
} else {
ret = RETURN_ERROR;
}
return ret;
}
TYPE_RETURN stack_divide_every_other_value(One_Stack* one_stack, One_Stack* another_stack) {
TYPE_RETURN ret;
if ((one_stack != NULL) && (another_stack != NULL)) {
if (one_stack->top != NULL) {
Stack_Element* element = one_stack->top;
Stack_Element* next = element->next;
Stack_Element* new_bottom = element;
Stack_Element* new_top = element;
another_stack->bottom = next;
another_stack->top = next;
if (next != NULL) {
next = next->next; // Start at the third element
while (next != NULL) {
element = next;
next = element->next;
element->next = new_top;
new_top = element;
if (next != NULL) {
element = next;
next = element->next;
element->next = another_stack->top;
another_stack->top = element;
}
}
// XXX : Here because another_stack->bottom can be NULL
// XXX : At last in order to not break links
another_stack->bottom->next = NULL;
}
// XXX : At last in order to not break links
new_bottom->next = NULL;
one_stack->bottom = new_bottom;
one_stack->top = new_top;
ret = RETURN_OK;
} else {
another_stack->bottom = NULL;
another_stack->top = NULL;
ret = RETURN_STACK_IS_EMPTY;
}
} else {
ret = RETURN_ERROR;
}
return ret;
}
// XXX : synchronize function stack_destroy and function stack_empty
void stack_empty(One_Stack* one_stack) {
if (one_stack != NULL) {
Stack_Element* element = one_stack->top;
Stack_Element* next;
while (element != NULL) {
if (element != one_stack->bottom) {
printf("Empty : %u\n", element->value);
} else {
printf("Empty : %u <- bottom\n", element->value);
}
next = element->next;
free(element);
element = next;
}
}
}
TYPE_RETURN stack_pop(One_Stack* one_stack, STACKED_TYPE* value) {
TYPE_RETURN ret;
if ((one_stack != NULL) && (value != NULL)) {
if (one_stack->top != NULL) {
Stack_Element* element = one_stack->top;
Stack_Element* next;
(*value) = element->value;
next = element->next;
free(element);
one_stack->top = next;
if (one_stack->top == NULL) { one_stack->bottom = NULL; }
ret = RETURN_OK;
} else {
ret = RETURN_STACK_IS_EMPTY;
}
} else {
ret = RETURN_ERROR;
}
return ret;
}
void stack_print(One_Stack* one_stack) {
if (one_stack != NULL) {
Stack_Element* element = one_stack->top;
if (element != NULL) {
while (element != NULL) {
if (element != one_stack->bottom) {
printf("Value : %u\n", element->value);
} else {
printf("Value : %u <- bottom\n", element->value);
}
element = element->next;
}
} else {
printf("Stack is empty - %s", ((element == one_stack->bottom)? "OK": "top != bottom"));
}
}
}
TYPE_RETURN stack_push(One_Stack* one_stack, STACKED_TYPE value) {
TYPE_RETURN ret;
if (one_stack != NULL) {
Stack_Element* new_element = (Stack_Element*) malloc( sizeof(Stack_Element) );
if (new_element != NULL) {
if ( STACK_IS_NOT_EMPTY( (*one_stack) ) ) {
new_element->value = value;
new_element->next = one_stack->top;
one_stack->top = new_element;
} else {
new_element->value = value;
new_element->next = NULL;
one_stack->bottom = new_element;
one_stack->top = new_element;
}
ret = RETURN_OK;
} else {
ret = RETURN_ERROR;
}
} else {
ret = RETURN_ERROR;
}
return ret;
}
// XXX : The top most element should be the lowest value
// XXX : Because the lowest value is popped to be "pushed"
// XXX : To avoid to reverse the stack at the end, the value is pushed at the bottom
TYPE_RETURN stack_sort_fusion1(One_Stack* one_stack) {
TYPE_RETURN ret;
if ( STACK_IS_NOT_EMPTY( (*one_stack) ) ) {
if ( STACK_HAS_AT_LEAST_2_ELEMENTS( (*one_stack) ) ) {
One_Stack another_stack;
// printf("\n***************\n");
// printf("***** Stack *****\n"); stack_print(one_stack);
stack_init(another_stack);
ret = stack_divide_every_other_value(one_stack, &another_stack);
if ((ret == RETURN_OK) && STACK_HAS_AT_LEAST_2_ELEMENTS( (*one_stack) )) { ret = stack_sort_fusion1(one_stack); }
if ((ret == RETURN_OK) && STACK_HAS_AT_LEAST_2_ELEMENTS(another_stack)) { ret = stack_sort_fusion1(&another_stack); }
// Fusion
if (ret == RETURN_OK) {
if ( STACK_IS_EMPTY( (*one_stack) ) ) { ret = RETURN_ERROR; }
}
if (ret == RETURN_OK) {
if ( STACK_IS_EMPTY(another_stack) ) { ret = RETURN_ERROR; }
}
if (ret == RETURN_OK) {
Stack_Element* current_top1;
Stack_Element* current_top2;
Stack_Element* new_bottom;
Stack_Element* new_top;
// printf("\n***** Stack 1 *****\n"); stack_print(one_stack);
// printf("\n***** Stack 2 *****\n"); stack_print(&another_stack);
// printf("\n***** *****\n");
current_top1 = one_stack->top;
current_top2 = another_stack.top;
// First : the lowest top is pushed
if (current_top1->value > current_top2->value) {
new_bottom = current_top2;
new_top = current_top2;
current_top2 = current_top2->next;
} else {
new_bottom = current_top1;
new_top = current_top1;
current_top1 = current_top1->next;
}
// Second : the lowest value is pushed
while ((current_top1 != NULL) && (current_top2 != NULL)) {
// printf("\n***** *****\n");
if (current_top1->value > current_top2->value) {
// printf("Pop 2 : %d\n", current_top2->value);
new_bottom->next = current_top2;
new_bottom = current_top2;
current_top2 = current_top2->next;
} else {
// printf("Pop 1 : %d\n", current_top1->value);
new_bottom->next = current_top1;
new_bottom = current_top1;
current_top1 = current_top1->next;
}
}
// Third : One stack is not empty : push the rest of the stack
if ((current_top1 == NULL) && (current_top2 != NULL)) {
new_bottom->next = current_top2;
one_stack->bottom = another_stack.bottom;
one_stack->top = new_top;
ret = RETURN_OK;
} else if ((current_top1 != NULL) && (current_top2 == NULL)) {
new_bottom->next = current_top1;
one_stack->bottom = one_stack->bottom;
one_stack->top = new_top;
ret = RETURN_OK;
} else {
ret = RETURN_ERROR;
}
}
} else {
// Stack is sorted
ret = RETURN_OK;
}
} else {
ret = RETURN_ERROR;
}
return ret;
}
TYPE_RETURN stack_sort_fusion(One_Stack* one_stack) {
TYPE_RETURN ret;
if (one_stack != NULL) {
if ( STACK_IS_NOT_EMPTY( (*one_stack) ) ) {
ret = stack_sort_fusion1(one_stack);
if ((ret == RETURN_OK) && ( STACK_HAS_AT_LEAST_2_ELEMENTS( (*one_stack) ) )) {
// Reverse the stack
Stack_Element* current;
Stack_Element* next;
Stack_Element* new_bottom;
Stack_Element* new_top;
new_bottom = one_stack->top;
new_top = new_bottom;
current = new_top->next;
while (current != NULL) {
next = current->next;
current->next = new_top;
new_top = current;
current = next;
}
new_bottom->next = NULL;
one_stack->bottom = new_bottom;
one_stack->top = new_top;
}
} else {
// Nothing to do
ret = RETURN_OK;
}
} else {
ret = RETURN_ERROR;
}
return ret;
}
int main(int argc, char** argv)
{
One_Stack one_stack;
One_Stack* another_stack = NULL;
STACKED_TYPE value;
TYPE_RETURN ret;
stack_init(one_stack);
ret = stack_create_init(&another_stack); TEST_RETURN(ret, "main : create init", 66)
printf("***** Init *****\n");
ret = stack_push(&one_stack, 94); TEST_RETURN(ret, "main : push", 94)
ret = stack_push(&one_stack, 16); TEST_RETURN(ret, "main : push", 16)
ret = stack_push(&one_stack, 47); TEST_RETURN(ret, "main : push", 47)
ret = stack_push(&one_stack, 63); TEST_RETURN(ret, "main : push", 63)
ret = stack_push(&one_stack, 15); TEST_RETURN(ret, "main : push", 15)
ret = stack_push(&one_stack, 1); TEST_RETURN(ret, "main : push", 1)
// ret = stack_push(&one_stack, 58); TEST_RETURN(ret, "main : push", 58)
// ret = stack_push(&one_stack, 33); TEST_RETURN(ret, "main : push", 33)
stack_print(&one_stack);
// printf("\n***** 3 pops *****\n");
//
// ret = stack_pop(one_stack, &value); TEST_RETURN(ret, "main : pop", value)
// ret = stack_pop(one_stack, &value); TEST_RETURN(ret, "main : pop", value)
// ret = stack_pop(one_stack, &value); TEST_RETURN(ret, "main : pop", value)
//
// stack_print(&one_stack);
printf("\n***** sort fusion *****\n");
ret = stack_sort_fusion(&one_stack); TEST_RETURN(ret, "main : sort fusion", 67)
stack_print(&one_stack);
// printf("\n***** divide every other value *****\n");
//
// ret = stack_divide_every_other_value(&one_stack, another_stack); TEST_RETURN(ret, "main : divide every other value", 67)
//
// stack_print(&one_stack);
//
// printf("\n***** *****\n");
//
// stack_print(another_stack);
// printf("\n***** 4 pops *****\n");
//
// ret = stack_pop(one_stack, &value); TEST_RETURN(ret, "main : pop", value)
// ret = stack_pop(one_stack, &value); TEST_RETURN(ret, "main : pop", value)
// ret = stack_pop(one_stack, &value); TEST_RETURN(ret, "main : pop", value)
// ret = stack_pop(one_stack, &value); TEST_RETURN(ret, "main : pop", value)
//
// stack_print(&one_stack);
//
// printf("\n***** 2 pops *****\n");
//
// ret = stack_pop(one_stack, &value); TEST_RETURN(ret, "main : pop", value)
// ret = stack_pop(one_stack, &value); TEST_RETURN(ret, "main : pop", value)
//
// stack_print(&one_stack);
CLEAN_ALL
return 0;
} |