Last active
May 30, 2023 21:04
-
-
Save opsJson/c2146ae5149e6d339bfef6a2d30e6e88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LIST_H_ | |
#define LIST_H_ | |
#define LIST(TYPE, SIZE) \ | |
\ | |
static TYPE list_##TYPE[SIZE]; \ | |
static int list_start_##TYPE = 0; \ | |
static int list_end_##TYPE = 0; \ | |
int list_##TYPE_size = SIZE; \ | |
\ | |
int list_push_start_##TYPE(TYPE value) { \ | |
int idx = list_start_##TYPE - 1; \ | |
\ | |
if (idx < 0) { \ | |
idx = SIZE - 1; \ | |
} \ | |
\ | |
if (list_##TYPE[idx]) { \ | |
return 0; \ | |
} \ | |
\ | |
list_##TYPE[idx] = value; \ | |
list_start_##TYPE--; \ | |
\ | |
if (list_start_##TYPE < 0) { \ | |
list_start_##TYPE = SIZE - 1; \ | |
} \ | |
\ | |
return 1; \ | |
} \ | |
\ | |
int list_push_end_##TYPE(TYPE value) { \ | |
if (list_##TYPE[list_end_##TYPE]) { \ | |
return 0; \ | |
} \ | |
\ | |
list_##TYPE[list_end_##TYPE++] = value; \ | |
\ | |
if (list_end_##TYPE >= SIZE) { \ | |
list_end_##TYPE = 0; \ | |
} \ | |
\ | |
return 1; \ | |
} \ | |
\ | |
int list_pop_start_##TYPE(TYPE *value) { \ | |
if (!list_##TYPE[list_start_##TYPE]) { \ | |
return 0; \ | |
} \ | |
\ | |
*value = list_##TYPE[list_start_##TYPE]; \ | |
list_##TYPE[list_start_##TYPE++] = 0; \ | |
\ | |
if (list_start_##TYPE >= SIZE) { \ | |
list_start_##TYPE = 0; \ | |
} \ | |
\ | |
return 1; \ | |
} \ | |
\ | |
int list_pop_end_##TYPE(TYPE *value) { \ | |
int idx = list_end_##TYPE - 1; \ | |
\ | |
if (idx < 0) { \ | |
idx = SIZE - 1; \ | |
} \ | |
\ | |
if (!list_##TYPE[idx]) { \ | |
return 0; \ | |
} \ | |
\ | |
*value = list_##TYPE[idx]; \ | |
list_##TYPE[idx] = 0; \ | |
list_end_##TYPE--; \ | |
\ | |
if (list_end_##TYPE < 0) { \ | |
list_end_##TYPE = SIZE - 1; \ | |
} \ | |
\ | |
return 1; \ | |
} \ | |
\ | |
TYPE list_see_end_##TYPE(void) { \ | |
return list_##TYPE[list_end_##TYPE]; \ | |
} \ | |
\ | |
TYPE list_see_start_##TYPE(void) { \ | |
return list_##TYPE[list_start_##TYPE]; \ | |
} \ | |
#endif | |
/*/////////////////////////////////// | |
Testing: | |
///////////////////////////////////*/ | |
#include <stdio.h> | |
LIST(int, 32) | |
int main() { | |
int x; | |
{ | |
printf("list: [1, 2, 3]\n"); | |
list_push_end_int(1); | |
list_push_end_int(2); | |
list_push_end_int(3); | |
printf("Queue:\n"); | |
list_pop_start_int(&x); | |
printf("%i\n", x); | |
list_pop_start_int(&x); | |
printf("%i\n", x); | |
list_pop_start_int(&x); | |
printf("%i\n", x); | |
} | |
{ | |
printf("\nlist: [1, 2, 3]\n"); | |
list_push_end_int(1); | |
list_push_end_int(2); | |
list_push_end_int(3); | |
printf("Stack:\n"); | |
list_pop_end_int(&x); | |
printf("%i\n", x); | |
list_pop_end_int(&x); | |
printf("%i\n", x); | |
list_pop_end_int(&x); | |
printf("%i\n", x); | |
} | |
{ | |
printf("\nlist: [3, 2, 1]\n"); | |
list_push_start_int(1); | |
list_push_start_int(2); | |
list_push_start_int(3); | |
printf("Revese Stack:\n"); | |
list_pop_start_int(&x); | |
printf("%i\n", x); | |
list_pop_start_int(&x); | |
printf("%i\n", x); | |
list_pop_start_int(&x); | |
printf("%i\n", x); | |
} | |
{ | |
printf("\nlist: [3, 2, 1]\n"); | |
list_push_start_int(1); | |
list_push_start_int(2); | |
list_push_start_int(3); | |
printf("Revese Queue:\n"); | |
list_pop_end_int(&x); | |
printf("%i\n", x); | |
list_pop_end_int(&x); | |
printf("%i\n", x); | |
list_pop_end_int(&x); | |
printf("%i\n", x); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment