-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
95 lines (73 loc) · 2.08 KB
/
stack.c
File metadata and controls
95 lines (73 loc) · 2.08 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/********************************************************************/
/*Projekt:Implementace interpretu imperativního jazyka IFJ16 */
/*Jména řešitelů: Sebastián Kisela, Ondrej Svoreň, Daniel Rudík, */
/* Patrik Roman, Martin Chudý */
/*Loginy řešitelů: xkisel02, xsvore01, xrudik00, xroman10, xchudy04 */
/********************************************************************/
#include "stack.h"
int solved;
void stackError ( int error_code ){
/* ----------
** Vytiskne upozornění, že došlo k chybě při práci se zásobníkem.
**
** TUTO FUNKCI, PROSÍME, NEUPRAVUJTE!
*/
// ret_error(INTERNAL_ERROR);
static const char* SERR_STRINGS[MAX_SERR+1] = {"Unknown error","Stack error: INIT","Stack error: PUSH","Stack error: TOP"};
if ( error_code <= 0 || error_code > MAX_SERR )
error_code = 0;
printf ( "%s\n", SERR_STRINGS[error_code] );
//err_flag = 1;
}
TStack* stackInit () {
//if stack does not exist, exit function
TStack * s;
s = malloc(sizeof(TStack));
if(!s)ret_error(INTERNAL_ERROR);
if(!s)
{
stackError(SERR_INIT);
return NULL;
}
//set the stack top to starting position
s->capacity = 10;
s->data = malloc(sizeof(void*) * s->capacity);
if(!s->data)ret_error(INTERNAL_ERROR);
s->top = -1;
return s;
}
int stackEmpty ( const TStack* s ) {
//returns true if the stack is empty
return ( s->top == -1) ? TRUE : FALSE;
}
void *stackTop ( const TStack* s) {
//if stack is empty, exit function
if(stackEmpty(s))
{
stackError(SERR_TOP);
return NULL;
}
//return data from the top of the stack
return s->data[s->top];
}
void* stackPop ( TStack* s ) {
//if the stack exists, delete element from the top of it
void *var = s->data[s->top];
if(!stackEmpty(s))
{
s->data[s->top] = '\0';
s->top--;
}
return var;
}
void stackPush ( TStack* s, void *data ) {
//if stack is full, exit function
s->top++;
if(s->top > s->capacity)
{
s->data = realloc(s->data, sizeof(void *) * s->capacity * 2);
if(!s->data)ret_error(INTERNAL_ERROR);
s->capacity = s->capacity * 2;
}
s->data[s->top] = data;
}