Skip to content

Commit a94abcc

Browse files
committed
doc: create list module documentation
1 parent f325c05 commit a94abcc

File tree

2 files changed

+205
-80
lines changed

2 files changed

+205
-80
lines changed

libs/list/main.c

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
Node* __getElementAt(List* _list, const size_t index);
66

7-
// Constructor
7+
/* ------------------------------- Constructor ------------------------------ */
8+
89
void newList(List* list) { *list = NULL; }
910

10-
// Destroyer
11+
/* ------------------------------- Destructor ------------------------------- */
12+
1113
void destroyList(List* _list) {
1214
Node* nextNode;
1315

@@ -20,7 +22,8 @@ void destroyList(List* _list) {
2022
}
2123
}
2224

23-
// Getters
25+
/* --------------------------------- Getters -------------------------------- */
26+
2427
Node* __getElementAt(List* _list, const size_t index) {
2528
size_t length = 0;
2629

@@ -32,14 +35,6 @@ Node* __getElementAt(List* _list, const size_t index) {
3235
return *_list;
3336
}
3437

35-
unsigned char getHead(const List* _list, void* store, const size_t sizeOfStore) {
36-
if ((*_list) == NULL) return 0;
37-
38-
memcpy(store, (*_list)->__data, MIN(sizeOfStore, (*_list)->__sizeOfData));
39-
40-
return 1;
41-
}
42-
4338
unsigned char getElement(const List* _list, void* store, const size_t sizeOfStore,
4439
const size_t index) {
4540
size_t length = 0;
@@ -56,6 +51,25 @@ unsigned char getElement(const List* _list, void* store, const size_t sizeOfStor
5651
return 0;
5752
}
5853

54+
size_t getLength(List* _list) {
55+
size_t length = 0;
56+
57+
while ((*_list) != NULL) {
58+
length++;
59+
_list = &(*_list)->__next;
60+
}
61+
62+
return length;
63+
}
64+
65+
unsigned char getHead(const List* _list, void* store, const size_t sizeOfStore) {
66+
if ((*_list) == NULL) return 0;
67+
68+
memcpy(store, (*_list)->__data, MIN(sizeOfStore, (*_list)->__sizeOfData));
69+
70+
return 1;
71+
}
72+
5973
unsigned char isListEmpty(List* _list) { return (*_list) == NULL; }
6074

6175
unsigned char isListFull(List* _list, const size_t sizeOfStore) {
@@ -71,18 +85,25 @@ unsigned char isListFull(List* _list, const size_t sizeOfStore) {
7185
return ((newNode == NULL) || (newNodeData == NULL));
7286
}
7387

74-
size_t getLength(List* _list) {
75-
size_t length = 0;
88+
/* --------------------------------- Methods -------------------------------- */
7689

77-
while ((*_list) != NULL) {
78-
length++;
90+
unsigned char popElement(List* _list, void* store, const size_t sizeOfStore) {
91+
if ((*_list) == NULL) {
92+
return 0;
93+
}
94+
95+
while ((*_list)->__next != NULL) {
7996
_list = &(*_list)->__next;
8097
}
8198

82-
return length;
99+
memcpy(store, (*_list)->__data, MIN(sizeOfStore, (*_list)->__sizeOfData));
100+
free((*_list)->__data);
101+
free(*_list);
102+
*_list = NULL;
103+
104+
return 1;
83105
}
84106

85-
// Methods
86107
unsigned char pushElement(List* _list, void* data, const size_t sizeOfData) {
87108
Node* newNode;
88109

@@ -104,49 +125,11 @@ unsigned char pushElement(List* _list, void* data, const size_t sizeOfData) {
104125
return 1;
105126
}
106127

107-
unsigned char popElement(List* _list, void* store, const size_t sizeOfStore) {
108-
if ((*_list) == NULL) {
109-
return 0;
110-
}
111-
112-
while ((*_list)->__next != NULL) {
113-
_list = &(*_list)->__next;
114-
}
115-
116-
memcpy(store, (*_list)->__data, MIN(sizeOfStore, (*_list)->__sizeOfData));
117-
free((*_list)->__data);
118-
free(*_list);
119-
*_list = NULL;
120-
121-
return 1;
122-
}
123-
124-
void selectionSort(List* _list, int (*cmp)(const void* a, const void* b)) {
125-
void* aux;
126-
unsigned tam;
127-
List *minor, *iterator;
128-
129-
if (!(*_list)) return;
130-
131-
while ((*_list)->__next) {
132-
iterator = &(*_list)->__next;
133-
minor = _list;
134-
while (*iterator) {
135-
if (cmp((*minor)->__data, (*iterator)->__data) > 0) minor = iterator;
136-
iterator = &(*iterator)->__next;
137-
}
138-
if (*minor != *_list) {
139-
aux = (*_list)->__data;
140-
tam = (*_list)->__sizeOfData;
141-
142-
(*_list)->__data = (*minor)->__data;
143-
(*_list)->__sizeOfData = (*minor)->__sizeOfData;
144-
145-
(*minor)->__data = aux;
146-
(*minor)->__sizeOfData = tam;
147-
}
128+
void map(List* _list, void (*callback)(void* element)) {
129+
while (*_list) {
130+
callback((*_list)->__data);
148131
_list = &(*_list)->__next;
149-
}
132+
};
150133
}
151134

152135
void randomSort(List* _list) {
@@ -203,9 +186,30 @@ void randomSort(List* _list) {
203186
}
204187
}
205188

206-
void map(List* _list, action _action, void* punt) {
207-
while (*_list) {
208-
_action((*_list)->__data, punt);
189+
void selectionSort(List* _list, int (*cmp)(const void* a, const void* b)) {
190+
void* aux;
191+
unsigned tam;
192+
List *minor, *iterator;
193+
194+
if (!(*_list)) return;
195+
196+
while ((*_list)->__next) {
197+
iterator = &(*_list)->__next;
198+
minor = _list;
199+
while (*iterator) {
200+
if (cmp((*minor)->__data, (*iterator)->__data) > 0) minor = iterator;
201+
iterator = &(*iterator)->__next;
202+
}
203+
if (*minor != *_list) {
204+
aux = (*_list)->__data;
205+
tam = (*_list)->__sizeOfData;
206+
207+
(*_list)->__data = (*minor)->__data;
208+
(*_list)->__sizeOfData = (*minor)->__sizeOfData;
209+
210+
(*minor)->__data = aux;
211+
(*minor)->__sizeOfData = tam;
212+
}
209213
_list = &(*_list)->__next;
210-
};
211-
}
214+
}
215+
}

libs/list/main.h

Lines changed: 137 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,160 @@
66

77
#include "../structs.h"
88

9-
/* ---------- List ---------- */
9+
typedef Node* List; /** Singly linked list */
1010

11-
typedef Node* List;
11+
/* ------------------------------- Constructor ------------------------------ */
1212

13-
// Constructor
13+
/**
14+
* @brief Initializes a new singly linked list.
15+
*
16+
* This function sets the given singly linked list to `NULL`.
17+
*
18+
* @param list Singly linked list structure to be initialized.
19+
*/
1420
void newList(List* list);
1521

16-
// Destroyer
17-
void destroyList(List* _list);
22+
/* ------------------------------- Destructor ------------------------------- */
1823

19-
// Getters
20-
unsigned char getHead(const List* _list, void* store, const size_t sizeOfStore);
24+
/**
25+
* @brief Destroys a singly linked list.
26+
*
27+
* This function deallocates all the memory used by the nodes in the singly linked list,
28+
* effectively destroying the list and setting the list pointer to `NULL`.
29+
*
30+
* @param _list Singly linked list to be destroyed.
31+
*/
32+
void destroyList(List* _list);
2133

34+
/* --------------------------------- Getters -------------------------------- */
35+
36+
/**
37+
* @brief Retrieves a copy of the data at a specific index in the singly linked list.
38+
*
39+
* This function copies the data from the node at the specified index in the singly linked list
40+
* into the provided storage.
41+
*
42+
* @param _list Singly linked list.
43+
* @param store Storage where the data from the specified index will be copied.
44+
* @param sizeOfStore Size in bytes of the storage.
45+
* @param index Index of data to be copied.
46+
*
47+
* @return 0 if the data was successfully copied, 1 otherwise.
48+
*
49+
* @warning Ensure that the storage provided is large enough to hold the data from the specified
50+
* index.
51+
*/
2252
unsigned char getElement(const List* _list, void* store, const size_t sizeOfStore,
2353
const size_t index);
2454

25-
unsigned char isListEmpty(List* _list);
55+
/**
56+
* @brief Retrieves the length of the singly linked list.
57+
*
58+
* @param _list Singly linked list whose length is to be determined.
59+
*
60+
* @return Length of the singly linked list.
61+
*/
62+
size_t getLength(List* _list);
2663

27-
unsigned char isListFull(List* _list, const size_t sizeOfStore);
64+
/**
65+
* @brief Retrieves a copy of the top data inside the singly linked list.
66+
*
67+
* This function copies the data from the top of the singly linked list into the provided
68+
* storage.
69+
*
70+
* @param _list Singly linked list.
71+
* @param store Storage where the data from the top will be copied.
72+
* @param sizeOfStore Size in bytes of the storage.
73+
*
74+
* @return 1 if the data was successfully copied, 0 otherwise.
75+
*
76+
* @warning Ensure that the storage provided is large enough to hold the top data.
77+
*/
78+
unsigned char getHead(const List* _list, void* store, const size_t sizeOfStore);
2879

29-
size_t getLength(List* _list);
80+
/**
81+
* @brief Checks if the singly linked list is empty.
82+
*
83+
* @param _list Singly linked list to be checked.
84+
*
85+
* @return 1 if the list is empty, 0 otherwise.
86+
*/
87+
unsigned char isListEmpty(List* _list);
3088

31-
// Methods
32-
unsigned char pushElement(List* _list, void* data, const size_t sizeOfData);
89+
/**
90+
* @brief Checks if the singly linked list can accommodate more data.
91+
*
92+
* This function attempts to allocate memory for a new data to determine if the list
93+
* can accommodate more data. If either allocation fails, the list is considered full.
94+
*
95+
* @param _list Singly linked list to be checked.
96+
* @param sizeOfStore Size in bytes of the data to be stored.
97+
*
98+
* @return 1 if the list is full, 0 otherwise.
99+
*
100+
* @warning This function temporarily allocates memory to check for fullness and immediately frees
101+
* it.
102+
*/
103+
unsigned char isListFull(List* _list, const size_t sizeOfStore);
33104

105+
/* --------------------------------- Methods -------------------------------- */
106+
107+
/**
108+
* @brief Removes the last data from the singly linked list and copies its data.
109+
*
110+
* This function removes the last data from the singly linked list and copies its data into the
111+
* provided storage.
112+
*
113+
* @param _list Singly linked list from which the last data will be removed.
114+
* @param store Storage where the data will be copied.
115+
* @param sizeOfStore Size in bytes of the storage.
116+
*
117+
* @return 1 if the data was successfully copied to the store, 0 otherwise.
118+
*
119+
* @warning Ensure that the storage provided is large enough to hold data.
120+
*/
34121
unsigned char popElement(List* _list, void* store, const size_t sizeOfStore);
35122

36-
void selectionSort(List* _list, int (*cmp)(const void* a, const void* b));
123+
/**
124+
* @brief Adds a new data to the end of the singly linked list.
125+
*
126+
* @param _list Singly linked list where the new data will be added.
127+
* @param data Data to be stored at the end of the singly linked list.
128+
* @param sizeOfData Size in bytes of the data to be stored.
129+
*
130+
* @return 1 if the data was successfully added, 0 otherwise.
131+
*/
132+
unsigned char pushElement(List* _list, void* data, const size_t sizeOfData);
37133

134+
/**
135+
* @brief Applies a callback function to each data in the singly linked list.
136+
*
137+
* This function iterates through each data in the singly linked list and applies the provided
138+
* callback function to the data.
139+
*
140+
* @param _list Singly linked list whose data will be processed.
141+
* @param callback Function that will be applied to each data.
142+
*/
143+
void map(List* _list, void (*callback)(void* element));
144+
145+
/**
146+
* @brief Randomly shuffles the data of the singly linked list.
147+
*
148+
* @param _list Singly linked list to be shuffled.
149+
*/
38150
void randomSort(List* _list);
39151

40-
typedef void (*action)(void* data, void* data2);
41-
42-
void map(List* _list, action _action, void* punt);
152+
/**
153+
* @brief Sorts the singly linked list using the selection sort algorithm.
154+
*
155+
* This function sorts the data of the singly linked list in ascending order
156+
* based on the comparison function provided. The selection sort algorithm repeatedly
157+
* selects the smallest data from the unsorted portion of the list and swaps it
158+
* with the first unsorted data.
159+
*
160+
* @param _list Singly linked list to be sorted.
161+
* @param cmp Comparison function.
162+
*/
163+
void selectionSort(List* _list, int (*cmp)(const void* a, const void* b));
43164

44165
#endif // LIBS__LIST_H_INCLUDED

0 commit comments

Comments
 (0)