-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.h
117 lines (110 loc) · 7.23 KB
/
stack.h
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @file stack.h
* @author Tomáš Hobza (xhobza03@vutbr.cz)
* @brief Generic stack implementation using linked list and macros.
* @version 0.1
* @date 2023-11-24
*
* @copyright Copyright (c) 2023
* Project: IFJ compiler
*
*/
#ifndef STACK_H
#define STACK_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// #include "psa.h" // Include psa.h if it provides necessary types for stack.h
#define DECLARE_STACK_FUNCTIONS(type) \
typedef struct type##_node \
{ \
type data; \
struct type##_node *next; \
} type##_node; \
\
typedef struct \
{ \
type##_node *top; \
unsigned int size; \
} type##_stack; \
\
type##_stack *type##_stack_init(); \
void type##_stack_push(type##_stack *stack, type value); \
type type##_stack_pop(type##_stack *stack); \
type type##_stack_top(type##_stack *stack); \
void type##_stack_free(type##_stack *stack); \
bool type##_stack_empty(type##_stack *stack);
#define DEFINE_STACK_FUNCTIONS(type) \
type##_stack *type##_stack_init() \
{ \
type##_stack *stack = (type##_stack *)malloc(sizeof(type##_stack)); \
if (stack == NULL) \
{ \
fprintf(stderr, "Memory allocation failed for stack\n"); \
throw_error(INTERNAL_ERR, -1, "Stack is allocation failed.") \
} \
stack->top = NULL; \
stack->size = 0; \
return stack; \
} \
\
void type##_stack_push(type##_stack *stack, type value) \
{ \
type##_node *newNode = (type##_node *)malloc(sizeof(type##_node)); \
if (!newNode) \
{ \
fprintf(stderr, "Memory allocation failed\n"); \
throw_error(INTERNAL_ERR, -1, "Stack new node allocation failed.") \
} \
newNode->data = value; \
newNode->next = stack->top; \
stack->top = newNode; \
stack->size++; \
} \
\
type type##_stack_pop(type##_stack *stack) \
{ \
if (stack->top == NULL || type##_stack_empty(stack)) \
{ \
fprintf(stderr, "Pop operation failed: stack is empty\n"); \
throw_error(INTERNAL_ERR, -1, "Stack is empty when popping.") \
} \
type##_node *topNode = stack->top; \
type value = topNode->data; \
stack->top = topNode->next; \
free(topNode); \
stack->size--; \
return value; \
} \
\
type type##_stack_top(type##_stack *stack) \
{ \
if (type##_stack_empty(stack)) \
{ \
fprintf(stderr, "Top operation failed: stack is empty\n"); \
throw_error(INTERNAL_ERR, -1, "Stack is empty in stack top.") \
} \
return stack->top->data; \
} \
\
void type##_stack_free(type##_stack *stack) \
{ \
while (stack->top != NULL) \
{ \
type##_node *temp = stack->top; \
stack->top = stack->top->next; \
free(temp); \
} \
free(stack); \
} \
\
bool type##_stack_empty(type##_stack *stack) \
{ \
if (stack != NULL && stack->top != NULL && stack->size > 0) \
{ \
return false; \
} \
return true; \
}
DECLARE_STACK_FUNCTIONS(int)
#endif // STACK_H