-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.c
110 lines (84 loc) · 2.51 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
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
#include <linux/slab.h>
#include "linkedList.h"
struct linkedList* initLinkedList(void){
struct linkedList *myLinkedList = kmalloc( sizeof(struct linkedList), GFP_KERNEL);
if(myLinkedList == NULL){
return NULL;
}
myLinkedList->head = NULL;
myLinkedList->tail = NULL;
return myLinkedList;
}
int isLinkedListEmpty(struct linkedList *myLinkedList){
if ( myLinkedList->head == NULL && myLinkedList->tail == NULL) {
return 1;
}else{
return 0;
}
}
int isLinkedListValid(struct linkedList *myLinkedList){
if ( myLinkedList == NULL){
//fprintf( stderr, "The linked list is NULL and I am sad :( \n" );
return 0;
}else if ( (myLinkedList->head == NULL || myLinkedList->tail == NULL) && ! isLinkedListEmpty(myLinkedList)) {
//fprintf( stderr, "The linked list is broken and I am sad :( \n" );
return 0;
}
return 1;
}
unsigned long countItems(struct linkedList *myLinkedList){
struct linkedListNode *myNode;
unsigned long counter = 0;
if ( ! isLinkedListValid(myLinkedList) ){
return -1;
}else if ( isLinkedListEmpty(myLinkedList)){
return 0;
}
myNode = myLinkedList->head;
do{
myNode = myNode->nextNode;
counter++;
}while(myNode != NULL);
return counter;
}
struct linkedList* insertNode( struct linkedList *myLinkedList, struct nodeDataStruct *myNodeData){
struct linkedListNode *newNode;
if ( ! isLinkedListValid(myLinkedList) ){
return myLinkedList;
}
newNode = kmalloc(sizeof(struct linkedListNode), GFP_KERNEL);
if ( newNode == NULL ){
//fprintf( stderr, "I cannot malloc a new element, I cannot add the element to the list and I am sad :( \n" );
return myLinkedList;
}
newNode->nextNode = NULL;
newNode->nodeData = myNodeData;
// If the linked list is empty, the newnode is the both the tail and the head
if ( isLinkedListEmpty(myLinkedList) ){
myLinkedList->head = newNode;
myLinkedList->tail = newNode;
}else{
myLinkedList->tail->nextNode = newNode;
myLinkedList->tail = newNode;
}
return myLinkedList;
}
struct nodeDataStruct* removeNode(struct linkedList *myLinkedList){
struct nodeDataStruct *myNodeData;
struct linkedListNode *oldHead;
if ( !isLinkedListValid(myLinkedList) || isLinkedListEmpty(myLinkedList) ){
return NULL;
}
// If there is only one element
myNodeData = myLinkedList->head->nodeData;
if ( countItems(myLinkedList) == 1 ){
kfree(myLinkedList->head);
myLinkedList->tail = NULL;
myLinkedList->head = NULL;
}else {
oldHead = myLinkedList->head;
myLinkedList->head = myLinkedList->head->nextNode;
kfree(oldHead);
}
return myNodeData;
}