-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpointerstack.c
64 lines (57 loc) · 1.26 KB
/
pointerstack.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
/*****************************************************************************************
*
* IFJ - interpret of IFJ15 language
*
* pointer stack
* stack to calling functions in interpret or for temp variables
*
* author: Libor Janíček (xjanic21)
* created: 2015-11-16
* modified: 2015-11-16
*
*****************************************************************************************/
#include <stdlib.h>
#include "pointerstack.h"
// init 'stack'
void initPS(pointerStack_T *stack)
{
stack->top = NULL;
}
// destroy 'stack' and free allocated memory
void destroyPS(pointerStack_T *stack)
{
while (stack->top)
popPS(stack);
}
// add new item with 'data' to 'stack'
int pushPS(pointerStack_T *stack, void *data)
{
pointerStackItem_T *newItem = malloc(sizeof(pointerStackItem_T));
if (newItem)
{
newItem->data = data;
newItem->next = stack->top;
stack->top = newItem;
return 0;
}
else
return 99;
}
// delete item on top from 'stack' and free allocated memory
void popPS(pointerStack_T *stack)
{
pointerStackItem_T *delItem = stack->top;
if (delItem)
{
stack->top = delItem->next;
free(delItem);
}
}
// return data of item on top 'stack'
void *topPS(pointerStack_T *stack)
{
if (stack->top)
return stack->top->data;
else
return NULL;
}