2022-10-12 02:01:37 +00:00
|
|
|
#include "list.h"
|
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
ListNode* list_init_head(void* data) {
|
2022-11-10 05:32:21 +00:00
|
|
|
ListNode* new = malloc(sizeof(ListNode));
|
|
|
|
if(new == NULL) return NULL;
|
2022-10-12 02:01:37 +00:00
|
|
|
new->data = data;
|
|
|
|
new->next = NULL;
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
ListNode* list_add(ListNode* head, void* data) {
|
2022-11-10 05:32:21 +00:00
|
|
|
ListNode* new = malloc(sizeof(ListNode));
|
|
|
|
if(new == NULL) return NULL;
|
2022-10-12 02:01:37 +00:00
|
|
|
new->data = data;
|
|
|
|
new->next = NULL;
|
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
if(head == NULL)
|
2022-10-12 02:01:37 +00:00
|
|
|
head = new;
|
|
|
|
else {
|
2022-10-13 14:00:20 +00:00
|
|
|
ListNode* it;
|
2022-10-12 02:01:37 +00:00
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
for(it = head; it->next != NULL; it = it->next)
|
2022-10-12 02:01:37 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
it->next = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:32:21 +00:00
|
|
|
ListNode* list_find(ListNode* head, const void* data) {
|
2022-11-17 19:33:31 +00:00
|
|
|
ListNode* it = NULL;
|
2022-10-12 02:01:37 +00:00
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
for(it = head; it != NULL; it = it->next)
|
|
|
|
if(it->data == data) break;
|
2022-10-12 02:01:37 +00:00
|
|
|
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
ListNode* list_element_at(ListNode* head, uint16_t index) {
|
|
|
|
ListNode* it;
|
2022-10-12 02:01:37 +00:00
|
|
|
uint16_t i;
|
2022-10-13 14:00:20 +00:00
|
|
|
for(it = head, i = 0; it != NULL && i < index; it = it->next, i++)
|
|
|
|
;
|
2022-10-12 02:01:37 +00:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
ListNode* list_remove(ListNode* head, ListNode* ep) {
|
2022-10-28 15:34:35 +00:00
|
|
|
if(head == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
if(head == ep) {
|
|
|
|
ListNode* new_head = head->next;
|
2022-10-12 02:01:37 +00:00
|
|
|
free(head);
|
|
|
|
return new_head;
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
ListNode* it;
|
2022-10-12 02:01:37 +00:00
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
for(it = head; it->next != ep; it = it->next)
|
2022-10-12 02:01:37 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
it->next = ep->next;
|
|
|
|
free(ep);
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2022-11-17 19:33:31 +00:00
|
|
|
ListNode* list_remove_at(ListNode* head, uint16_t index, void** removed_node_data) {
|
|
|
|
if(head == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ListNode* it;
|
|
|
|
ListNode* prev = NULL;
|
|
|
|
|
|
|
|
uint16_t i;
|
|
|
|
|
|
|
|
for(it = head, i = 0; it != NULL && i < index; prev = it, it = it->next, i++)
|
|
|
|
;
|
|
|
|
|
|
|
|
if(it == NULL) return head;
|
|
|
|
|
|
|
|
ListNode* new_head = head;
|
|
|
|
if(prev == NULL) {
|
|
|
|
new_head = it->next;
|
|
|
|
} else {
|
|
|
|
prev->next = it->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(removed_node_data != NULL) {
|
|
|
|
*removed_node_data = it->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(it);
|
|
|
|
|
|
|
|
return new_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
ListNode* list_insert_at(ListNode* head, uint16_t index, void* data) {
|
|
|
|
if(index == 0 || head == NULL) {
|
|
|
|
ListNode* new_head = list_init_head(data);
|
|
|
|
if(new_head != NULL) {
|
|
|
|
new_head->next = head;
|
|
|
|
}
|
|
|
|
return new_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
ListNode* it;
|
|
|
|
ListNode* prev = NULL;
|
|
|
|
|
|
|
|
uint16_t i;
|
|
|
|
|
|
|
|
for(it = head, i = 0; it != NULL && i < index; prev = it, it = it->next, i++)
|
|
|
|
;
|
|
|
|
|
|
|
|
ListNode* new = malloc(sizeof(ListNode));
|
|
|
|
if(new == NULL) return NULL;
|
|
|
|
new->data = data;
|
|
|
|
new->next = it;
|
|
|
|
prev->next = new;
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
void list_free(ListNode* head) {
|
2022-11-10 05:32:21 +00:00
|
|
|
ListNode* it = head;
|
|
|
|
ListNode* tmp;
|
2022-10-12 02:01:37 +00:00
|
|
|
|
2022-10-13 14:00:20 +00:00
|
|
|
while(it != NULL) {
|
2022-10-12 02:01:37 +00:00
|
|
|
tmp = it;
|
|
|
|
it = it->next;
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
}
|