summaryrefslogtreecommitdiff
path: root/BTree/bt_data.c
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-07-10 22:55:07 +0200
committerCarlos Maiolino <[email protected]>2025-07-10 22:56:55 +0200
commitd98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch)
tree267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /BTree/bt_data.c
parent869e68986aa8f69af6e7842260a68d1e5c6f796f (diff)
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'BTree/bt_data.c')
-rw-r--r--BTree/bt_data.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/BTree/bt_data.c b/BTree/bt_data.c
new file mode 100644
index 0000000..c10ce3d
--- /dev/null
+++ b/BTree/bt_data.c
@@ -0,0 +1,51 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "btree.h"
+
+struct Data * init_item(int id, char *name)
+{
+ struct Data *new = malloc(sizeof(struct Data));
+
+ if (new == NULL)
+ return new;
+
+ new->id = id;
+ strncpy(new->name, name, 20);
+
+ /* Ensure string is null terminated */
+ new->name[19] = '\0';
+
+ return new;
+}
+
+void destroy_item(struct Data *item)
+{
+ if (item)
+ free(item);
+}
+
+/* Insert new item in the tree */
+void add_item(void)
+{
+ char name[20];
+ int id;
+ struct Data *item;
+
+ printf("Name: ");
+ scanf(" %s", name);
+ printf("Id: ");
+ scanf(" %d", &id);
+
+ item = init_item(id, name);
+
+ if (!item) {
+ printf("Error adding new data\n");
+ return;
+ }
+
+ if (!btree_add(item))
+ printf("Btree Error: Failed to add item\n");
+
+ return;
+}