unleashed-firmware/applications/plugins/totp/services/list/list.c

80 lines
1.4 KiB
C
Raw Normal View History

#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;
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;
new->data = data;
new->next = NULL;
2022-10-13 14:00:20 +00:00
if(head == NULL)
head = new;
else {
2022-10-13 14:00:20 +00:00
ListNode* it;
2022-10-13 14:00:20 +00:00
for(it = head; it->next != NULL; it = it->next)
;
it->next = new;
}
return head;
}
2022-11-10 05:32:21 +00:00
ListNode* list_find(ListNode* head, const void* data) {
2022-10-13 14:00:20 +00:00
ListNode* it;
2022-10-13 14:00:20 +00:00
for(it = head; it != NULL; it = it->next)
if(it->data == data) break;
return it;
}
2022-10-13 14:00:20 +00:00
ListNode* list_element_at(ListNode* head, uint16_t index) {
ListNode* it;
uint16_t i;
2022-10-13 14:00:20 +00:00
for(it = head, i = 0; it != NULL && i < index; it = it->next, i++)
;
return it;
}
2022-10-13 14:00:20 +00:00
ListNode* list_remove(ListNode* head, ListNode* ep) {
if(head == NULL) {
return NULL;
}
2022-10-13 14:00:20 +00:00
if(head == ep) {
ListNode* new_head = head->next;
free(head);
return new_head;
}
2022-10-13 14:00:20 +00:00
ListNode* it;
2022-10-13 14:00:20 +00:00
for(it = head; it->next != ep; it = it->next)
;
it->next = ep->next;
free(ep);
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-13 14:00:20 +00:00
while(it != NULL) {
tmp = it;
it = it->next;
free(tmp);
}
}