-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.c
65 lines (51 loc) · 1.37 KB
/
linkedList.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
/*
* linkedlist.c - Source File
* Implementation of the functions declared in the header file
*/
#include <stdio.h>
#include <stdlib.h>
#include "linkedList.h"
#include "graph.h"
#include "errorHandler.h"
/* --------Functions Deceleration--------- */
linkedList_node* createNode(int);
linkedList* createLinkedList();
void free_linkedList(linkedList*);
void free_linkedList_rec(linkedList_node*);
/* --------Functions Implementation---------*/
linkedList_node* createNode(int value)
{
linkedList_node *newNode;
newNode = (linkedList_node *)malloc(sizeof(linkedList_node));
if(newNode == NULL) returnErrorByType(9);
newNode -> value = value;
newNode -> next = NULL;
return newNode;
}
linkedList* createLinkedList()
{
linkedList *list = (linkedList *)malloc(sizeof(linkedList));
if(list == NULL) returnErrorByType(8);
list -> head = NULL;
list -> size = 0;
list -> node_index = -1;
list -> free_linkedList = free_linkedList;
return list;
}
/*
* Frees the list by performing recursive frees of the nodes,
* and when the list is empty, frees the pointer to the list itself.
*/
void free_linkedList(linkedList* list) {
free_linkedList_rec(list -> head);
free(list);
}
/*A recursive implementation of free linked list */
void free_linkedList_rec(struct _linkedList_node* head)
{
if (head != NULL)
{
free_linkedList_rec(head -> next);
free(head);
}
}