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

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 are links for you to review our previous lessons:

Linked List; Lesson 1 

Linked List; Lesson 2 

Linked List; Lesson 3 




Alright! lets begin our 4th 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 and at some given/specified position. So today we'll try to insert nodes at:
  • At position specified by user at run time.
 At position specified by user at run time:


If you understood lesson 3,this will be a piece of cake for you. So what we are trying to do here is just to replace a specific position in code with the position given at run time by user.
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 << "\nEnter position at which you want to insert new node\n"; //4.1
    cin >> pos; //4.2
    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

}


What we did here? 
  • Created three new node as temp,temp1,temp2 (line 1,2,3). Why? because we need to link the new node with pos-1 node and pos node. If we link new node in such a way that the next part of pos-1 node has address of temp node in its next pointer and temp node gets address of pos node in its next pointer,then automatically new node temp will become node at specified pos-1 node and node at pos will become next node. For this,we use two nodes temp1, temp2 and make them copy of pos-1 node and pos node 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)
  • Took input from user for a specified position at run time (line 4.1,4.2)
  • 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 pos-1 node, so we used temp1 = temp1->next; in a loop which executes pos-1 times, thus making temp1 a copy of node previous to the position user entered  (line 8,9,10). Similarly we made temp2 copy of the node that user specified in pos variable.  
  • Finally,we'll link our nodes. Using line 11, we stored temp address in temp1 and stored temp2 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;
    int pos;
    cout << "Enter a value\n";
    cin >> value;
    cout << "\nEnter position at which you want to insert new node\n";
    cin >> pos;
    temp->data = value;

  

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

    for (int i = 1; i <= pos; 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 specified 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 second node position that was specified by user, second node has become third node and so on..

In our next lesson, we'll be learning to delete node. Stay tuned!

Comments

Popular posts from this blog

Introduction to C#; Lesson 2

Introduction to C#; Lesson 3