-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
293 lines (252 loc) · 7.12 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* @file stack.c
*
* @brief Implementation of stack operations for managing nodes containing a Latin square board.
*
* This file provides the implementation of functions to initialize, manipulate, and free a stack
* of nodes. Each node contains a dynamically allocated 2D array representing part of a Latin
* square, along with its position in the square. The stack supports basic operations like push,
* pop, and printing, as well as memory management functions for safe deallocation.
*
* @authors
* - Panagiotis Tsembekis
* - Rafael Tsekouronas
*
* @bug No known bugs.
*/
#include "stack.h"
#include "file.h"
int initStack(STACK **stack)
{
*stack = (STACK *)malloc(1 * sizeof(STACK));
if (stack == NULL) // handle improper memory allocation
{
return EXIT_FAILURE;
}
// Initialize stack's values
(*stack)->top = NULL;
(*stack)->length = 0;
return EXIT_SUCCESS;
}
int initNode(NODE **newNode, int **board, int row, int col, int size)
{
*newNode = (NODE *)malloc(sizeof(NODE));
if (*newNode == NULL) // handle improper memory allocation
{
return EXIT_FAILURE;
}
// Assign values to `newNode`
(*newNode)->row = row;
(*newNode)->col = col;
(*newNode)->arraySize = size;
// Allocate memory for the 2D array
(*newNode)->square = (int **)malloc(size * sizeof(int *));
if ((*newNode)->square == NULL)
{ // handle improper memory allocation
perror("Error: Memory allocation for new node's 2D array rows failed.\n");
free(*newNode); // free previously allocated space for node
*newNode = NULL;
return EXIT_FAILURE;
}
// Allocate space for columns for 2D array of `newNode`
for (int i = 0; i < size; i++)
{
(*newNode)->square[i] = (int *)malloc(size * sizeof(int));
if ((*newNode)->square[i] == NULL) // handle improper memory allocation
{
perror("Error: Memory allocation for new node's 2D array columns failed.\n");
// Free previously allocated space for 2D array and `node`
free2DArray((*newNode)->square, i);
free((*newNode)->square);
free(*newNode); // free newNode itself
*newNode = NULL;
return EXIT_FAILURE;
}
// Copy each element in the row
for (int j = 0; j < size; j++)
{
(*newNode)->square[i][j] = board[i][j];
}
}
(*newNode)->next = NULL; // initialize the next pointer to NULL
return EXIT_SUCCESS;
}
int push(STACK *stack, NODE *newNode)
{
if (stack == NULL) // handle edge case of null stack
{
perror("Unable to push into stack | Stack is NULL.\n");
return EXIT_FAILURE;
}
if (newNode == NULL) // handle edge case of null node passed in
{
perror("Unable to push into stack | newNode is NULL.\n");
}
// Add node to stack
newNode->next = stack->top;
stack->top = newNode;
(stack->length)++;
return EXIT_SUCCESS;
}
NODE *pop(STACK *stack)
{
if (stack == NULL || stack->length == 0) // handle edge case of empty/null stack
{
perror("Unable to pop from stack | Empty or NULL stack.\n");
return NULL;
}
// Remove node at stack->top
NODE *temp = stack->top;
stack->top = temp->next;
(stack->length)--;
return temp;
}
bool isEmpty(STACK *stack)
{
return (stack->length == 0); // if length = 0 => empty stack
}
void printNode(NODE *node)
{
if (node == NULL) // handle edge case of null node
{
printf("Can't print node. Node is NULL!\n");
return;
}
// Print top border for the node
for (int i = 0; i < node->arraySize; i++)
{
printf("+-----");
}
printf("+\n");
// Iterate over the 2D array and print each element with borders
for (int i = 0; i < node->arraySize; i++)
{
for (int j = 0; j < node->arraySize; j++)
{
if (node->square[i][j] < 0)
{ // negative values printed inside parentheses
printf("| (%d) ", -node->square[i][j]);
}
else
{ // normal values
printf("| %d ", node->square[i][j]);
}
}
printf("|\n"); // end the row
// Print bottom border for the current row
for (int k = 0; k < node->arraySize; k++)
{
printf("+-----");
}
printf("+\n");
}
}
void printStack(STACK *stack, int size)
{
if (isEmpty(stack))
{
printf("Stack is empty!\n");
return;
}
NODE *current = stack->top; // start from top element
printf("Stack elements (from top to bottom):\n");
while (current != NULL)
{
printf("Node (row: %d, col: %d):\n", current->row, current->col);
printNode(current); // print node's contents
printf("\n"); // separate nodes with a blank line
current = current->next;
}
}
void freeNode(NODE *node)
{
if (node != NULL)
{
if (node->square != NULL)
{
free2DArray(node->square, node->arraySize); // free 2D array
}
free(node); // free node itself
}
}
void freeStack(STACK *stack)
{
while (!isEmpty(stack))
{
NODE *node = pop(stack); // pop top node
freeNode(node); // free top node
}
free(stack);
}
#ifdef DEBUG_STACK
int main()
{
STACK *stack = NULL;
// Initialize the stack
if (initStack(&stack) != EXIT_SUCCESS)
{
printf("Failed to initialize stack.\n");
return 1;
}
printf("Stack initialized successfully.\n");
// Create a 2D array to represent a Latin square node
int size = 3;
int **board = (int **)malloc(size * sizeof(int *));
for (int i = 0; i < size; i++)
{
board[i] = (int *)malloc(size * sizeof(int));
for (int j = 0; j < size; j++)
{
board[i][j] = (i + j + 1) % size + 1; // Simple Latin square
}
}
// Create and push nodes into the stack
for (int i = 0; i < 3; i++)
{
NODE *node = NULL;
if (initNode(&node, board, i, i, size) != EXIT_SUCCESS)
{
printf("Failed to initialize node %d.\n", i);
}
else
{
printf("Node %d initialized successfully.\n", i);
push(stack, node);
printf("Node %d pushed to stack.\n", i);
}
}
// Print the stack
printf("\nCurrent stack:\n");
printStack(stack, size);
// Pop and print nodes
for (int i = 0; i < 3; i++)
{
NODE *node = pop(stack);
if (node != NULL)
{
printf("\nPopped node (row: %d, col: %d):\n", node->row, node->col);
printNode(node);
freeNode(node);
}
else
{
printf("Failed to pop node %d.\n", i);
}
}
// Check if the stack is empty
if (isEmpty(stack))
{
printf("\nStack is now empty.\n");
}
else
{
printf("\nStack is not empty.\n");
}
// Free the stack
freeStack(stack);
printf("Stack freed successfully.\n");
// Free the test board
free2DArray(board, size);
return 0;
}
#endif