-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
131 lines (121 loc) · 3.37 KB
/
stack.c
File metadata and controls
131 lines (121 loc) · 3.37 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "stack.h"
#include <inttypes.h> // PRIu32
#include <stdint.h> // extended integer library
#include <stdio.h> // Print
#include <stdlib.h> // Malloc
//
// Structure for defining stack data structure
//
// Function taken from Professor Darrell Long (Stacks and Queues 4/19/21)
//
struct Stack {
uint32_t top; // index of the next empty slot.
uint32_t capacity; // number of items that can be pushed.
int64_t *items; // array of items, each with type int64_t.
};
//
// Creates a stack
//
// capacity: max size of the stack
// Function taken from Professor Darrell Long (Stacks and Queues 4/19/21)
//
Stack *stack_create(uint32_t capacity) {
Stack *s = (Stack *) malloc(sizeof(Stack)); // allocate memory for stack structure
if (s) { // if allocation successful
s->top = 0;
s->capacity = capacity;
s->items = (int64_t *) calloc(capacity, sizeof(int64_t)); // Allocate memory for stack items
if (!s->items) { // if no room in memory
free(s);
s = NULL; // Ensure pointer is not pointing to memory no longer used
}
}
return s;
}
//
// Deletes a stack
//
// s: a stack to delete
// Function taken from Professor Darrell Long (Stacks and Queues 4/19/21)
//
void stack_delete(Stack **s) {
if (*s && (*s)->items) { // if stack exists and has items
free((*s)->items);
free(*s);
*s = NULL; // ensure pointer is not pointing to memory no longer used
}
return;
}
//
// Returns if stack is empty or not
//
// s: a stack to check
// Function taken from Professor Darrell Long (Stacks and Queues 4/19/21)
//
bool stack_empty(Stack *s) {
return s->top == 0;
}
//
// Returns if stack is full or not
//
// s: stack to test
//
// Function taken from Professor Darrell Long (Stacks and Queues 4/19/21)
//
bool stack_full(Stack *s) {
return s->top == s->capacity;
}
//
// Returns size of the stack
//
// s: the stack to return the size of
// Function taken from Professor Darrell Long (Stacks and Queues 4/19/21)
//
uint32_t stack_size(Stack *s) {
return (s && s->items) ? s->top : (uint32_t) -1; //if stack exists and has items
}
//
// Adds the given item to the top of the stack and returns if successful
//
// s: stack to push item to
// x: item to add to stack
// Function taken from Professor Darrell Long (Stacks and Queues 4/19/21)
//
bool stack_push(Stack *s, int64_t x) {
if (stack_full(s)) { // can not add to a full stack
return false;
}
s->items[s->top] = x; // top item = x
s->top += 1;
return true;
}
//
// Removes last item on the stack and stores returns the
//
// s: the stack to pop from
// x: the address to store the popped value
// Function taken from Professor Darrell Long (Stacks and Queues 4/19/21)
bool stack_pop(Stack *s, int64_t *x) {
if (stack_empty(s)) { // can not pop from an empty stack
return false;
}
s->top -= 1;
*x = s->items[s->top]; // store top item at given address
return true;
}
//
// Prints elements of the stack
//
// s: the stack to print
// Function taken from teaching assistant Eugene Chou (Lab Section 4/22/21)
//
void stack_print(Stack *s) {
printf("[");
for (uint32_t i = 0; i < s->top; i += 1) { // iterate through item indices
printf("%" PRId64, s->items[i]);
if (i + 1 != s->top) { // do not print comma for last case
printf(", ");
}
}
printf("]\n");
}