-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfa_stack.c
More file actions
executable file
·69 lines (56 loc) · 1.75 KB
/
nfa_stack.c
File metadata and controls
executable file
·69 lines (56 loc) · 1.75 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
#include "nfa_stack.h"
/* Initializes a stack. */
NfaStack* init_nfa_stack(unsigned int capacity) {
NfaStack *stack = (NfaStack*) malloc(sizeof(NfaStack));
stack->capacity = capacity;
stack->size = 0;
stack->stack = malloc(sizeof(NFA*)*capacity);
if (stack == NULL) {
perror("Error creating stack.\n");
return NULL;
}
return stack;
}
/* Couple of useful functions */
int nfa_stack_is_empty(NfaStack *stack) { return (stack->size == 0) ? 1 : 0; }
int nfa_stack_is_full(NfaStack *stack) { return (stack->size == stack->capacity) ? 1 : 0; }
/* Push function - adds nfa to stack */
void nfa_stack_push(NfaStack *stack, NFA *nfa) {
if (nfa_stack_is_full(stack)) {
perror("Stack size wouuld exceed capacity.\n");
return;
}
int i = stack->size;
stack->stack[i] = nfa;
stack->size++;
}
/* Pop function - pops element from stack */
NFA* nfa_stack_pop(NfaStack *stack) {
if (nfa_stack_is_empty(stack)) {
perror("Stack size is 0, cannot pop.\n");
return NULL;
}
int i = stack->size-1;
NFA *nfa = stack->stack[i];
stack->size--;
return nfa;
}
/* Returns the element at the top without popping it */
NFA* nfa_stack_peek(NfaStack *stack) {
if (nfa_stack_is_empty(stack)) {
perror("Stack size is 0, cannot peek.\n");
return NULL;
}
int i = stack->size-1;
NFA *nfa = stack->stack[i];
return nfa;
}
/* Prints a stack structure */
void print_nfa_stack(NfaStack *stack) {
printf("Stack: size=%d, capacity=%d [nfa]\n", stack->size, stack->capacity);
int size = stack->size;
int i;
for (i=0; i<size; i++) {
nfa_print(stack->stack[i]);
}
}