Linked List

Singly linked list implementation with common operations

c (C11) 2025-11-12 linked-list data-structure collections list

Description

A generic singly linked list implementation with operations for insertion, deletion, traversal, and searching.

Features

  • Insert at head/tail
  • Delete by value
  • Search
  • Traversal

Code

RAW
#ifndef LINKED_LIST_H#define LINKED_LIST_H#include <stdlib.h>typedef struct Node {    int data;    struct Node* next;} Node;typedef struct LinkedList {    Node* head;    int size;} LinkedList;LinkedList* list_create() {    LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));    if (list) {        list->head = NULL;        list->size = 0;    }    return list;}void list_insert_head(LinkedList* list, int data) {    if (list == NULL) return;        Node* new_node = (Node*)malloc(sizeof(Node));    if (new_node) {        new_node->data = data;        new_node->next = list->head;        list->head = new_node;        list->size++;    }}void list_insert_tail(LinkedList* list, int data) {    if (list == NULL) return;        Node* new_node = (Node*)malloc(sizeof(Node));    if (new_node) {        new_node->data = data;        new_node->next = NULL;                if (list->head == NULL) {            list->head = new_node;        } else {            Node* current = list->head;            while (current->next != NULL) {                current = current->next;            }            current->next = new_node;        }        list->size++;    }}int list_delete(LinkedList* list, int data) {    if (list == NULL || list->head == NULL) return 0;        if (list->head->data == data) {        Node* temp = list->head;        list->head = list->head->next;        free(temp);        list->size--;        return 1;    }        Node* current = list->head;    while (current->next != NULL) {        if (current->next->data == data) {            Node* temp = current->next;            current->next = current->next->next;            free(temp);            list->size--;            return 1;        }        current = current->next;    }        return 0;}int list_search(LinkedList* list, int data) {    if (list == NULL) return 0;        Node* current = list->head;    while (current != NULL) {        if (current->data == data) return 1;        current = current->next;    }    return 0;}void list_free(LinkedList* list) {    if (list == NULL) return;        Node* current = list->head;    while (current != NULL) {        Node* temp = current;        current = current->next;        free(temp);    }    free(list);}#endif
RAW
#include "linked_list.h"#include <stdio.h>int main() {    LinkedList* list = list_create();        list_insert_head(list, 10);    list_insert_tail(list, 20);    list_insert_tail(list, 30);        printf("Size: %d\n", list->size);    printf("Found 20: %d\n", list_search(list, 20));        list_delete(list, 20);    printf("Size after delete: %d\n", list->size);        list_free(list);    return 0;}

Comments

No comments yet. Be the first to comment!