-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
46 lines (38 loc) · 1.32 KB
/
stack.h
File metadata and controls
46 lines (38 loc) · 1.32 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
#pragma once
#include <stddef.h>
// `Stack` implements a stack data structure for `void*` types which
// automatically resizes itself (and `err`s if memory allocation fails)
struct Stack {
// `buffer` is a pointer to your `void*` elements that were pushed
// on the stack.
void **buffer;
// `length` is the number of elements currently stored
// in the stack
size_t length;
// `capacity` is size of the buffer `buffer` for the stack
size_t capacity;
};
// `stackInit` initializes an uninitialized stack containing no elements with a
// buffer of the given `capacity`.
//
// Example usage
// ```
// #define DEFAULT_STACK_SIZE 16
// struct Stack myStack;
// stackInit(&myStack, DEFAULT_STACK_SIZE);
// ```
void stackInit(struct Stack *stack, size_t capacity);
// `stackPush` pushes `elem` to the top of `stack`.
void stackPush(struct Stack *stack, void *elem);
// `stackPop` returns and removes the top element from the stack.
//
// Preconditions:
//
// - `stack` must have length > 0.
void *stackPop(struct Stack *stack);
// `stackTop` returns the top element from the stack.
void *stackTop(struct Stack *stack);
void *stackRead(struct Stack *stack, int indexFromTop);
// `stackPrint` prints the pointer addresses of all elements in the stack and
// other related information to stderr
void stackPrint(struct Stack *stack);