Added some functions
This commit is contained in:
+18
-21
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user