forked from Inverted/tasbot_eyes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
59 lines (48 loc) · 1.09 KB
/
stack.c
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
#include <stdio.h>
#include "stack.h"
#include "arguments.h"
int top = -1;
void* stack[MAX_SIZE];
bool isEmpty() {
if (top == -1) {
return true;
}
return false;
}
bool isFull() {
if (top == MAX_SIZE) {
return 1;
}
return 0;
}
void* peek() {
if (!isEmpty()) {
return stack[top];
}
printf("[ERROR] Could not retrieve data. Stack is empty.\n");
return NULL; //TODO: Check if returned item is not NULL
}
void* pop() {
if (!isEmpty()) {
void* item = stack[top];
top--;
if (verbose){
printf("[INFO] Stack size is now %d\n", top);
}
return item;
}
printf("[ERROR] Could not retrieve data. Stack is empty.\n");
return NULL; //TODO: Check if returned item is not NULL
}
bool push(void* _item) {
if (!isFull()) {
top++;
if (verbose){
printf("[INFO] Stack size is now %d\n", top);
}
stack[top] = _item; //not the same object
return true;
}
printf("[ERROR] Could not insert data. Stack is full.\n");
return false;
}