-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular_linked_list.c
160 lines (141 loc) · 3.33 KB
/
circular_linked_list.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
#include <stdlib.h>
#include <stdio.h>
#include "circular_linked_list.h"
LIST_NODE * initialize(void){
return NULL;
}
void push(LIST_NODE **head_ref, void *element){
LIST_NODE *node = malloc(sizeof(LIST_NODE));
node->data = element;
if (!*head_ref) {//empty
node->next = node;
node->prev = node;
}
else{
node->next = *head_ref;
node->prev = node->next->prev;//previous points to last element
node->next->prev->next = node;//last element points to new head
node->next->prev = node;//next element points to new head
}
*head_ref = node;
}
void append(LIST_NODE **head_ref, void *element){
LIST_NODE *node = malloc(sizeof(LIST_NODE));
LIST_NODE *aux = *head_ref;
node->data = element;
if (!aux) {//empty list
node->prev = node;
node->next = node;
*head_ref = node;
}
else{
aux = last_node(*head_ref);
aux->next = node;
(*head_ref)->prev = node;
node->next = *head_ref;
node->prev = aux;
}
}
void for_each(LIST_NODE *head, iterator iterator){
LIST_NODE *node = head;
while(node){
iterator(node->data);
node = (node->next != head) ? node->next : NULL;
}
}
void for_each_reverse(LIST_NODE *head, iterator iterator){
LIST_NODE *node = head;
node = last_node(head);
while(node){
iterator(node->data);
node = (node == head) ? NULL : node->prev;
}
}
void destroy(LIST_NODE **head_ref){
LIST_NODE *aux = *head_ref;
LIST_NODE *head = *head_ref;
while(*head_ref){
aux = *head_ref;
*head_ref = (aux->next == head) ? NULL : aux->next;
free(aux->data);
free(aux);
aux = NULL;
}
free(*head_ref);
*head_ref = NULL;
}
void remove_all(LIST_NODE **head_ref, filter filter, void * filter_arg ){
LIST_NODE *aux = *head_ref;
LIST_NODE *next;
LIST_NODE *current;
LIST_NODE *tail = last_node(*head_ref);
while(aux){
current = aux;
next = aux->next;
if (filter(aux->data, filter_arg)) {//remove
if (aux == *head_ref) {//first element
*head_ref = (aux->next == aux) ? NULL : aux->next;
if (*head_ref) {
tail->next = *head_ref;
}
}
else {
aux->prev->next = aux->next;
}
aux->next->prev = aux->prev;
free(aux->data);
free(aux);
aux = NULL;
}
tail = last_node(*head_ref);
aux = (*head_ref && current != tail) ? next : NULL;
}
}
void insert_ordered(LIST_NODE **head_ref, void *element, filter filter){
LIST_NODE *node = malloc(sizeof(LIST_NODE));
LIST_NODE *aux = *head_ref;
LIST_NODE *next = NULL;
node->data = element;
bool found = FALSE;
if (!aux) {//empty list
node->prev = node;
node->next = node;
*head_ref = node;
}
else if (!filter(aux->data, element)) {
push(head_ref, element);
}
else{
while (filter(aux->data, element)) {
if (aux->next == *head_ref) {
found = TRUE;
break;
}
aux = aux->next;
}
if (!found) {
aux = aux->prev;
}
next = aux->next;
aux->next = node;
node->next = next;
node->next->prev = node;
node->prev = aux;
}
}
int list_size(LIST_NODE *head){
unsigned int i = 0;
LIST_NODE *node = head;
while(node){
i++;
node = (node->next == head) ? NULL : node->next;
}
return i;
}
LIST_NODE * last_node(LIST_NODE *head){
LIST_NODE *node = head;
if (node) {
node = node->prev;
}
return node;
}