Singly Linked List Insertion at beginning in C

Insertion in singly linked list at the beginning

C Program for insertion at the beginning of the Singly Linked List. A Singly linked list is made up of many nodes which are connected. Every node is mainly divided into two parts, one part holds the data and the other part is the link that connects to the next node

Insertion at beginning in singly linked list using C

Singly Linked List allow insertion at the following places:-

  • Insertion at the start of the singly Linked List
  • Insertion at the end of the singly linked list.
  • Insertion at the nth node of the singly linked list.
Insertion at the beginning of Singly Linked List

Initialization of singly linked list in C:-

struct node
{
    int data;
    struct node *next;
};

C Program for Insertion at the Beginning of the Singly Linked List:-

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

struct Node{
    int data;
    struct Node *next;
};


void insertBeginning(struct Node** head, int data){
    
    // dynamically create memory for this newNode
    struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
    
    // assign data value
    newNode->data = data;
    // change the next node of this newNode 
    // to current head of Linked List
    newNode->next = *head;

    //re-assign head to this newNode
    *head = newNode;
    printf("%d Insertion Successful\n",newNode->data);
}

void printLinkedList(struct Node* node)
{
    printf("\nLinked List: ");
    
    // as linked list will end when Node is Null
    while(node!=NULL){
        printf("%d ",node->data);
        node = node->next;
    }
    
    printf("\n");
}

int main()
{
    struct Node* head = NULL;

    // Use '&' i.e. address as we need to change head address
    insertBeginning(&head,100);
    insertBeginning(&head,80);
    insertBeginning(&head,60);
    insertBeginning(&head,40);
    insertBeginning(&head,20);
    
    // Don't use '&' as not changing head in printLinkedList operation
    printLinkedList(head);
    
    
    return 0; 
}

Output

100 Insertion Successful
80 Insertion Successful
60 Insertion Successful
40 Insertion Successful
20 Insertion Successful

Linked List: 20 40 60 80 100
Run
#include<stdio.h>
#include<stdlib.h>

struct Node{
    int data;
    struct Node *next;
};


void insertBeginning(struct Node** head, int data){
    
    // dynamically create memory for this newNode
    struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
    
    // assign data value
    newNode->data = data;
    
    // change the next node of this newNode 
    // to current head of Linked List
    newNode->next = *head;

    //re-assign head to this newNode
    *head = newNode;
    printf("%d Insertion Successful\n",newNode->data);
}

void printLinkedList(struct Node* node)
{
    printf("\nLinked List: ");
    
    // as linked list will end when Node is Null
    while(node!=NULL){
        printf("%d ",node->data);
        node = node->next;
    }
    
    printf("\n");
}

int main()
{
    struct Node* head = NULL;
    int input, item;
    
    do{
        printf ("\nEnter the item to insert?\n");
        scanf ("%d", &item);
        
        // Use '&' i.e. address as we need to change head address
        insertBeginning(&head, item);
        
        printf ("\nPress 0: To Exit\n");
        printf ("Press 1: To insert more items\n");
        scanf ("%d", &input);
    }
    while (input == 1);
    
    // Don't use '&' as not changing head in printLinkedList operation
    printLinkedList(head);
    
    return 0; 
}

Output

Enter the item to insert?
10
10 Insertion Successful

Press 0: To Exit
Press 1: To insert more items
1

Enter the item to insert?
20
20 Insertion Successful

Press 0: To Exit
Press 1: To insert more items
1

Enter the item to insert?
30
30 Insertion Successful

Press 0: To Exit
Press 1: To insert more items
0

Linked List: 30 20 10