-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
79 lines (63 loc) · 2.17 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
///////////////////////////////////////////////////////////////////////////////////
// School: Brno University of Technology, Faculty of Information Technology //
// Course: Formal Languages and Compilers //
// Project: IFJ17 //
// Module: Stack //
// Authors: Kristián Liščinský (xlisci01) //
// Matúš Liščinský (xlisci02) //
// Šimon Stupinský (xstupi00) //
// Vladimír Marcin (xmarci10) //
///////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include "error.h"
#include "clear.h"
/// replace malloc calls with our malloc wrapper
#define malloc(size) _malloc(size)
stack_t* S_Init () {
stack_t* s;
if( !(s = (stack_t*)malloc(sizeof(stack_t))) )
print_err(99);
s->top_ptr = NULL;
return s;
}
void S_Push (stack_t *s, void* d) {
elem_t *new_element;
if ( (new_element = (elem_t *) malloc(sizeof(struct elem_t))) == NULL )
print_err(99);
/// insert new element on top of stack
new_element->data = d;
new_element->next_ptr = s->top_ptr;
s->top_ptr = new_element;
}
void S_Pop (stack_t *s) {
if ( s->top_ptr != NULL )
s->top_ptr = s->top_ptr->next_ptr;
}
void* S_Top (stack_t *s) {
if ( s->top_ptr != NULL )
return s->top_ptr->data;
else
return NULL;
}
int S_Empty (stack_t *s) {
return ( s->top_ptr != NULL ? 0 : -1 );
}
void S_Copy (stack_t *dst_stack, stack_t *src_stack) {
while ( !S_Empty(src_stack) ) {
S_Push(dst_stack, S_Top(src_stack));
S_Pop(src_stack);
}
}
void S_Print (stack_t *s) {
(void)s;
#ifdef DEBUG
elem_t *tmp = s->top_ptr;
while ( s->top_ptr != NULL ) {
printf ("%d\n", *((int*)s->top_ptr->data));
s->top_ptr = s->top_ptr->next_ptr;
}
s->top_ptr = tmp;
#endif
}