-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
51 lines (39 loc) · 1.62 KB
/
stack.c
File metadata and controls
51 lines (39 loc) · 1.62 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
#include "stack.h"
#include <assert.h>
#include <err.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
// Hashmap Function Provided by Tutorial Section from Jared Pon, which allows Stack Structures to be created in C Code.
void stackInit(struct Stack *stack, size_t capacity) {
stack->length = 0;
stack->capacity = capacity;
stack->buffer = malloc(sizeof(void *) * capacity);
if (stack->buffer == NULL)
err(EXIT_FAILURE, "error in `stackInit` memory allocation failed.");
}
void stackPush(struct Stack *stack, void *elem) {
if (stack->length == stack->capacity) {
size_t newCapacity = 2 * stack->capacity + 1;
stack->buffer = realloc(stack->buffer, sizeof(void *) * newCapacity);
stack->capacity = newCapacity;
if (stack->buffer == NULL)
err(EXIT_FAILURE, "error in `stackPush` memory reallocation failed.");
}
stack->buffer[(stack->length)++] = elem;
}
void *stackPop(struct Stack *stack) {
assert(stack->length > 0);
return stack->buffer[--stack->length];
}
void *stackTop(struct Stack *stack) { return stack->buffer[stack->length - 1]; }
void *stackRead(struct Stack *stack, int indexFromTop) { return stack->buffer[stack->length - 1 - indexFromTop]; }
void stackPrint(struct Stack *stack) {
fprintf(stderr, "Stack:\n");
fprintf(stderr, "\tLength: %zu\n", stack->length);
fprintf(stderr, "\tCapacity: %zu\n", stack->capacity);
fprintf(stderr, "\tElements (highest element is the top of the stack):\n");
// iterate through the stack backwards to the top appears at the top.
for (size_t i = stack->length; i--;)
fprintf(stderr, "\t\t%zu: %p\n", i, stack->buffer[i]);
}