Added some functions

This commit is contained in:
Ano-sys
2025-01-29 01:36:31 +01:00
parent 1d760df46b
commit 190011be88
95 changed files with 37949 additions and 1161 deletions
+18 -21
View File
@@ -4,9 +4,6 @@
#include <stdlib.h>
#include <string.h>
key_value_pair *head = NULL;
key_value_pair *tail = NULL;
char *key_value_pair_to_string(key_value_pair *kv)
{
char *str = malloc(sizeof(char) * (strlen(kv->key) + strlen(kv->value) + 1));
@@ -62,29 +59,29 @@ void free_key_value_pair(key_value_pair **kv)
}
// frees whole key_value_pair list structure
void free_key_value_pairs()
void free_key_value_pairs(key_value_pair **head, key_value_pair **tail)
{
while (head != NULL)
while (*head != NULL)
{
key_value_pair *toFree = head;
head = head->next;
key_value_pair *toFree = *head;
*head = (*head)->next;
free_key_value_pair(&toFree);
}
tail = NULL;
*tail = NULL;
}
// adds given key_value_pair to list
int add_key_value_pair(key_value_pair *kv)
int add_key_value_pair(key_value_pair *kv, key_value_pair **head, key_value_pair **tail)
{
if (head == NULL)
if (!head || !*head)
{
head = kv;
tail = kv;
*head = kv;
*tail = kv;
}
else
{
key_value_pair *iterator = head;
key_value_pair *iterator = *head;
while (iterator->next != NULL)
{
if (strcmp(kv->key, iterator->key) == 0)
@@ -103,23 +100,23 @@ int add_key_value_pair(key_value_pair *kv)
iterator = iterator->next;
}
iterator->next = kv;
tail = kv;
*tail = kv;
}
return 0;
}
// removes given key_value_pair from list
void remove_key_value_pair(key_value_pair *kv)
void remove_key_value_pair(key_value_pair *kv, key_value_pair **head, key_value_pair **tail)
{
if (head == NULL || kv == NULL) return;
if (head == kv)
if (*head == NULL || kv == NULL) return;
if (*head == kv)
{
head = head->next;
if (tail == kv) tail = NULL;
*head = (*head)->next;
if (*tail == kv) *tail = NULL;
return;
}
key_value_pair *iterator = head;
key_value_pair *iterator = *head;
if (iterator == NULL) return;
while (iterator != NULL)
@@ -127,7 +124,7 @@ void remove_key_value_pair(key_value_pair *kv)
if (iterator->next == kv)
{
iterator->next = kv->next;
if (tail == kv) tail = iterator;
if (*tail == kv) *tail = iterator;
return;
}
iterator = iterator->next;