Created
May 5, 2026 04:32
-
-
Save mentix02/81a5a697ef2fbfa200884d0a9799bb6f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdint.h> | |
| #include <stddef.h> | |
| #include <assert.h> | |
| #include <stdbool.h> | |
| #include <inttypes.h> | |
| #define CAP_GROWTH_RATE 2 | |
| typedef struct Stack { | |
| size_t len; | |
| size_t cap; | |
| int64_t * data; | |
| } cstack_t; | |
| cstack_t * newStack(const size_t cap) { | |
| assert(cap > 0); | |
| cstack_t * stack = malloc(sizeof(cstack_t)); | |
| stack->len = 0; | |
| stack->cap = cap; | |
| stack->data = malloc(sizeof(int64_t) * cap); | |
| return stack; | |
| } | |
| void cleanStack(cstack_t * stack) { | |
| free(stack->data); | |
| free(stack); | |
| } | |
| int64_t safePop(cstack_t * stack) { | |
| assert(stack != NULL); | |
| assert(stack->len != 0); | |
| return stack->data[--(stack->len)]; | |
| } | |
| inline static bool isFull(cstack_t * stack) { | |
| return stack->len == stack->cap; | |
| } | |
| inline static bool isEmpty(cstack_t * stack) { | |
| return stack == NULL || stack->len == 0; | |
| } | |
| static bool resizeStack(cstack_t * stack) { | |
| stack->cap *= CAP_GROWTH_RATE; | |
| int64_t * newData = realloc(stack->data, stack->cap * sizeof(int64_t)); | |
| if (newData == NULL) { | |
| fprintf(stderr, "failed to resize stack [%p]\n", (void*) stack); | |
| return false; | |
| } else { | |
| stack->data = newData; | |
| return true; | |
| } | |
| } | |
| void safePush(cstack_t * stack, int64_t num) { | |
| assert(stack != NULL); | |
| if (isFull(stack)) assert(resizeStack(stack)); | |
| stack->data[stack->len++] = num; | |
| } | |
| void printStack(cstack_t * stack) { | |
| if (isEmpty(stack)) | |
| printf("[ ] (%p)\n", (void*) stack->data); | |
| else { | |
| printf("[ "); | |
| for (size_t i = 0; i < stack->len - 1; ++i) | |
| printf("%" PRId64 ", ", stack->data[i]); | |
| printf("%" PRId64 " ] (%p)\n", stack->data[stack->len - 1], (void*) stack->data); | |
| } | |
| } | |
| int main(void) { | |
| cstack_t * stack = newStack(2); | |
| printStack(stack); | |
| safePush(stack, 3); | |
| safePush(stack, 5); | |
| printStack(stack); | |
| safePush(stack, 8); | |
| printStack(stack); | |
| safePush(stack, 12); | |
| safePush(stack, 15); | |
| printStack(stack); | |
| safePush(stack, 18); | |
| safePush(stack, 20); | |
| printStack(stack); | |
| safePush(stack, 22); | |
| safePush(stack, 25); | |
| printStack(stack); | |
| safePush(stack, 28); | |
| safePush(stack, 30); | |
| safePush(stack, 31); | |
| printStack(stack); | |
| safePush(stack, 33); | |
| safePush(stack, 35); | |
| safePush(stack, 38); | |
| printStack(stack); | |
| cleanStack(stack); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment