-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_stack.c
60 lines (50 loc) · 1.21 KB
/
app_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
60
/***************************************************************************//**
* @file app_stack.c
* @brief Implementation file of a stack with integer elements.
* @author Georgios Apostolakis
******************************************************************************/
#include "app_stack.h"
#include "app_log.h"
///The space where the objects of the stack will be stored.
static int stack[STACK_MAXSIZE];
///This variable points at the top of the stack.
static int top = -1;
int peek() {
if(!isempty())
return stack[top];
app_log_error("Error. The stack is empty.\n");
return -1;
}
int pop() {
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
}
app_log_error("Error. The stack is empty.\n");
return -1;
}
void push(int data) {
if(!isfull()) {
top = top + 1;
stack[top] = data;
}
else
app_log_error("Could not insert data because stack is full. Increase the capacity in 'stack.h' file and the problem will be fixed.\n");
}
void clear(){
top = -1;
}
bool isempty() {
if(top == -1)
return true;
else
return false;
}
bool isfull() {
if(top == STACK_MAXSIZE)
return true;
else
return false;
}