-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.c
More file actions
136 lines (125 loc) · 4.02 KB
/
linked_list.c
File metadata and controls
136 lines (125 loc) · 4.02 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/****************************************************************************
* Copyright (C) 2017 by Hsien Wen, Hu *
* *
* *
* This is a free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with Box. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
/**
* @file linked_list.c
* @author Hsien Wen, Hu
* @date 14 April 2017
* @brief Containing functions source code
*/
#include "linked_list.h"
/**
* This function can initial and create a new list to use
* @param list The list descriptor.
* @return No return.
* @note To becontinue...
* @warning To becontinue...
*/
void ListNew(List *list)
{
list->head = NULL;
}
/**
* This function can add node to list
* @param list The list descriptor.
* @param input_node Whatever kinds of struct which you want to discribe your information.
* @return No return.
* @note To becontinue...
* @warning To becontinue...
*/
void ListAdd(List *list, void *input_node) {
if (input_node == NULL){ return; }
ListNode *new_node = (ListNode *)malloc(sizeof(ListNode));
new_node->obj = input_node;
new_node->next = list->head;
list->head = new_node;
}
/**
* This function can free the whole list
* @param list The list descriptor.
* @return No return.
* @note To becontinue...
* @warning To becontinue...
*/
void ListFree(List *list){
while(list->head != NULL){
ListNode *tmp_next = list->head->next;
free(list->head);
list->head = tmp_next;
}
}
/**
* This function can search a specific node in the list
* @param list The list descriptor.
* @param key The struct which you want.
* @param key_size The size of key in byte.
* @return The node you want
* @note To becontinue...
* @warning To becontinue...
*/
void *ListSearch(List *list, void *key, size_t key_size){
ListNode *tmp = list->head;
while(tmp != NULL){
ListNode *tmp_next = tmp->next;
if(memcmp(tmp->obj, key, key_size) == 0){
return tmp->obj;
}else{
tmp = tmp_next;
}
}
return NULL;
}
/**
* This function can delete a specifi node in the list
* @param list The list descriptor.
* @param key The struct which you want.
* @param key_size The size of key in byte.
* @return The reault of the function
* @see #LIST_FEED_BACK
* @note To becontinue...
* @warning To becontinue...
*/
LIST_FEED_BACK ListDelete(List *list, void *key, size_t key_size){
if(memcmp(list->head->obj, key, key_size) == 0){
ListNode *tmp = list->head;
list->head = list->head->next;
free(tmp);
return DELETED;
}
ListNode *tmpCurrent = list->head->next;
ListNode *tmpPrevious = list->head;
while(tmpCurrent != NULL && tmpPrevious != NULL){
if(memcmp(tmpCurrent->obj, key, key_size) == 0){
ListNode *tmp = tmpCurrent;
tmpPrevious->next = tmpCurrent->next;
free(tmp);
return DELETED;
}
tmpPrevious = tmpCurrent;
tmpCurrent = tmpCurrent->next;
}
return NO_MATCH;
}
/**
* This function can print the whole list.
* @param list The list descriptor.
* @param f print function
* @return No return
* @note To becontinue...
* @warning To becontinue...
*/
void ListPrint(List *list, PRINT_FUNC f) {
ListNode *p;
for (p = list->head; p != NULL; p=p->next)
f(p->obj);
}