This repository was archived by the owner on Oct 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-stack.h
More file actions
56 lines (50 loc) · 1.77 KB
/
array-stack.h
File metadata and controls
56 lines (50 loc) · 1.77 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
52
53
54
55
56
/// Stack structure
typedef struct array_stack {
int* data; // dynamic array of ints
int capacity; // current maximum amount of data points permitted (ideally an even number)
int last; // index of the stack's top element
int size; // amount of data points stored
} stack;
/**
* @brief Allocates memory and starts an empty stack.
*
* @param capacity initial number of data points supported
* @return the address of the stack created or NULL, if it failed.
*/
stack* salloc(int capacity);
/**
* @brief Changes the array supporting the stack by multipying its capacity by a given factor.
*
* @param s stack being resized
* @param factor ratio between the new capacity and the old one
* @return 1 on success. 0 on failure.
*/
int resize_stack(stack* s, float factor);
/**
* @brief Appends a data point in the array.
* If the darray is full, it tries to double its capacity and then adds the key.
* When resizing the array is not possible, it will not push.
*
* @param s stack in which data is added
* @param key the data inserted
* @return 1 on success. 0 on failure.
*/
int s_push(stack* s, int key);
/**
* @brief Removes, by invalidating its position with the `last` field, a data point from the stack.
* If the darray is a quarter full, it tries to halve its capacity, no matter how small the array already is.
* When resizing the array is not possible, it will pop without changing the capacity.
*
* @return pointer to the data point invalidated. NULL if the stack is empty or null.
*/
int* s_pop(stack* s);
/**
* @brief Releases a whole stack deallocating memory.
*/
void free_stack(stack* s);
/**
* @brief Displays all the contents in a stack (LIFO, of course) separeted by '|'.
*
* @param s stack being printed.
*/
void print_s(stack* s);