-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.c
37 lines (31 loc) · 998 Bytes
/
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
#include "stack.h"
#include <stdlib.h>
Stack* createStack(int capacity) {
Stack* stack = (Stack*) malloc(sizeof(Stack));
stack->capacity = capacity;
stack->items = (BacktrackState*) malloc(capacity * sizeof(BacktrackState));
stack->top_item_index = -1;
return stack;
}
/* TODO: check if the default value is NULL while making malloc and non default value cannot be null */
int isEmpty(Stack* stack) {
return stack->top_item_index == -1;
}
int isFull(Stack* stack)
{
return stack->top_item_index == stack->capacity - 1;
}
void push(Stack* stack, BacktrackState item)
{
if (isFull(stack))
return;
stack->items[++(stack->top_item_index)] = item;
}
BacktrackState pop(Stack* stack)
{
BacktrackState default_state = {-1,-1,0,NULL,0,1,0};
if (isEmpty(stack))
// because it was initilized on the stack, the value could not be used outside
return default_state;
return stack->items[(stack->top_item_index)--];
}