Insert node at beginning of linked list in c++

Write a program which is create and add new node at the beginning of linked list in c++.

insert node at beginning of linked list

Process to add node : We need to creating a new node. So we can create new node of linked list by using of structure or class.

 // Using structure
struct LinkNode
{
    int data;
    struct LinkNode *next;
};

// Using class
class LinkNode
{
    public: 
    int data;
    LinkNode *next;
    // Constructor which is set node value
    LinkNode(int data)
    {
        this->data = data;
        this->next = NULL;
    }
};

We are follows class concept in c++ to create a new node.

1) First step create a dynamic linked list node and assign data and next pointer field value (We Assume that there are no memory overflow problem).

2) When linked list are empty so in this situation assign the reference of this create node to head pointer of linked list.

3) When linked list are not empty then do first steps. After that this created node pointer field are assigned the address of front node of linked list. And head pointer is assigned the address of newly create node.

Here given efficient program which is add new node at the front in linked list.

// Include header file
#include <iostream>
using namespace std;
/*
    C++ program for
    Insert node at beginning of linked list
*/
// Linked list node
class LinkNode
{
    public: int data;
    LinkNode *next;
    LinkNode(int data)
    {
        this->data = data;
        this->next = NULL;
    }
};
class SingleLL
{
    public: LinkNode *head;
    SingleLL()
    {
        this->head = NULL;
    }
    // Adding new node at beginning of linked list
    void addNode(int data)
    {
        // Create new node
        LinkNode *node = new LinkNode(data);
        // Connect current node to previous head
        node->next = this->head;
        // Make new head
        this->head = node;
    }
    // Display linked list element
    void display()
    {
        if (this->head == NULL)
        {
            return;
        }
        LinkNode *temp = this->head;
        // iterating linked list elements
        while (temp != NULL)
        {
            cout << " " << temp->data << " →";
            // Visit to next node
            temp = temp->next;
        }
        cout << " NULL\n";
    }
};
int main()
{
    SingleLL *sll = new SingleLL();
    // Linked list
    // 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
    sll->addNode(8);
    sll->addNode(7);
    sll->addNode(6);
    sll->addNode(5);
    sll->addNode(4);
    sll->addNode(3);
    sll->addNode(2);
    sll->addNode(1);
    sll->display();
    return 0;
}
Visualize the insert node beginning of singly linked list
 Linked List
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL

Time complexity of above program is O(1).



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved