-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataStructure.c
57 lines (46 loc) · 1.48 KB
/
dataStructure.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
// partial source: Ziming's submission to COMP30023 assignment 1 [DataStructure.h]
// I have copied over some parts of it and modified in this file
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "dataStructure.h"
/* linked list node helpers*/
node_t* createNode(void* data, int32_t id){
node_t* node = malloc(sizeof(node_t));
assert(node != NULL);
node->data = data;
node->id = id;
node->next = NULL;
return node;
}
/* linked list helpers*/
linkedList_t* createLinkedList(){
linkedList_t* linkedList = (linkedList_t*)malloc(sizeof(linkedList_t));
assert(linkedList != NULL);
linkedList->head = NULL;
linkedList->tail = NULL;
return linkedList;
}
void insertAtLinkedListTail(linkedList_t* linkedList, node_t* newNode){
assert(linkedList != NULL);
assert(newNode != NULL);
newNode->next = NULL;
if(linkedList->tail == NULL){
linkedList->head = linkedList->tail = newNode;
}
else{
linkedList->tail->next = newNode;
linkedList->tail = newNode;
}
}
/* registered function helper*/
function_t* createFunction(char* name, rpc_handler handler){
function_t* newFunction = (function_t*)malloc(sizeof(*newFunction));
assert(newFunction != NULL);
//newFunction->name = name;
size_t name_len = strlen(name);
newFunction->name = (char*)malloc(name_len + 1);
strcpy(newFunction->name, name);
newFunction->handler = handler;
return newFunction;
}