summaryrefslogtreecommitdiff
path: root/BTree/bt_data.c
blob: c10ce3d5efb19e120e64f713b24a9a2feab604d2 (plain)
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
#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;
}