-
Notifications
You must be signed in to change notification settings - Fork 2
/
linkedlist.c
189 lines (166 loc) · 3.68 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (c) 2019 Alan Skorkin
* https://github.com/skorks/c-linked-list
*
* I reformatted the code according to my coding style, made heavy tweeks
* and add my code as well
*
* Copyright (C) 2021 Edward LEI <edward_lei72@hotmail.com>
*
* license: MIT license
*/
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
//#define DEBUG
#include "debug.h"
static node_t *_node_new(void *data,
const long stamp)
{
node_t *n = malloc(sizeof(struct _node));
if (!n) return NULL;
n->data = data;
n->stamp = stamp;
n->next = NULL;
return n;
}
list_t *list_new()
{
list_t *list = malloc(sizeof(struct _list));
if (!list) return NULL;
list->head = NULL;
list->current = NULL;
return list;
}
static void _add(list_t *list,
void *data,
const long stamp)
{
node_t *current = NULL;
if (list->head == NULL) {
list->head = _node_new(data, stamp);
}
else {
current = list->head;
while (current->next != NULL) {
current = current->next;
}
current->next = _node_new(data, stamp);
}
}
void list_update(list_t *list,
void *data,
const long stamp)
{
node_t *current = list->head;
while (current != NULL){
if (current->data == data && current->stamp == stamp) return;
if (current->data == data && current->stamp < stamp) {
D_PRINT("[STAMP] record time %ld, current time %ld\n",
current->stamp, stamp);
current->stamp = stamp;
return;
}
current = current->next;
}
_add(list, data, stamp);
}
void list_del(list_t *list,
const long stamp)
{
node_t *current = list->head;
node_t *previous = current;
while (current != NULL) {
if (current->stamp == stamp) {
previous->next = current->next;
if (current == list->head) list->head = current->next;
free(current);
return;
}
previous = current;
current = current->next;
}
}
void list_reverse(list_t *list)
{
node_t *reversed = NULL;
node_t *current = list->head;
node_t *temp = NULL;
while (current != NULL) {
temp = current;
current = current->next;
temp->next = reversed;
reversed = temp;
}
list->head = reversed;
}
static void _swap(node_t *p1,
node_t *p2)
{
long stamp = p1->stamp;
void *data = p1->data;
p1->stamp = p2->stamp;
p1->data = p2->data;
p2->stamp = stamp;
p2->data = data;
}
void list_sort(list_t *list,
const int asc)
{
node_t *start = list->head;
node_t *traverse;
node_t *min;
if (start == NULL) return;
while (start->next) {
min = start;
traverse = start->next;
while (traverse) {
/* Find the minimum */
if (!asc) {
if (min->stamp < traverse->stamp)
min = traverse;
}
else {
if (min->stamp > traverse->stamp)
min = traverse;
}
traverse = traverse->next;
}
_swap(start, min);
start = start->next;
}
}
void list_display(list_t *list)
{
node_t *current = list->head;
if (list->head == NULL) return;
for(; current != NULL; current = current->next) {
D_PRINT("[LIST] data = %ld, stamp = %ld\n",
(long)current->data, current->stamp);
}
}
void list_destroy(list_t *list)
{
node_t *current = list->head;
node_t *next = current;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
free(list);
}
node_t *list_first(list_t *list)
{
list->current = list->head;
return list->head;
}
node_t *list_next(list_t *list)
{
node_t *current = list->current;
if (current) {
list->current = list->current->next;
current = list->current;
}
return current;
}