Home > database >  LinkedList Insert Issue
LinkedList Insert Issue

Time:04-19

I was trying to code insert in linkedlist for position 0 (i.e. the beginning of the linked list) and for other positions (like in between any two nodes and at the end of the linkedlist). The code is given below. But it seems to be not working as expected. Please let me know as to what I am doing wrong here.

#include <iostream>
using namespace std;

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

void displayLL(Node *p)
{
    while (p != NULL)
    {
        cout << p->data << " ";
        p = p->next;
    }
}

Node *createLL(int A[], int n, Node *ll)
{
    Node *tmp, *last;
    ll = new Node;

    ll->data = A[0];
    ll->next = NULL;
    last = ll;

    for (int i = 1; i < n; i  )
    {
        tmp = new Node;
        tmp->data = A[i];
        tmp->next = NULL;
        last->next = tmp;
        last = tmp;
    }
    return ll;
}

int countNodesLL(Node *p)
{
    int count = 0;
    while (p != NULL)
    {
        count  ;
        p = p->next;
    }
    return count;
}

void InsertNodeLL(Node *p, int index, int value)
{
    Node *tmp;
    if (index < 0 || index > countNodesLL(p))
    {
        return;
    }
    tmp = new Node;
    tmp->data = value;

    // This should insert in the beginning of the Linked List - but it is not working.
    if (index == 0)
    {
        tmp->next = p; // pointing next of tmp to p node
        p = tmp;       // making tmp as the HEAD of linkedList
    }
    // This inserts after 1st node, in between two nodes and at the end of the LL
    else
    {
        for (int i = 0; i < index - 1; i  )
        {
            p = p->next;
        }
        tmp->next = p->next;
        p->next = tmp;
    }
}

int main(int argc, char const *argv[])
{

    Node *linkedList = NULL;
    int A[] = {1, 2, 3, 4, 5, 6, 7, 8};
    linkedList = createLL(A, 8, linkedList);
    displayLL(linkedList);
    cout << endl;

    InsertNodeLL(linkedList, 0, 15);
    displayLL(linkedList);
    cout << endl;

    InsertNodeLL(linkedList, 4, 10);
    displayLL(linkedList);
    return 0;
}

I am getting the below output:

1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 10 5 6 7 8 

Expected Output:

1 2 3 4 5 6 7 8 
15 1 2 3 4 5 6 7 8 
15 1 2 3 4 10 5 6 7 8 

Please help me as to what is wrong in the code.

CodePudding user response:

You made two mistakes, first you didn't return a value back to the linked list since you passed the pointer by value, and second in your insert function you also do not return a value as well you are modifying the head variable so you lose the previous values. Also you want to insert at the 5th position not the 4th. When you make the changes inside the function you are only using local variables so after the stack frame gets popped your linked list doesn't get updated. You can use a global variable instead but if you are passing by value you have to return back to the caller if you wish to change the list in main. Here is my reimplementation of your code: `

#include <iostream>
using namespace std;

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

void displayLL(Node* p)
{
    while (p != NULL)
    {
        cout << p->data << " ";
        p = p->next;
    }
}

Node* createLL(int A[], int n, Node* ll)
{
    Node* tmp, * last;
    ll = new Node;

    ll->data = A[0];
    ll->next = NULL;
    last = ll;

    for (int i = 1; i < n; i  )
    {
        tmp = new Node;
        tmp->data = A[i];
        tmp->next = NULL;
        last->next = tmp;
        last = tmp;
    }
    return ll;
}

int countNodesLL(Node* p)
{
    int count = 0;
    while (p != NULL)
    {
        count  ;
        p = p->next;
    }
    return count;
}

Node* InsertNodeLL(Node* p, int index, int value)
{
    Node* tmp;
    Node* tmp2;
    if (index < 0 || index > countNodesLL(p))
    {
        return 0;
    }
    tmp = new Node;
    tmp->data = value;

    // This should insert in the beginning of the Linked List - but it is not working.
    if (index == 0)
    {
        tmp->next = p; // pointing next of tmp to p node
        p = tmp;
        return p;       // making tmp as the HEAD of linkedList
    }
    // This inserts after 1st node, in between two nodes and at the end of the LL
    else
    {
        tmp2 = p;
        for (int i = 0; i < index - 1; i  )
        {
            tmp2 = tmp2->next;
        }
        tmp->next = tmp2->next;
        tmp2->next = tmp;
        return p;
    }
}

int main(int argc, char const* argv[])
{

    Node* linkedList = NULL;
    int A[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    linkedList = createLL(A, 8, linkedList);
    displayLL(linkedList);
    cout << endl;

    linkedList = InsertNodeLL(linkedList, 0, 15);
    displayLL(linkedList);
    cout << endl;

    linkedList = InsertNodeLL(linkedList, 5, 10);
    displayLL(linkedList);
    return 0;
}
`
  • Related