Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Linked List

A singly linked list where each node holds an integer and a pointer to the next node.

Interface

typedef struct LinkedList {
    int data;
    struct LinkedList *next;
} linked_list_t;

/**
 * Create a new single-element linked list containing @p element.
 *
 * @return Pointer to the new node, or NULL on allocation failure.
 *         The caller must free the list with linked_list_destroy().
 */
linked_list_t *linked_list_create(int element);

/**
 * Append @p element to the end of @p list.
 * @return Pointer to the head of the list, or NULL on allocation failure.
 */
linked_list_t *linked_list_append(linked_list_t *list, int element);

/**
 * Delete the node @p element from @p list.
 * @return Pointer to the head of the modified list.
 */
linked_list_t *linked_list_delete(linked_list_t *list, const linked_list_t *element);

/**
 * Find the first node containing @p element.
 * @return Pointer to the matching node, or NULL if not found.
 */
linked_list_t *linked_list_find(linked_list_t *list, int element);

/**
 * Destroy the entire linked list and free all nodes.
 */
void linked_list_destroy(linked_list_t *list);

Implementation

//
// Created by moamen on 3/14/26.
//

#include <stddef.h>

#include "data_structures.h"

#include <stdlib.h>

linked_list_t *linked_list_create(const int element)
{
    linked_list_t *list = malloc(sizeof(linked_list_t));
    list->data = element;
    list->next = NULL;
    return list;
}

linked_list_t *linked_list_append(linked_list_t *list, const int element)
{
    if (list == NULL) {
        return NULL;
    }
    linked_list_t *new_list = malloc(sizeof(linked_list_t));
    new_list->data = element;
    new_list->next = NULL;

    linked_list_t *last = list;
    while (last->next != NULL) {
        last = last->next;
    }
    last->next = new_list;
    return new_list;
}

linked_list_t *linked_list_delete(linked_list_t *list, const linked_list_t *element)
{
    if (list == NULL) {
        return NULL;
    }
    linked_list_t *prev = NULL;
    linked_list_t *current = NULL;

    current = list;
    if (current == element) {
        if (current->next == NULL) {
            free(current);
            return NULL;
        }
        list = current->next;
        free(current);
        return list;
    }

    prev = current;
    current = current->next;
    while (current != NULL) {
        if (current == element) {
            prev->next = current->next;
            free(current);
            return list;
        }
        prev = current;
        current = current->next;
    }
    return list;
}

linked_list_t *linked_list_find(linked_list_t *list, const int element)
{
    if (list == NULL) {
        return NULL;
    }

    linked_list_t *current = list;
    while (current != NULL) {
        if (current->data == element) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

void linked_list_destroy(linked_list_t *list)
{
    if (list == NULL) {
        return;
    }
    linked_list_t *current = list;
    linked_list_t *next = NULL;
    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }
}

Complexity

OperationTime
CreateO(1)
AppendO(n)
DeleteO(n)
FindO(n)