Thank you, next! Linked List in C++; Lesson 3

Hope you are following us from our first post about linked list, if so! lets continue with our linked list lesson, if not, don't worry, you can start form the beginning, here is a link for you to review our previous lessons:
Linked List; Lesson 1 

Linked List; Lesson 2 

Alright! lets begin our third lesson. As we previously learnt about creating nodes and printing by using concept of linked list, further we learnt about inserting new node at head position. So today we'll try to insert nodes at:




  • At some given/specified position.
 
Insert node at some given/
specified position:
Suppose you have a linked list with (say) three nodes and you are required to insert new node such as, after second node i.e your new node will become third node,third node will become forth node and so on. For this, you'll be codding insert() as follows:

void insert()
{
    node *temp = new node; //1
    node *temp1 = new node; //2
    node *temp2 = new node; //3
    int value; //4
    cout << "Enter a value\n"; //5
    cin >> value; //6
    temp->data = value;//7

   

    for (int i = 1; i <= 2; i++)
    {
        if (i == 1) //8
        {
            temp1 = head; //9
        }
        else
        {
            temp1 = temp1->next; //10
        }
       
    }

    for (int i = 1; i <= 3; i++)
    {
        if (i == 1)
        {
            temp2 = head;
        }
        else
        {
            temp2 = temp2->next;
        }
       
    }

    temp1->next = temp; //11
    temp->next = temp2; //12


So what we did here? 
  • Created three new node as temp,temp1,temp2 (line 1). Why? because we need to link the new node with current node 2 and node 3. If we link new node in such a way that the next part of node 2 has address of temp node in its next pointer and temp node gets address of node 3 in its next pointer,then automatically new node temp will become node 3 and node 3 will become node 4. For this,we use two nodes temp1, temp2 and make them copy of node 2 and node 3 respectively. When we'll link temp, temp1, tem2 then we will have our desired result.
  • Initialized an integer and took value from user (line 4,5,6)
  • Stored user's input value in temp's data part (line 7)
  • Now here comes the main part, as we first need to make temp1 as node2's copy, so we use a for loop here, we use condition that    if (i == 1)assign temp1 = head; Thus initializing the temp1 node,now we need to make it copy of node 2,so we used temp1 = temp1->next; in a loop which executes two times, thus making temp1 a copy of node2 (line 8,9,10). Similarly we made temp2 copy of node 3
  • Finally,we'll link our nodes. Using line 11 we stored temp address in temp1 (node 2) and stored temp2 (node3) address in temp.
Our complete code will look something like this:

#include "pch.h"
#include <iostream>
using namespace std;
struct node
{
    int data;
    node *next;
};
class list
{
private:

    node *head;
    node *tail;


public:
    list()
    {
        head = NULL;
        tail = NULL;
       
    }
    void createnode()
    {

        int value; //1

        cout << "Enter a value\n"; //2

        cin >> value; //3


        node *temp = new node; //4

        temp->data = value; //5
        temp->next = NULL; //6


        if (head == NULL)//7 decision
        {

            head = temp; //8
            tail = temp; //9
            temp = NULL; //10
        }
        else
        {

            tail->next = temp;//11
            tail = temp;//12

        }
    }
void disp()
{
   

    node *temp = new node;// 1
    temp= head; //2

    while (temp!= NULL) //3
    {

        cout << temp->data;//4
        temp = temp->next;//5
    }
}

void insert()
{
    node *temp = new node;
    node *temp1 = new node;
    node *temp2 = new node;
    int value;
    cout << "Enter a value\n";
    cin >> value;
    temp->data = value;

   

    for (int i = 1; i <= 2; i++)
    {
        if (i == 1)
        {
            temp1 = head;
        }
        else
        {
            temp1 = temp1->next;
        }
       
    }

    for (int i = 1; i <= 3; i++)
    {
        if (i == 1)
        {
            temp2 = head;
        }
        else
        {
            temp2 = temp2->next;
        }
       
    }

    temp1->next = temp;
    temp->next = temp2;

}
};

int main()
{
    list l;
    int choice;

    do
    {
        cout << "\nEnter 1 to create node\nEnter 2 to disp node\nEnter 3 to insert node at third position\nEnter 0 to exit\n";
        cin >> choice;
        if (choice == 1)
        {
            l.createnode();
        }
        else if (choice == 2)
        {
            l.disp();
        }
        else if (choice == 3)
        {
            l.insert();
        }
    } while (choice != 0);

    getchar();

}


Output:

  
As you can see, the new value "8" has been inserted at third node position,and third node has become 4th node.

That's it for now, lets continue  insert node at position specified by user at run time in next lesson. Stay tuned! 

Comments

Popular posts from this blog

Introduction to C#; Lesson 2

Introduction to C#; Lesson 3

Thank you, next! Linked List in C++; Lesson 4