Insertion at Beginning in Singly Linked List in C++

How to perform insertion at the beginning in Linked List in C++?

Insertion at Beginning in Linked List in C++ is one of the operations that we can perform on Linked List. linked list in C++ is part of a linked list and is a type of linear data structure. Linked is made up of two parts node and pointer where node contains the data and pointer contains the address of the next node.

Insertion at begin in Singly linked list in C++ programming

Linked list definition in C++

Nodes of a linked list are created by using the code mentioned besides.
This set of codes will construct a linked list by creating each node of the list.

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

Steps to insert an element at beginning of linked list

  • Allocate space for a new node.
    • ptr = (struct node *) malloc(sizeof(struct node *));
  • In the space created for new node put the data in.
    • new_node->data = new_data
  • Point the pointer of new node to the head of the linked list
    • new_node->ptr = (*head);
  • Make new node as head
    • (*head) = new_node
  •  
Insertin at beginning in singly linked list in C++

C++ program to insert an element at beginning of linked list

Using non-member functions and passing head explicitly

Run
#include<iostream>
using namespace std;

class Node
{
    public:
    int data;
    Node *next;
};

void insertFront(Node** head, int data){

    Node* new_node = new Node();

    // assign data
    new_node->data = data;
    
    // change the next node of this newNode 
    // to current head of Linked List
    new_node->next = *head;

    // new_node should become the new head of Linked List
    *head = new_node;
    
    cout << "Inserted Item: " << new_node->data << endl;
}

void printList(Node* node){
    
    cout << "\nLinked List : " ;
    
    //as linked list will end when Node is Null
    while(node!=NULL){
        cout << node->data << " "; 
        node = node->next;
    }
    cout << endl;
}

int main(){

    Node* head = NULL;

    insertFront(&head,4);
    insertFront(&head,5);
    insertFront(&head,6);
    insertFront(&head,7);
    insertFront(&head,8);
    insertFront(&head,9);

    printList(head); 
    
    return 0;  
}

Output

Inserted Item: 4
Inserted Item: 5
Inserted Item: 6
Inserted Item: 7
Inserted Item: 8
Inserted Item: 9

Linked List : 9 8 7 6 5 4

Using member functions and using head member variable

Run
#include<iostream>
using namespace std;

class Node
{
    public:
    int data;
    Node *next;
};

class LinkedList
{
    private:
        Node* head;
    public:
        LinkedList() { // constructor
        head = NULL;
    }
        void insertFront(int data);
        void printList();
};

void LinkedList::insertFront(int data){

    Node* new_node = new Node();
    
    // assign data value
    new_node->data = data;
    // change the next node of this new_node 
    // to current head of Linked List
    new_node->next = head;

    // new_node should become the new head of Linked List
    head = new_node;
    
    cout << "Inserted Item: " << new_node->data << endl;
}

void LinkedList::printList(){
    
    Node* node = new Node();
    node = head;
    
    cout << "\nLinked List : " ;
    
    //as linked list will end when Node is Null
    while(node!=NULL){
        cout << node->data << " "; node = node->next;
    }
    cout << endl; } int main() { LinkedList* new_list = new LinkedList(); new_list->insertFront(4);
    new_list->insertFront(5);
    new_list->insertFront(6);
    new_list->insertFront(7);
    new_list->insertFront(8);
    new_list->insertFront(9);

    new_list->printList();
    
    return 0;  
}

Output

Inserted Item: 4
Inserted Item: 5
Inserted Item: 6
Inserted Item: 7
Inserted Item: 8
Inserted Item: 9

Linked List : 9 8 7 6 5 4 

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java