clox/include/doubly-linked-list.h

55 lines
1.4 KiB
C
Raw Permalink Normal View History

2024-11-18 05:16:17 +00:00
struct dll_node {
struct dll_node *previous;
struct dll_node *next;
char *value;
};
typedef struct dll_node dll_node;
// TODO: I probably don't need a separate "list" type
2024-11-18 05:16:17 +00:00
typedef struct {
struct dll_node *head;
} dll_list;
/**
* Allocates a new (empty) doubly linked list
*/
dll_list *dll_list_new();
/**
* Frees the memory used by a list, including all of its child nodes
*/
void dll_list_free(dll_list *list);
/**
* Inserts a given node at the given index of the given list. If index is -1
* or is greater than the size of the list, then the node will be inserted at the end of the list
*/
void dll_list_insert(dll_list *list, dll_node *node, int index);
/**
* Appends a node to the end of a given list
*/
void dll_list_append(dll_list *list, dll_node *node) {
dll_list_insert(list, node, -1);
}
/**
* Searches the given list for a node with a given value
* @returns a pointer to the node with the value or NULL if no nodes match the value
*/
dll_node *dll_list_find(dll_list *list, char *value);
/**
* Removes the first node with the matching value in the given list
* @returns a pointer to the node with the value or NULL if no nodes match the value
*/
dll_node *dll_list_delete(dll_list *list, char *value);
2024-11-18 05:16:17 +00:00
dll_node *dll_node_new(char *value);
/**
* Frees the memory for a given node, returning a pointer to the next node, if any
*/
dll_node *dll_node_free(dll_node *node);