-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetStructure.c
144 lines (110 loc) · 2.45 KB
/
setStructure.c
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
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define EXIT_SUCCESS 0
#define HASHPAP_SIZE 64
#define HASHMAP_SIZE_LIST 1
#define KEY_FOUND 0
#define KEY_NOT_FOUND 1
//rappresenting item
typedef struct Item
{
const char* key;
size_t len;
struct Item* next;
} Item;
//create item
Item* create_item(const char* key_value, size_t lenght)
{
Item* new_item = ((Item*)malloc(sizeof(Item)));
new_item->key = key_value;
new_item->len = lenght;
return new_item;
}
void add_item(Item** item_list, const char* key, size_t lenght)
{
Item* item = create_item(key, lenght);
item->next = *item_list;
*item_list = item;
}
void remove_item(Item** item_list, const char* key)
{
Item* list = *item_list;
Item* previous_item = NULL;
while(list != NULL)
{
if(strcmp(list->key, key) == 0)
{
if(previous_item == NULL)
{
*item_list = list->next;
}
else
{
previous_item->next = list->next;
}
free(list);
return;
}
previous_item = list;
list = list->next;
}
}
// Function to find an item with the given key in the linked list
int find(const Item* item_list, const char* key)
{
const Item* item = item_list;
while(item != NULL)
{
if(strcmp(item->key, key) == 0)
{
//found it
printf("key found -> %s\n", key);
return KEY_FOUND;
}
item = item->next;
}
printf("Key %s -> not found\n", key);
return KEY_NOT_FOUND;
}
void print_list(Item** item_list)
{
const Item* item = *item_list;
while(item != NULL)
{
printf("KEY -> %s Lenght -> %zu\n", item->key, item->len);
item = item->next;
}
}
// Function to add a unique key to the linked list
void add_unique_key(Item** item_list, const char* key, size_t key_size)
{
if(!find(*item_list, key))
{
printf("key %s already exists return..\n", key);
return;
}
Item* new_item = create_item(key, key_size);
new_item->next = *item_list;
*item_list = new_item;
}
int main(int argc, char *argv[])
{
Item* itemList = NULL;
add_item(&itemList, "first", 0);
add_item(&itemList, "second", 1);
add_item(&itemList, "third", 2);;
print_list(&itemList);
find(itemList, "third");
find(itemList, "fourth");
printf("removing item\n");
remove_item(&itemList, "first");
print_list(&itemList);
printf("\n");
add_unique_key(&itemList, "fift", 3);
print_list(&itemList);
free(itemList);
return EXIT_SUCCESS;
}