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

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 

Linked List; Lesson 4 

Alright! lets begin our fifth lesson i.e deleting node.Now this is a feature of linked list which array doesn't provide. You can always overwrite any value in array but you cant delete it, but linked list gives you the opportunity to do so.

Say, you have a linked list of 4 nodes and you are tasked to delete a node from that linked list asked by user at run time (This code will be quite similar to insert node code). For this, you'll be codding del() as follows:


void del()
{
    node *temp = new node; //1
    node *temp1 = new node; //2
    node *temp2 = new node; //3
    int pos;//4
    cout << "\nEnter the node you want to delete\n";//5
    cin >> pos; //6



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

    }

    for (int i = 1; i <= pos; i++)//loop 2
    {
        if (i == 1)
        {
            temp = head;
        }
        else
        {
            temp = temp->next;
        }

    }

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

    }

    temp1->next = temp2;
    delete temp;
}


What we did here? 

  • Created three new node as temp,temp1,temp2 (line 1,2,3). Why? because we need to link the pos-1 node and pos+1 node. Once this link is established,we can delete the node at position specified by user.
  • Initialized an integer and took input from user for a specified position at run time (line 4,5,6)
  • Now here comes the main part of linking, in (loop 1) we used 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 temp and temp2 copy of the node that user specified in pos variable and pos+1 node respectively.
  • Finally,we'll link our nodes. Using line 11, we stored temp2 address in temp1 and deleted temp node.
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 del()
{
    node *temp = new node;
    node *temp1 = new node;
    node *temp2 = new node;
    int pos;
    cout << "\nEnter the node you want to delete\n";
    cin >> pos;



    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)
        {
            temp = head;
        }
        else
        {
            temp = temp->next;
        }

    }

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

    }

    temp1->next = temp2;
    delete temp;
}

};

int main()
{
    list l;
    int choice;

    do
    {
        cout << "\nEnter 1 to create node\nEnter 2 to disp node\nEnter 3 to delete 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.del();
        }
    } while (choice != 0);

    getchar();

}


Output:

  

As you can see, the node 3, specified by user, has been deleted.

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