-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlinkedList.c
More file actions
80 lines (73 loc) · 1.72 KB
/
Copy pathlinkedList.c
File metadata and controls
80 lines (73 loc) · 1.72 KB
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
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include "linkedList.h"
struct Node* createLinkedList(void) {
struct Node* head = kmalloc(sizeof(struct Node), GFP_KERNEL);
if (head != NULL) {
head->next = NULL;
}
return head;
}
bool removeValue(struct Node* head, void* key,bool freeValue) {
struct Node* cur = head;
struct Node* prev = NULL;
while (cur != NULL) {
if (cur->key == key) {
if (prev != NULL) {
prev->next = cur->next;
} else {
head = cur->next;
}
if(freeValue){
kfree(cur->value);
}
kfree(cur);
return true;
}
prev = cur;
cur = cur->next;
}
return false; // Key not found
}
void insertNode(struct Node* head, void* key, void* value) {
struct Node* newNode = kmalloc(sizeof(struct Node), GFP_KERNEL);
if (newNode != NULL) {
newNode->key = key;
newNode->value = value;
newNode->next = head->next;
head->next = newNode;
}
}
void* findValue(struct Node* head, void* key) {
struct Node* t = head->next;
while (t != NULL) {
if (t->key == key) {
return t->value;
}
t = t->next;
}
return NULL; // Key not found
}
void cleanupLinkedList(struct Node* head,bool freeValue) {
struct Node* t = head->next;
struct Node* temp;
while (t != NULL) {
temp = t;
t = t->next;
if(freeValue){
kfree(temp->value);
}
kfree(temp);
}
kfree(head);
}
int countLinkedList(struct Node* head) {
struct Node* t = head->next;
int count=0;
while (t != NULL) {
count ++;
t = t->next;
}
return count;
}