Linked List in C (original) (raw)

Last Updated : 24 Jan, 2026

A linked list is a linear data structure made of nodes connected using pointers. Each node has: Data: The value stored in the node. Pointer: A reference to the next node.

#include <stdio.h> #include <stdlib.h>

// Define the node structure struct Node { int data; struct Node *next; };

int main() { // Create nodes struct Node *head = NULL; struct Node *second = NULL; struct Node *third = NULL;

// Allocate memory
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));

// Check allocation
if (!head || !second || !third) {
    printf("Memory allocation failed\n");
    return 1;
}

// Assign data and link nodes
head->data = 10;
head->next = second;

second->data = 20;
second->next = third;

third->data = 30;
third->next = NULL;

// Print linked list
struct Node *temp = head;
while (temp != NULL)
{
    printf("%d -> ", temp->data);
    temp = temp->next;
}
printf("NULL\n");

// Free allocated memory
temp = head;
while (temp != NULL)
{
    struct Node *next = temp->next;
    free(temp);
    temp = next;
}

return 0;

}

`

Output

10 -> 20 -> 30 -> NULL

Types of Linked List in C

Linked list can be classified on the basis of the type of structure they form as a whole and the direction of access. Based on this classification, there are five types of linked lists:

Singly Linked List in C

singly-linked-list-in-c

Doubly Linked List

doubly-linked-list-in-c

Basic Operations on Doubly Linked List

Operation Operation Type Description Time Complexity Space Complexity
Insertion At Beginning Insert a new node at the start of a linked list. O (1) O (1)
At the End Insert a new node at the end of the linked list. O (N) O (1)
At Specific Position Insert a new node at a specific position in a linked list. O (N) O (1)
Deletion From Beginning Delete a node from the start of a linked list O (1) O (1)
From the End Delete a node at the end of a linked list. O (N) O (1)
A Specific Node Delete a node from a specific position of a linked list. O (N) O (1)
Traversal Traverse the linked list from start to end or vice versa. O (N) O (1)

Circular Linked List

A circular linked list is a variation of a singly linked list where the last node points back to the first node, forming a circle. This means there is no NULL at the end, and the list can be traversed in a circular manner.

Circular-linked-list-in-c

**Note: A circular linked list can also be represented by a pointer to the last node.

Basic Operations on Circular Linked List

Operation Operation Type Description Time Complexity Space Complexity
Insertion At Beginning Insert a new node at the start of a linked list. O (N) O (1)
At the End Insert a new node at the end of the linked list. O (N) O (1)
At Specific Position Insert a new node at a specific position in a linked list. O (N) O (1)
Deletion From Beginning Delete a node from the start of a linked list O (N) O (1)
From the End Delete a node at the end of a linked list. O (N) O (1)
A Specific Node Delete a node from a specific position of a linked list. O (N) O (1)
Traversal Traverse the linked list from start to end or vice versa. O (N) O (1)

Memory Management in Linked List

Memory management is the process of efficiently allocating, using, and freeing memory in a program so that the system runs smoothly without wasting resources or causing errors.

Dynamic Memory Allocation using malloc().

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

Dynamic Memory Allocation using calloc().

struct Node* newNode = (struct Node*)calloc(1, sizeof(struct Node));

Freeing Memory with free().

free(nodeToDelete);
nodeToDelete = NULL;

Applications of Linked Lists

The following are some major applications of linked list:

Advantages of a Linked List

The following points shows the advantages of linked list:

Disadvantages of a Linked List

Disadvantages of linked list are mentioned below: