-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
52 lines (45 loc) · 1.46 KB
/
stack.c
File metadata and controls
52 lines (45 loc) · 1.46 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
/*********************** Includes *********************************/
#include "stack.h"
#include <stdlib.h>
/*********************** Macros *********************************/
/*********************** Defines *********************************/
/*********************** Typedefs *********************************/
/*********************** Variables *********************************/
/*********************** Functions *********************************/
void init(struct stack_t **stack) {
*stack = (struct stack_t*)malloc(sizeof(struct stack_t));
if(*stack == NULL) {
return; //mem error
}
(*stack)->index = -1;
for (int i = 0; i < STACK_SIZE; ++i) {
(*stack)->buffer[i] = 0;
}
}
int pop(struct stack_t *stack) {
if(stack == NULL) {
return 0; // if stack is not init
}
if(stack->index == -1) {
return 0; //stack is empty
}
int result = stack->buffer[stack->index];
stack->buffer[stack->index] = 0;
stack->index--;
return result;
}
void push(struct stack_t *stack, int data) {
if(stack == NULL) {
return; // if stack is not init
}
if(stack->index == STACK_SIZE - 1){
return;
}
stack->buffer[++stack->index] = data;
}
int getStackSize(struct stack_t *stack) {
return (stack->index + 1);
}
bool isEmpty(struct stack_t *stack){
return stack->index == -1 ? true : false;
}