added node value allocation by passed size

This commit is contained in:
Václav Šmejkal 2023-04-14 15:28:14 +02:00
parent 189d438a18
commit c85f0d9669
Signed by: ENGO150
GPG Key ID: 4A57E86482968843
2 changed files with 8 additions and 4 deletions

View File

@ -35,7 +35,7 @@ typedef struct
} why2_list_t; //SINGLE LINKED LIST MADE OF why2_node_t } why2_list_t; //SINGLE LINKED LIST MADE OF why2_node_t
//FUNCTIONS //FUNCTIONS
void why2_list_push(why2_list_t *list, void *value); //PUSH ELEMENT TO LIST BACK void why2_list_push(why2_list_t *list, void *value, unsigned long size); //PUSH ELEMENT TO LIST BACK
void why2_list_remove(why2_list_t *list, why2_node_t *node); //REMOVE ELEMENT void why2_list_remove(why2_list_t *list, why2_node_t *node); //REMOVE ELEMENT
void why2_list_remove_back(why2_list_t *list); //REMOVE LAST ELEMENT void why2_list_remove_back(why2_list_t *list); //REMOVE LAST ELEMENT
why2_node_t *why2_list_find(why2_list_t *list, void *value); //FIND ELEMENT IN LIST why2_node_t *why2_list_find(why2_list_t *list, void *value); //FIND ELEMENT IN LIST

View File

@ -22,26 +22,29 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
void why2_list_push(why2_list_t *list, void *value) void why2_list_push(why2_list_t *list, void *value, unsigned long size)
{ {
//CREATE NODE //CREATE NODE
why2_node_t *head = list -> head; why2_node_t *head = list -> head;
why2_node_t *new_node = malloc(sizeof(why2_node_t)); why2_node_t *new_node = malloc(sizeof(why2_node_t));
new_node -> value = malloc(size); //TODO: Deallocation
why2_node_t *buffer = head; why2_node_t *buffer = head;
//INSERT DATA //INSERT DATA
new_node -> value = value; memcpy(new_node -> value, value, size);
new_node -> next = NULL; new_node -> next = NULL;
if (head == NULL) //INIT LIST if (head == NULL) //INIT LIST
{ {
head = new_node; buffer = new_node;
} else } else
{ {
while (buffer -> next != NULL) buffer = buffer -> next; //GET TO THE END OF LIST while (buffer -> next != NULL) buffer = buffer -> next; //GET TO THE END OF LIST
buffer -> next = new_node; //LINK buffer -> next = new_node; //LINK
} }
list -> head = buffer; //TODO: Unused?
} }
void why2_list_remove(why2_list_t *list, why2_node_t *node) void why2_list_remove(why2_list_t *list, why2_node_t *node)
@ -77,6 +80,7 @@ void why2_list_remove(why2_list_t *list, why2_node_t *node)
} }
//DEALLOCATION //DEALLOCATION
free(node -> value);
free(node); free(node);
} }