osurs  0.0.1
Data Structures | Typedefs | Functions
linkedlist.h File Reference

Linkedlist data structure. More...

#include <stddef.h>
Include dependency graph for linkedlist.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ListNode
 Linked list node. More...
 
struct  LinkedList
 Linked list. More...
 

Typedefs

typedef struct ListNode ListNode
 Linked list node. More...
 
typedef struct LinkedList LinkedList
 Linked list. More...
 

Functions

void linked_list_init (LinkedList *list)
 Initialize a LinkedList structure. More...
 
LinkedListlinked_list_create ()
 Create a new LinkedList structure. More...
 
void linked_list_add_first (LinkedList *list, void *data)
 Add an element to the beginning of a LinkedList. More...
 
void linked_list_add_last (LinkedList *list, void *data)
 Add an element to the end of a LinkedList. More...
 
void * linked_list_remove_first (LinkedList *list)
 Remove the first element from a LinkedList. More...
 
void * linked_list_remove_last (LinkedList *list)
 Remove the last element from a LinkedList. More...
 
void * linked_list_get_first (LinkedList *list)
 Get the first element of a LinkedList. More...
 
void * linked_list_get_last (LinkedList *list)
 Get the last element of a LinkedList. More...
 
void linked_list_clear (LinkedList *list)
 Clear all elements from a LinkedList. More...
 
void linked_list_free (LinkedList *list)
 Free a LinkedList structure. More...
 

Detailed Description

Linkedlist data structure.

This file defines a LinkedList data structure, which is an abstract data type (ADT) that implements a doubly linked list. Each node of the list contains a value and two pointers: one pointing to the previous node in the list and one pointing to the next node in the list. This allows for efficient insertion and deletion of elements at the ends of the list, as well as the ability to easily traverse the list in both directions. The head of the list points to the first node, and the tail of the list points to the last node. If the list is empty, both the head and the tail will be null.

Note
Accessing elements via indexes in a doubly linked list is slower, with a time complexity of O(n), compared to an array, which has a time complexity of O(1), since elements in a doubly linked list are not stored consecutively in memory and must be accessed by iterating through the elements.
Date
2022-12-20
Author
Merlin Unterfinger

Typedef Documentation

◆ LinkedList

typedef struct LinkedList LinkedList

Linked list.

This structure represents a LinkedList data structure. It consists of pointers to the head and tail of the list (the first and last nodes in the list), as well as an integer size representing the number of elements in the list.

◆ ListNode

typedef struct ListNode ListNode

Linked list node.

This structure represents a node in a LinkedList data structure. It consists of pointers to the previous and next nodes in the list, as well as a void pointer to the node's data.

Function Documentation

◆ linked_list_add_first()

void linked_list_add_first ( LinkedList list,
void *  data 
)

Add an element to the beginning of a LinkedList.

This function adds an element to the beginning of a LinkedList.

Parameters
listThe LinkedList to which the element will be added.
dataThe element to be added to the list.

◆ linked_list_add_last()

void linked_list_add_last ( LinkedList list,
void *  data 
)

Add an element to the end of a LinkedList.

This function adds an element to the end of a LinkedList.

Parameters
listThe LinkedList to which the element will be added.
dataThe element to be added to the list.

◆ linked_list_clear()

void linked_list_clear ( LinkedList list)

Clear all elements from a LinkedList.

This function removes all elements from a LinkedList, but does not free the list structure itself.

Parameters
listThe LinkedList to be cleared.

◆ linked_list_create()

LinkedList* linked_list_create ( )

Create a new LinkedList structure.

This function creates a new LinkedList structure and returns a pointer to it. The new structure is initialized with its head and tail pointers set to NULL and its size set to 0.

Returns
A pointer to the new LinkedList structure.

◆ linked_list_free()

void linked_list_free ( LinkedList list)

Free a LinkedList structure.

This function frees a LinkedList structure, including all nodes and their data.

Parameters
listThe LinkedList structure to be freed.

◆ linked_list_get_first()

void* linked_list_get_first ( LinkedList list)

Get the first element of a LinkedList.

This function returns a pointer to the first element of a LinkedList, without removing it from the list. If the list is empty, the function returns NULL.

Parameters
listThe LinkedList from which the first element will be returned.
Returns
The first element of the list, or NULL if the list is empty.

◆ linked_list_get_last()

void* linked_list_get_last ( LinkedList list)

Get the last element of a LinkedList.

This function returns a pointer to the last element of a LinkedList, without removing it from the list. If the list is empty, the function returns NULL.

Parameters
listThe LinkedList from which the last element will be returned.
Returns
The last element of the list, or NULL if the list is empty.

◆ linked_list_init()

void linked_list_init ( LinkedList list)

Initialize a LinkedList structure.

This function initializes a LinkedList structure by setting its head and tail pointers to NULL and its size to 0.

Parameters
listThe LinkedList structure to be initialized.

◆ linked_list_remove_first()

void* linked_list_remove_first ( LinkedList list)

Remove the first element from a LinkedList.

This function removes the first element from a LinkedList and returns a pointer to it. If the list is empty, the function returns NULL.

Parameters
listThe LinkedList from which the element will be removed.
Returns
The first element of the list, or NULL if the list is empty.

◆ linked_list_remove_last()

void* linked_list_remove_last ( LinkedList list)

Remove the last element from a LinkedList.

This function removes the last element from a LinkedList and returns a pointer to it. If the list is empty, the function returns NULL.

Parameters
listThe LinkedList from which the element will be removed.
Returns
The last element of the list, or NULL if the list is empty.