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

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 first lesson:

Linked List; Lesson 1 

Alright! lets begin our second lesson. As we previously learnt about creating nodes and printing by using concept of linked list, today we'll try to insert nodes at various locations


  1. At head node.
  2. At some given position.
  3. At position specified by user at run time.
  Insert node at head node:


Now we hope and assume you grasped the concept of linked list quite well from our first lesson, so first lets jump to insert_start() function. Suppose you have a linked list with (say) three nodes and you are required to insert new node such that it replaces head node,i.e your new node will become head node,head node will become second node and so on. For this, you'll be codding insert_start() as follows:

void insert_start()
{
    node *temp = new node;//1
    int value; //2
    cout << "Enter a value\n"; //3

    cin >> value; //4
    temp->data = value; //5
    temp->next = head; //6
    head = temp; //7
}


 So what we did here? 
  • Created a new node as temp (line 1)
  • Initialized an integer and took value from user (line 2,3,4)
  • Stored user's input value in temp's data part (line 5)
  • Stored head's address in temp's next part so temp will point the current head node as its next node.(line 6)
  • Finally, making our head pointer point at newly created temp node, thus making it the head node now, don't worry about rest of nodes, as we linked new temp node and previously head node properly so our linked list will automatically arrange rest of nodes itself.
Here is the complete code:

#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_start()
{
    node *temp = new node;
    int value;
    cout << "Enter a value\n";
    cin >> value;
    temp->data = value;
    temp->next = head;
    head = temp;
}
};

int main()
{
    list l;
    int choice;

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

    getchar();



Output:


 As you can see, the new value "4" has been inserted at head position. That's it for now, lets continue insert node at some given position and 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