Program To Add A Node In Beginning Of Singly Linked List

Description

To add node in the beginning of the list - 
case 1: When list is empty
	create a node and assign the address of newly created node to the head
Case 2: When list has few elements
	step 1 : create a new node 
	step 2 : Assign address of head to the next field of newly created node
	step 3 : Assign the address of newly created node to head
	
Note: Here head contains the address of first node

C/C++

/* C program of Program To Add a node in the beginning of singly linked list */
//Save it as add_node_beginning_singly_linked_list.c

#include<stdio.h>
#include<malloc.h>
struct node *create(int);
void display(struct node *);
struct node *insert_beg(struct node *);


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

int main()
{
    int n;
    struct node *head;

    printf("Enter number of nodes : ");
    scanf("%d",&n);
    
    //Calling function to create node
    head=create(n);
    
    //Calling function to display list
    printf("\nThe linked list is - \n");
    display(head);

    
    //Function to insert the node at beginning of linked list
    head=insert_beg(head);
    
    //Calling function to display list after addition of node at beginning
    printf("\nThe linked list after addition of node at beginning-\n");
    display(head);
    return 0;
}

struct node *create(int n)
{
    int i;
    struct node *head=NULL;
    struct node *ptr,*newNode;
    for(i=0;i<n;i++)
    {
        
        //Creating a node
        newNode=(struct node*)malloc(sizeof(struct node));
        
        //Assigning data to newly created node
        printf("Enter the value of %d node : ",i+1);
        scanf("%d",&newNode->data);
        newNode->next=NULL;
        
        /*If list is empty assign the address of newly created node
        to head*/
        if(head==NULL)
        {
            head=newNode;
        }
        else
        {
            
            /* If list already have few elements then loop through 
            list and add newly created node at the end of list*/
            ptr=head;
            while(ptr->next!=NULL)
            {
                ptr=ptr->next;
            }
            ptr->next=newNode;
        }
    }
    return head;
}

void display(struct node *head)
{
    struct node *ptr;
    
    //If list is empty
    if(head==NULL)
    {
        printf("The linked list is empty");
    }
    else
    {
        
        /*If list has elements then loop through the loop and 
        print elements one by one in sequential manner*/
        ptr=head;
        while(ptr!=NULL)
        {
            printf("The node value = %d and their address = %u\n",ptr->data,ptr);
            ptr=ptr->next;
        }
    }
}


//Function to insert a node in the beginning
struct node *insert_beg(struct node *head){
    int data;
    struct node *ptr, *newNode;
    
        //Creating new node to be added
    newNode=(struct node*)malloc(sizeof(struct node));
    printf("\nEnter the node data of new node : ");
    scanf("%d",&newNode->data);
    newNode->next=NULL;
    
    //assign the address of newly created node to the head if list is empty
    if(head==NULL){
        head=newNode;
    }else{
           
        /*Assign the address of head to next field of newly created node*/
        newNode->next=head;
        
        /*Assign the address of newly created node to the head*/
        head=newNode;
    }
    return head;
}
Input:
Enter number of nodes : 5
Enter the value of 1 node : 4
Enter the value of 2 node : 1
Enter the value of 3 node : 9
Enter the value of 4 node : 6
Enter the value of 5 node : 3

The linked list is -
The node value = 4 and their address = 7015768
The node value = 1 and their address = 7015800
The node value = 9 and their address = 7015816
The node value = 6 and their address = 7015832
The node value = 3 and their address = 7015848

Enter the node data of new node : 7

Output:
The linked list after addition of node at beginning-
The node value = 7 and their address = 7015864
The node value = 4 and their address = 7015768
The node value = 1 and their address = 7015800
The node value = 9 and their address = 7015816
The node value = 6 and their address = 7015832
The node value = 3 and their address = 7015848

Java

/* Java program of Program To Add a node in the beginning of singly linked list */
//Save it as AddNodeBeginningSinglyLinkedList.java

import java.io.*;
import java.util.Scanner;

public class AddNodeBeginningSinglyLinkedList {

    
        //Declaring node
    class Node{
        int data;
        Node next;
        public Node(int data) {
            this.data=data;
            
            //Assigning null value to the next field
            this.next=null;
        }
    }
    
    public Node head=null;
    
    
    //Function to add node
    public void addNode(int data) {
        
        //Creating a new node
        Node newNode = new Node(data);
        
                /*If list is empty assign the address of newly created node
        to head*/
        if(head==null) {
            head=newNode;
        }else {
            
                    /* If list already have few elements then loop through 
            list and add newly created node at the end of list*/
            Node ptr=head;
            while(ptr.next!=null) {
                ptr=ptr.next;
            }
            ptr.next=newNode;
        }
    }
    
    public void display() {
        Node ptr=head;
        
                //If list is empty
        if(ptr==null) {
            System.out.println("The list is empty");
            return;
        }
        System.out.println("The nodes of lists are : ");
        
             /*If list has elements then loop through the loop and 
               print elements one by one in sequential manner*/
        while(ptr!=null) {
            System.out.print(ptr.data+" ");
            ptr=ptr.next;
        }
    }
    
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        AddNodeBeginningSinglyLinkedList list = new AddNodeBeginningSinglyLinkedList();
        
        System.out.println("Enter number of node you want to enter : ");
        int n = scanner.nextInt();
        
        for(int i=0;i<n;i++) {
            System.out.println("Enter value of "+(i+1)+" node : ");
            int val = scanner.nextInt();
            list.addNode(val);
        }
        
                //Calling function to display list
        list.display();
        
        System.out.println("\nEnter the value you want to enter at beginning : ");
        int beg = scanner.nextInt();
        list.insert_beg(beg);
        
        
                //Calling function to display list after addition of node in the beginning
        System.out.println("After addition of node in the beginning - ");
        list.display();
    }

    
    //Function to add node in the beginning
    private void insert_beg(int beg) {
        
        //Creating new node to be added
        Node nodeToAdd = new Node(beg);
        
        //assign the address of newly created node to the head if list is empty
        if(head==null) {
            head=nodeToAdd;
        }else {
            
            /*Assign the address of head to next field of newly created node*/
            nodeToAdd.next=head;
            
            /*Assign the address of newly created node to the head*/
            head=nodeToAdd;
        }
    }
}
Input:
Enter number of node you want to enter : 
3
Enter value of 1 node : 
4
Enter value of 2 node : 
1
Enter value of 3 node : 
9
The nodes of lists are : 
4 1 9 
Enter the value you want to enter at beginning : 
8

Output:
After addition of node in the beginning - 
The nodes of lists are : 
8 4 1 9

Related Programs

1) Program To Create And Display Singly Linked List
2) Program To Add A Node In The End Of Singly Linked List
3) Program To Add A Node Before A Given Node Singly Linked List
4) Program To Add A Node After A Given Node Singly Linked List
5) Program To Delete Node From Beginning Singly Linked List
6) Program To Delete Node From End Singly Linked List
7) Program To Delete Given Node Singly Linked List
8) Program To Delete Node Before Given Node Singly Linked List
9) Program To Delete Node After Given Node Singly Linked List
10) Program To Delete All Nodes Of Singly Linked List
11) Program To Sort Elements Of Singly Linked List
12) Program To Reverse Elements Singly Linked List
13) Program to Find Maximum And Minimum Value Node In Singly Linked List
14) Program to Search an Element In Singly Linked List
15) Program To Remove Duplicate Elements From Singly Linked List
16) Program To Create And Display Circular Singly Linked List
17) Program To Add A Node At The Beginning Of Circular Linked List
Share Me

Leave a Reply