If Any Required Program Please Ask In Comment I Will Help You(any Program in JAVA or C++) . . THANKS FOR VISITING MY BLOG!

If U LIKE MY PROFILE RAISE YOUR HAND IF U NOT RAISE UR STANDARD. Powered by Blogger.

Monday, November 12, 2018

PROGRAM INSERT A NODE AT END OF LINK LIST.



#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

class Chain;

class Node
{
private:
    int Info;
    Node *Link;

    friend class Chain;
};

class Chain
{
private:
    Node *First;

public:
    Chain()
    {
        First = NULL;
    }

    void Insert(int No)
    {
        Node *Temp = new Node;

        Temp->Info = No;
        Temp->Link = First;

        First = Temp;
    }

    void Insert_End(int Value)
    {
        Node *Temp, *Temp1;

        Temp = First;
        Temp1 = new Node;


        Temp1->Info = Value;
        Temp1->Link = NULL;

        if(First == NULL)
            First = Temp1;

        else
        {
            while(Temp->Link != NULL)

                Temp = Temp->Link;
            Temp->Link = Temp1;
        }
    }

    void Print()
    {
        if(ListMsg())
        {
            cout << "\nLIST UNDERFLOWED...!!!\n" << endl;
            exit(1);
        }

        cout << "Elements in Node Is: " <<endl;
        for(Node *Temp = First; Temp != NULL; Temp = Temp->Link)
            cout << Temp->Info << endl;
    }

    inline bool ListMsg()
    {
        return First == NULL;
    }
};


int main()
{
    Chain Obj;

    Obj.Insert(12);
    Obj.Insert(13);
    Obj.Insert(14);
    Obj.Insert(15);
    Obj.Insert(16);

    Obj.Print();

    cout << "\nA Node Has Been Inserted At End...\n" << endl;
    Obj.Insert_End(17);
    Obj.Print();

    getch();
    return 0;
}

0 comments:

Post a Comment