-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.c
More file actions
50 lines (46 loc) · 1.16 KB
/
Copy pathlinkedlist.c
File metadata and controls
50 lines (46 loc) · 1.16 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
#include "linkedlist.h"
LinkedList* createLinkedList(void) {
LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
if (list == NULL) {
printf("memory allocation failed\n");
return NULL;
}
list->head = NULL;
list->count = 0;
return list;
}
void insertFront(LinkedList* list, void* data) {
LinkedListNode* newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
if (newNode == NULL) {
printf("memory allocation failed\n");
return;
}
newNode->data = data;
newNode->next = list->head;
list->head = newNode;
list->count++;
}
void* removeFront(LinkedList* list) {
LinkedListNode* temp;
void* data;
if (list->head == NULL) {
return NULL;
}
temp = list->head;
data = temp->data;
list->head = list->head->next;
list->count--;
free(temp);
return data;
}
void freeLinkedList(LinkedList* list, void (*freeData)(void*)) {
LinkedListNode* current = list->head;
LinkedListNode* next;
while (current != NULL) {
next = current->next;
freeData(current->data);
free(current);
current = next;
}
free(list);
}