-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.h
151 lines (138 loc) · 3.87 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
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
/**
* @file stack.h
*
* @brief Header file for stack operations involving nodes containing a Latin square board.
*
* This file contains function declarations for initializing and managing a stack of nodes,
* where each node holds a 2D array representing part of a Latin square.
*
* @authors
* - Panagiotis Tsembekis
* - Rafael Tsekouronas
*
* @bug No known bugs.
*/
#ifndef STACK_H
#define STACK_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/**
* @struct NODE
* @brief Represents a node in the stack containing a Latin square board.
*
* Each node holds a 2D array representing a section of a Latin square,
* along with its position (row and column) and the size of the array.
* Nodes are linked together to form a stack.
*/
typedef struct node
{
int **square;
int row;
int col;
int arraySize;
struct node *next;
} NODE;
/**
* @struct STACK
* @brief Represents a stack data structure for managing NODE elements.
*
* The stack tracks the top element and the number of nodes in the stack,
* allowing for stack operations such as push and pop.
*/
typedef struct stack
{
NODE *top;
int length;
} STACK;
/**
* @brief Initializes the stack.
*
* Allocates memory for a stack and initializes its top pointer and length.
*
* @param stack A double pointer to the stack to be initialized.
*
* @return `EXIT_SUCCESS` on successful initialization, `EXIT_FAILURE` otherwise.
*/
int initStack(STACK **stack);
/**
* @brief Initializes a new node with a Latin square board and position.
*
* Allocates memory for a new node and its 2D array representing a board.
* Copies the content of the provided board array into the node.
*
* @param newNode A double pointer to the new node to be initialized.
* @param board A 2D array representing the board to be copied into the node.
* @param row The row position in the Latin square.
* @param col The column position in the Latin square.
* @param size The size of the Latin square.
*
* @return `EXIT_SUCCESS` on successful initialization, `EXIT_FAILURE` otherwise.
*/
int initNode(NODE **newNode, int **square, int row, int col, int size);
/**
* @brief Pushes a node onto the stack.
*
* Adds the specified node to the top of the stack.
*
* @param stack A pointer to the stack.
* @param newNode The node to be pushed onto the stack.
*
* @return `EXIT_SUCCESS` on successful push, `EXIT_FAILURE` if the stack or node is NULL.
*/
int push(STACK *stack, NODE *newNode);
/**
* @brief Pops a node from the stack.
*
* Removes and returns the node from the top of the stack.
*
* @param stack A pointer to the stack.
*
* @return The popped node, or NULL if the stack is empty or NULL.
*/
NODE *pop(STACK *stack);
/**
* @brief Checks if the stack is empty.
*
* Determines if the stack contains any elements.
*
* @param stack A pointer to the stack.
*
* @return `true` if the stack is empty, `false` otherwise.
*/
bool isEmpty(STACK *stack);
/**
* @brief Prints the contents of a node.
*
* Displays the 2D array of the given node with borders around each element.
* Negative values are shown within parentheses.
*
* @param node A pointer to the node to be printed.
*/
void printNode(NODE *node);
/**
* @brief Prints the entire stack from top to bottom.
*
* Iterates through each node in the stack and prints its contents.
*
* @param stack A pointer to the stack.
* @param size The size of the 2D array in each node.
*/
void printStack(STACK *stack, int size);
/**
* @brief Frees memory allocated for a node.
*
* Deallocates the 2D array within the node and then frees the node itself.
*
* @param node A pointer to the node to be freed.
*/
void freeNode(NODE *node);
/**
* @brief Frees memory allocated for the entire stack.
*
* Pops and frees each node in the stack, then frees the stack itself.
*
* @param stack A pointer to the stack to be freed.
*/
void freeStack(STACK *stack);
#endif // STACK_H