HuntnGather – Blame information for rev 46
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
46 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) 2021 Wizardry and Steamworks - License: MIT // |
||
3 | /////////////////////////////////////////////////////////////////////////// |
||
4 | // // |
||
5 | // stack // |
||
6 | // // |
||
7 | // An implementation of a stack. // |
||
8 | // Implemented functions: // |
||
9 | // - push // |
||
10 | // - pop // |
||
11 | // - is empty // |
||
12 | // - count // |
||
13 | // - size // |
||
14 | // - print // |
||
15 | // // |
||
16 | /////////////////////////////////////////////////////////////////////////// |
||
17 | |||
18 | #include <stdio.h> |
||
19 | #include <stdlib.h> |
||
20 | #include <string.h> |
||
21 | #include <dirent.h> |
||
22 | |||
23 | #include <sys/types.h> |
||
24 | #include <sys/stat.h> |
||
25 | |||
26 | #include "stack.h" |
||
27 | |||
28 | /* |
||
29 | * Creates a new stack with a given size. |
||
30 | */ |
||
31 | stack* stackCreate(unsigned int size) { |
||
32 | stack *s; |
||
33 | s = malloc(sizeof(*s)); |
||
34 | if ((s->store = malloc(size * sizeof(*s->store))) == NULL) { |
||
35 | free(s); |
||
36 | s = NULL; |
||
37 | return NULL; |
||
38 | } |
||
39 | s->size = size; |
||
40 | s->top = 0; |
||
41 | return s; |
||
42 | } |
||
43 | |||
44 | /* |
||
45 | * Clears a stack and returns a pointer to a new empty stack. |
||
46 | */ |
||
47 | stack* stackClear(stack *s) { |
||
48 | if (s != NULL) { |
||
49 | free(s); |
||
50 | s = NULL; |
||
51 | } |
||
52 | return stackCreate(1); |
||
53 | } |
||
54 | |||
55 | /* |
||
56 | * Pushes an element onto the stack. |
||
57 | */ |
||
58 | void stackPush(stack *s, void *e, unsigned int size) { |
||
59 | if (s->top > s->size - 1) { |
||
60 | s->store = realloc(s->store, ++s->size * sizeof(*s->store)); |
||
61 | } |
||
62 | s->store[s->top] = malloc(1 * sizeof(*s->store[s->top])); |
||
63 | s->store[s->top]->data = malloc(size * sizeof(*s->store[s->top]->data)); |
||
64 | s->store[s->top]->size = size; |
||
65 | memcpy(s->store[s->top]->data, e, size); |
||
66 | ++s->top; |
||
67 | } |
||
68 | |||
69 | /* |
||
70 | * Pops an element off the stack or returns NULL in case the |
||
71 | * stack is empty. |
||
72 | */ |
||
73 | void *stackPop(stack *s) { |
||
74 | void *e; |
||
75 | if (stackIsEmpty(s)) { |
||
76 | return NULL; |
||
77 | } |
||
78 | --s->top; |
||
79 | e = malloc(s->store[s->top]->size); |
||
80 | memcpy(e, s->store[s->top]->data, s->store[s->top]->size); |
||
81 | free(s->store[s->top]->data); |
||
82 | free(s->store[s->top]); |
||
83 | s->store[s->top] = NULL; |
||
84 | return e; |
||
85 | } |
||
86 | |||
87 | /* |
||
88 | * Delete a stack. |
||
89 | */ |
||
90 | void stackDestroy(stack *s) { |
||
91 | stackElement *e; |
||
92 | while(!stackIsEmpty(s)) { |
||
93 | e = stackPop(s); |
||
94 | free(e->data); |
||
95 | free(e); |
||
96 | e = NULL; |
||
97 | } |
||
98 | free(s); |
||
99 | s = NULL; |
||
100 | } |
||
101 |