DSA Hacking Club Library
Loading...
Searching...
No Matches
linkedlist.h File Reference

A simple singly linked list implementation in C. More...

#include <stdio.h>
Include dependency graph for linkedlist.h:

Go to the source code of this file.

Classes

struct  node
 A node in the singly linked list. More...

Typedefs

typedef struct node node_t
 A node in the singly linked list.

Functions

void printlist (node_t *head)
 Prints all the values in the linked list.
node_tcreate_new_node (int value)
 Creates a new node with a given integer value.
node_tinsert_at_head (node_t **head, node_t *node_to_insert)
 Inserts a node at the head of the list.
node_tfind_node (node_t *head, int value)
 Finds the first node with a given value.
void insert_after_node (node_t *node_to_insert_after, node_t *newnode)
 Inserts a new node after a given node.
void free_list (node_t *head)
 Frees all nodes in the linked list.

Detailed Description

A simple singly linked list implementation in C.

Author
Alifoo
Date
2025-09-25

This file provides the public interface for a linked list of integers. The list supports insertion, search, printing, memory management, and basic node operations.

Typedef Documentation

◆ node_t

typedef struct node node_t

A node in the singly linked list.

Each node stores an integer value and a pointer to the next node.

Function Documentation

◆ create_new_node()

node_t * create_new_node ( int value)

Creates a new node with a given integer value.

Allocates memory for a new node, sets its value, and initializes its next pointer to NULL.

Parameters
[in]valueThe integer value to store in the new node.
Returns
Pointer to the newly created node.

◆ find_node()

node_t * find_node ( node_t * head,
int value )

Finds the first node with a given value.

Traverses the list to search for a node containing the specified value.

Parameters
[in]headPointer to the head node of the list.
[in]valueThe integer value to search for.
Returns
Pointer to the node if found, otherwise NULL.

◆ free_list()

void free_list ( node_t * head)

Frees all nodes in the linked list.

Iterates through the list, freeing each node’s allocated memory. After calling this function, the list is no longer valid.

Parameters
[in]headPointer to the head node of the list.

◆ insert_after_node()

void insert_after_node ( node_t * node_to_insert_after,
node_t * newnode )

Inserts a new node after a given node.

Updates pointers so that the new node follows the specified node.

Parameters
[in]node_to_insert_afterPointer to the node after which to insert.
[in]newnodePointer to the new node to insert.

◆ insert_at_head()

node_t * insert_at_head ( node_t ** head,
node_t * node_to_insert )

Inserts a node at the head of the list.

Updates the head pointer to point to the new node.

Parameters
[in,out]headPointer to the head pointer of the list.
[in]node_to_insertPointer to the node to insert.
Returns
Pointer to the inserted node.

◆ printlist()

void printlist ( node_t * head)

Prints all the values in the linked list.

Iterates from the head of the list to the end, printing each value.

Parameters
[in]headPointer to the head node of the list.