linked_list: Initial implementation
This commit is contained in:
parent
c35b2d2d88
commit
4afe674e54
2 changed files with 49 additions and 0 deletions
33
common/linked_list.c
Normal file
33
common/linked_list.c
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "linked_list.h"
|
||||||
|
|
||||||
|
void linked_list_init(struct linked_list *list) {
|
||||||
|
list->next = list;
|
||||||
|
list->prev = list;
|
||||||
|
}
|
||||||
|
|
||||||
|
void linked_list_insert(struct linked_list *list, struct linked_list *elem) {
|
||||||
|
assert(list->prev != NULL && list->next != NULL);
|
||||||
|
assert(elem->prev == NULL && elem->next == NULL);
|
||||||
|
|
||||||
|
elem->prev = list;
|
||||||
|
elem->next = list->next;
|
||||||
|
list->next = elem;
|
||||||
|
elem->next->prev = elem;
|
||||||
|
}
|
||||||
|
|
||||||
|
void linked_list_remove(struct linked_list *elem) {
|
||||||
|
assert(elem->prev != NULL && elem->next != NULL);
|
||||||
|
|
||||||
|
elem->prev->next = elem->next;
|
||||||
|
elem->next->prev = elem->prev;
|
||||||
|
elem->next = NULL;
|
||||||
|
elem->prev = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool linked_list_empty(struct linked_list *list) {
|
||||||
|
return list->next == list;
|
||||||
|
}
|
16
include/linked_list.h
Normal file
16
include/linked_list.h
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef _LINKED_LIST_H
|
||||||
|
#define _LINKED_LIST_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
struct linked_list {
|
||||||
|
struct linked_list *prev;
|
||||||
|
struct linked_list *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
void linked_list_init(struct linked_list *list);
|
||||||
|
void linked_list_insert(struct linked_list *list, struct linked_list *elem);
|
||||||
|
void linked_list_remove(struct linked_list *elem);
|
||||||
|
bool linked_list_empty(struct linked_list *list);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Add table
Add a link
Reference in a new issue