This Linked List has two major design goals:
- To have a straightforward and easy-to-use API for users.
- To have a robust and "production-ready" implementation under the hood.
Represents a node in a linked list.
- Properties
data
: int16_t; data stored in the node.next
: Node*; pointer to the next node.
Represents the linked list.
- Properties
head
: Node*; pointer to the first node in the list.
Creates and initializes an empty linked list.
- Returns: A pointer to the newly created linked list.
Inserts a new node at the specified index in the linked list. If the index is out of bounds, the node is appended to the end.
- Parameters:
list
: Pointer to the linked list.index
: Index at which to insert the new node.
Appends a new node with the given data at the end of the linked list.
- Parameters:
list
: Pointer to the linked list.data
: Data to be stored in the new node.
Removes the last node from the linked list.
- Parameters:
list
: Pointer to the linked list.
Removes the node at the specified index from the linked list.
- Parameters:
list
: Pointer to the linked list.index
: Index of the node to be removed.
Deletes the head node of the linked list.
- Parameters:
list
: Pointer to the linked list.
Prints the elements of the linked list.
- Parameters:
list
: Pointer to the linked list.
Returns the number of nodes in the linked list.
- Parameters:
list
: Pointer to the linked list.
- Returns: Size of the linked list.
Checks if the linked list is empty.
- Parameters:
list
: Pointer to the linked list.
- Returns:
1
if the list is empty,0
otherwise.
Frees the memory occupied by the linked list.
- Parameters:
list
: Pointer to the linked list.
#include <stdio.h>
#include <stdint.h>
#include "linked_list.h"
int main() {
LinkedList* myLinkedList = createLinkedList();
appendNode(myLinkedList, 42);
appendNode(myLinkedList, 99);
insertNode(myLinkedList, 1, 55);
deleteHead(myLinkedList);
printf("Linked list length: %zu\n", getLength(myLinkedList));
printLinkedList(myLinkedList);
destroyLinkedList(myLinkedList);
return 0;
}