C Linked List

A linked list is a linear data structure in which elements, called nodes, are stored in separate memory locations and connected using pointers.

Unlike arrays, the elements of a linked list are not stored in contiguous memory. Each node contains:

  1. Data – the value stored in the node.
  2. Pointer (link) – the address of the next node in the list.

Types of Linked Lists

  1. Singly – each node points to the next node, last node points to NULL.
  2. Doubly – each node has two pointers: one to the next node, one to the previous node.
  3. Circular – the last node points back to the first node, forming a loop.

Single Linked List

/* Include Files */
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};

int count_tot_no_nodes(struct node *address)
{
    int count=0;
    if(address->link==NULL)
    {
        printf("Linked List is empty.\n\r");
        return count;
    }
    else
    {
        while(address->link!=NULL)
        {
            count++;
            address = address->link;
        }
        return ++count;
    }
}

void printLinkedListData(struct node *Address)
{
    int count=1;

    if(Address->link== NULL)
    {
        printf("\nNode %d - %x",count++, Address->data);
    }
    else
    {

        while(Address->link!= NULL)
        {
            printf("\nNode %d - %x",count++, Address->data);
            Address = Address->link;
        }

        printf("\nNode %d - %x",count++, Address->data);
        Address = Address->link;
    }
}

void initNode()
{
    struct node *head;
    int b =sizeof(struct node);
    head = (struct node *)malloc(b);

    head->data = 0x55AA55AA;
    head->link = head;
}

void add_at_end_node(struct node *address, int data)
{
    struct node *end_node = (struct node *)malloc(sizeof(struct node));
    end_node->data = data;
    end_node->link = NULL;

    while(address->link != NULL)
    {
        address = address->link;
    }
    address->link = end_node;
}

void add_at_any_position(struct node *address, int data, int pos)
{
    struct node *temp_node = (struct node *)malloc(sizeof(struct node));
    temp_node->data = data;
    temp_node->link = NULL;

    while(pos--)
    {
        address=address->link;
    }
    temp_node->link= address->link;
    address->link = temp_node;
}

void delete_node(struct node *address,int pos)
{
    struct node *previous;

    pos--;
    while(pos--)
    {
        previous=address;
        address=address->link;
    }
    struct node *temp = address;
    previous->link = address->link;
    free(temp);
}

void delete_entire_linkedList(struct node *address)
{
    struct node *temp;

    while(address->link != NULL)
    {
        temp=address;
        address= address->link;
        free(temp);
    }
    free(address);
}


int main(void)
{
    printf("\n Welcome to ArunEworld");
    printf("\n Single Linked List Example");
    struct node *head;
    int b =sizeof(struct node);
    head = (struct node *)malloc(b);

    head->data = 0xAAAAAAAA;
    head->link = head;

    struct node *current = (struct node *)malloc(b);
    current->data = 0xBBBBBBBB;
    current->link = NULL;
    head->link= current;

    current = (struct node *)malloc(b);
    current->data = 0xCCCCCCCC;
    current->link = NULL;
    head->link->link= current;

    printf("\nLinked List counting - %d",count_tot_no_nodes(head));
    printLinkedListData(head);

    add_at_end_node(head,0xDDDDDDDD);

    printf("\nLinked List counting - %d",count_tot_no_nodes(head));
    printLinkedListData(head);

    add_at_end_node(head,0xEEEEEEEE);
    printf("\nLinked List counting - %d",count_tot_no_nodes(head));
    printLinkedListData(head);


    add_at_any_position(head, 0xFFFFFFFF, 2);
    printf("\nLinked List counting - %d",count_tot_no_nodes(head));
    printLinkedListData(head);

    add_at_any_position(head, 0xBBBBBBBB, 2);
   printf("\nLinked List counting - %d",count_tot_no_nodes(head));
   printLinkedListData(head);


   delete_node(head,2);
   printf("\nLinked List counting - %d",count_tot_no_nodes(head));
   printLinkedListData(head);

   delete_node(head,4);
   printf("\nLinked List counting - %d",count_tot_no_nodes(head));
   printLinkedListData(head);

    printf("\nLinked List counting - %d",count_tot_no_nodes(head));
    printLinkedListData(head);

    delete_entire_linkedList(head);
    printf("\nLinked List counting - %d",count_tot_no_nodes(head));
    free(head);
    printLinkedListData(head);

    printf("\n____________________________________________");
    return 0;
}

Functions

  1. count_tot_no_nodes(struct node *address)
    • Counts how many nodes exist in the linked list.
    • Logic flaw: Your “empty” condition checks if(address->link==NULL), but that’s not accurate. If head exists but has no link, it’s still a single-node list.
  2. printLinkedListData(struct node *Address)
    • Prints the data in each node.
    • Logic repeats printing the last node unnecessarily — could be simplified into one loop.
  3. initNode()
    • Creates a single self-referencing node.
    • Not used in main().
  4. add_at_end_node()
    • Appends a new node at the end.
  5. add_at_any_position()
    • Inserts a node after a certain position (0-based index not clearly handled).
    • No bounds checking — could cause crashes.
  6. delete_node()
    • Deletes a node at a given position.
    • Can fail if position is 1 (first node) because previous is uninitialized.
  7. delete_entire_linkedList()
    • Frees all nodes.

Main Flow

  • Creates a head node and links two more nodes manually.
  • Performs:
    • Counting nodes
    • Printing
    • Adding at end
    • Adding at any position
    • Deleting nodes
    • Deleting the entire list

Previous & Next

Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off