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.

Friday, November 30, 2018

PROGRAM TO IMPLEMENT THE TREE TRAVERSELS.



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

using namespace std;

class B_Tree;

class Node
{
private:
    int Info;
    Node *Left, *Right;

    friend class B_Tree;
};

class B_Tree
{
private:
    Node *Root;

public:
    B_Tree()
    {
        Root = NULL;
    }

    Node* Make_Root(int Value)
    {
        Node *Temp = new Node;

         Temp->Info = Value;

         Temp->Left = NULL;
         Temp->Right = NULL;

         return Temp;
    }

    void Make_Tree()
    {
        int No;
        Node *P, *Q;

        cout << "Enter  Number To Make Root:  ";
        cin >> No;

        cout << endl;

        Root = Make_Root(No);

        while(No != 25)
        {
            cout << "Enter Any Number: ";
            cin >> No;

            P = Q = Root;

            while(P->Info != No && Q != NULL)
            {
                P = Q;

                if(No < P->Info)
                Q = P->Left;

                else
                    Q = P->Right;
            }

            if(P->Info == No)
            {
                cout << "\nDuplicate Numbers Occurred...!!!\n" << endl;
                exit(1);
            }

            else if(No < P->Info)
                Set_Left(P, No);

            else
                Set_Right(P, No);
        }
    }

    void Set_Left(Node *P, int Value)
    {
        if(P == NULL || P->Left != NULL)
        {
            cout << "\nCannot Insert On Left Of It...!!!\n" << endl;
            exit(1);
        }

        P->Left = Make_Root(Value);
    }

    void Set_Right(Node *P, int Value)
    {
        if(P == NULL || P->Right != NULL)
        {
            cout << "\nCannot Insert On Right OF It...!!!\n" << endl;
            exit(1);
        }

        P->Right = Make_Root(Value);
    }

    void Pre_Traverse(Node* T)
    {
        if(T != NULL)
        {
            cout << T->Info << " ";

            Pre_Traverse(T->Left);
            Pre_Traverse(T->Right);
        }
    }

    void Post_Traverse(Node *T)
    {
        if(T != NULL)
        {
            Post_Traverse(T->Left);
            Post_Traverse(T->Right);

            cout << T->Info << " ";
        }
    }

    void In_Traverse(Node *T)
    {
        if(T != NULL)
        {
            In_Traverse(T->Left);

            cout << T->Info << " ";

            In_Traverse(T->Right);
        }
    }

    void Traverses()
    {
        cout << "\nTree In PreOrder Traverse Is: " << endl;
        Pre_Traverse(Root);

        cout << "\n\nTree In InOrder Traverse Is: " << endl;
        In_Traverse(Root);

        cout << "\n\nTree In PostOrder Traverse Is: " << endl;
        Post_Traverse(Root);
    }
};

int main()
{
    cout << "\t\t\tEnter 25 To End......" << endl;

    B_Tree Obj;

    Obj.Make_Tree();
    Obj.Traverses();

    getch();
    return 0;
}

PROGRAM TO IMPLEMENT THE DOUBLY LINKLIST.



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

using namespace std;

class Double_LL;

class Node
{
private:
    int Info;
    Node *Left, *Right;

    friend class Double_LL;
};

class Double_LL
{
private:
    Node *First;

public:
    Double_LL()
    {
        First = NULL;
    }

    void First_Node(int Value)
    {
           Node *Temp = new Node;
           Temp->Info = Value;

          Temp->Right = NULL;
          Temp->Left = NULL;

          First = Temp;
    }

    void Insert_Right(int Value1, int Value2)
    {
        Node *R, *P, *Temp;

        Temp = new Node;
        Temp->Info = Value2;

            P = Search(Value1);
            R = P->Right;

            if(P == NULL)
            {
                cout << "\nCannot Insert On Right Of It.....!!!\n" << endl;
                exit(1);
            }

            Temp->Left = P;
            P->Right = Temp;

            Temp->Right = R;

            if(R != NULL)
                R->Left = Temp;
    }

    void Insert_Left(int Value1, int Value2)
    {
        Node *L, *P, *Temp;

        Temp = new Node;
        Temp->Info = Value2;

        P = Search(Value1);
        L = P->Left;

        if(P == NULL)
        {
            cout << "\nCannot Insert On Left Of It.....!!!\n" << endl;
            exit(1);
        }

        Temp->Right = P;
        P->Left = Temp;

        Temp->Left = L;

        if(L != NULL)
            L->Right = Temp;
    }

    void Remove(int Value)
    {
        Node *L, *R, *Temp;

        Temp = Search(Value);

        if(Temp == NULL || IsEmpty())
        {
            cout << "\nDeletion is Not Possible...!!!\n" << endl;
            exit(1);
        }

        L = Temp->Left;
        R = Temp->Right;

        cout << "Value To Be Deleted is: " << Temp->Info << endl;

        if(L == NULL)
            R->Left = NULL;

        else if(R == NULL)
            L->Right = NULL;

        else
        {
            L->Right = R;
            R->Left = L;
        }
    }

    Node* Search(int Value)
    {
        Node *Temp;

        for(Temp = First; Temp != NULL ;Temp = Temp->Right)
        {
            if(Temp->Info == Value)
                return Temp;
        }

        return NULL;
    }

    void Print()
    {
        Node *Temp = new Node;

        for(Temp = First; Temp != NULL; Temp = Temp->Right)
            cout << "Value Of Node Is " << Temp->Info << endl;
    }

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

int main()
{
    Double_LL Obj;

    Obj.First_Node(1);

    Obj.Insert_Right(1,2);
    Obj.Insert_Right(2,3);
    Obj.Insert_Right(3,4);
    Obj.Insert_Right(4,5);

    Obj.Insert_Left(5,6);
    Obj.Insert_Left(6,7);
    Obj.Insert_Left(7,8);
    Obj.Insert_Left(8,9);

    Obj.Remove(1);
    Obj.Remove(2);
    Obj.Remove(3);
    Obj.Remove(4);
    Obj.Remove(5);
    Obj.Remove(6);
    Obj.Remove(7);

    ///Obj.Print();

    getch();
    return 0;
}

PROGRAM TO IMPLEMENT THE JOSEPHUS PROBLEM.



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

using namespace std;

class C_List;

class Node
{
    friend class C_List;
    friend class Josephus;

private:
    char Name[100];
    Node *Link;
};

class Josephus;

class C_List
{
    friend class Josephus;

private:
    Node *Last;

public:
    C_List()
    {
        Last = NULL;
    }

    void Insert(char *Naam)
    {
        Node *Temp = new Node;

        strcpy(Temp->Name , Naam);

        if(IsEmpty())
            Last = Temp;

        else
            Temp->Link = Last->Link;

            Last->Link = Temp;
    }

    void Delet_After(Node *Test)
{
    if( Test == NULL || Test == Test->Link)
        {
            cout << "DELETION IS NOT POSSIBLE...!!!" << endl;
            exit(1);
        }

        Node *Temp = Test->Link;
        cout << "Node to Be Deleted is: " << Temp->Name << endl;

        Test->Link = Temp->Link;

        delete(Temp);
}

    inline bool IsEmpty()
    {
        return Last == NULL;
    }
};

class Josephus
{
private:
    int No;
    char Name1[100];

public:
    Josephus()
    {
        No = 0;
        strcpy(Name1, "NULL");
    }

    void Joseph_Problem()
    {
         C_List Obj;

        while(strcmp(Name1, "end") != 0)
        {
            cout << "Enter the Name: ";
            cin >> Name1;

            Obj.Insert(Name1);
        }

        cout << "\nEnter Any Number: ";
        cin >> No;
        cout << endl;

        while(Obj.Last != Obj.Last->Link)
        {
            for(int I =0 ; I < No -1; I++)
                Obj.Last = Obj.Last->Link;

            Obj.Delet_After(Obj.Last);
        }

        cout  << "\nNode That Will Escape is: " << Obj.Last->Name << endl;
    }
};

int main()
{
    Josephus Obj;

    Obj.Joseph_Problem();

    getch();
    return 0;
}

PROGRAM TO IMPLEMENT STACK USING CIRCULAR LIST.



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

using namespace std;

class Stack;

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

friend class Stack;
};

class Stack
{
private:
Node *Last;

public:
Stack()
{
Last = NULL;
}

void Push(int Value)
{
Node *Temp = new Node;

Temp->Info = Value;

if (IsEmpty())
Last = Temp;

else
Temp->Link = Last->Link;

Last->Link = Temp;
}

int Pop()
{
int Value;
Node *Temp = new Node;

if (IsEmpty())
{
cout << "\nLIST IS EMPTY.....!!!\n" << endl;
exit(1);
}

Temp = Last->Link;
Value = Temp->Info;

if (Last == Temp)
Last = NULL;

else
Last->Link = Temp->Link;

delete Temp;
    return Value;
}

inline bool IsEmpty()
{
return Last == NULL;
}
};

int main()
{
Stack Obj;

Obj.Push(1);
Obj.Push(2);
Obj.Push(3);
Obj.Push(4);
Obj.Push(5);

cout << Obj.Pop() << endl;
cout << Obj.Pop() << endl;
cout << Obj.Pop() << endl;
cout << Obj.Pop() << endl;
cout << Obj.Pop() << endl;

getch();
return 0;
}

PROGRAM TO IMPLEMENT QUEUE USING CIRCULAR LIST.



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

using namespace std;

class Queue;

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

friend class Queue;
};

class Queue
{
private:
Node *Last;

public:
Queue()
{
Last = NULL;
}

void EnQueue(int Value)
{
Node *Temp = new Node;

Temp->Info = Value;

if (IsEmpty())
Last = Temp;

else
Temp->Link = Last->Link;

Last->Link = Temp;
Last = Temp;
}

int dQueue()
{
    int Value;
    Node *Temp = new Node;

if (IsEmpty())
{
cout << "\nLIST IS EMPTY.....!!!\n" << endl;
exit(1);
}

Temp = Last->Link;
Value = Temp->Info;

if(Temp == Last)
            Last = NULL;

        else
            Last->Link = Temp->Link;

delete Temp;
    return Value;
}

inline bool IsEmpty()
{
return Last == NULL;
}
};

int main()
{
Queue Obj;

Obj.EnQueue(1);
Obj.EnQueue(2);
Obj.EnQueue(3);
Obj.EnQueue(4);
Obj.EnQueue(5);

cout << Obj.dQueue() << endl;
cout << Obj.dQueue() << endl;
cout << Obj.dQueue() << endl;
cout << Obj.dQueue() << endl;
cout << Obj.dQueue() << endl;

getch();
return 0;
}

PROGRAM TO IMPLEMENT CIRCULAR LISTS(function del after).



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

using namespace std;

class C_List;

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

friend class C_List;
};

class C_List
{
private:
Node *Last;

public:
C_List()
{
Last = NULL;
}

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

Temp->Info = Value;

if (IsEmpty())
Last = Temp;

else
Temp->Link = Last->Link;

Last->Link = Temp;
Last = Temp;
}

void Delet_After(Node *Temp)
{
    if(IsEmpty() || Temp == Temp->Link)
        {
            cout << "DELETION IS NOT POSSIBLE...!!!" << endl;
            exit(1);
        }

        Node *Test = Temp->Link;

        cout << "\nValue to be Deleted is: " << Temp->Info << endl;

        Temp->Link = Test->Link;
        delete(Test);
}

void Delete(C_List Obj)
{
    Delet_After(Obj.Last);
}

inline bool IsEmpty()
{
return Last == NULL;
}
};

int main()
{
C_List Obj;

Obj.Insert(1);
Obj.Insert(2);
Obj.Insert(3);
Obj.Insert(4);
Obj.Insert(5);

Obj.Delete(Obj);

getch();
return 0;
}

PROGRAM TO IMPLEMENT CIRCULAR LIST.



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

using namespace std;

class C_List;

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

friend class C_List;
};

class C_List
{
private:
Node *Last;

public:
C_List()
{
Last = NULL;
}

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

Temp->Info = Value;

if (IsEmpty())
Last = Temp;

else
Temp->Link = Last->Link;

Last->Link = Temp;
Last = Temp;
}

void Print()
{
    Node *Temp = new Node;

    if(IsEmpty())
        {
            cout << "\nLIST IS EMPTY...!!!\n" << endl;
            exit(1);
        }

        for(Temp = Last->Link; Temp != Last; Temp = Temp->Link)
            cout << Temp->Info << endl;

        cout << Temp->Info << endl;
}

void Delet()
{
Node *Temp = new Node;

if (IsEmpty())
{
cout << "\nLIST IS EMPTY.....!!!\n" << endl;
exit(1);
}

Temp = Last->Link;

if (Last == Temp)
Last = NULL;

else
Last->Link = Temp->Link;

delete Temp;
}

inline bool IsEmpty()
{
return Last == NULL;
}
};

int main()
{
C_List Obj;

Obj.Insert(1);
Obj.Insert(2);
Obj.Insert(3);
Obj.Insert(4);
Obj.Insert(5);

Obj.Print();

Obj.Delet();
Obj.Delet();
Obj.Delet();
Obj.Delet();
Obj.Delet();

getch();
return 0;
}

Monday, November 12, 2018

PROGRAM TO CONCATENATE TWO LISTS TO 3rd



#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 Concate_List(Chain Ptr1, Chain Ptr2)
    {
        Node *Temp2;
        for(Temp2 = Ptr2.First; Temp2 != NULL; Temp2 = Temp2->Link)
            Insert(Temp2->Info);

        Node *Temp1;
        for(Temp1 = Ptr1.First; Temp1 != NULL; Temp1 = Temp1->Link)
            Insert_End(Temp1->Info);
    }

    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 Obj1, Obj2, Obj3;

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

    Obj2.Insert(17);
    Obj2.Insert(18);
    Obj2.Insert(19);
    Obj2.Insert(20);
    Obj2.Insert(21);

    cout << "\n1st and 2nd list has been Concatenated to 3rd List......\n" << endl;
    Obj3.Concate_List(Obj1, Obj2);
    Obj3.Print();

    getch();
    return 0;
}

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;
}

PROGRAM TO MERGE TWO LISTS.



#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 Merge_List(Chain Ptr)
    {
        Node *Temp;
        for(Temp = Ptr.First; Temp != NULL; Temp = Temp->Link)
            Insert_End(Temp->Info);
    }

    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 Obj1, Obj2;

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

    Obj2.Insert(17);
    Obj2.Insert(18);
    Obj2.Insert(19);
    Obj2.Insert(20);
    Obj2.Insert(21);

    cout << "\n2nd List has been Merged To 1st List......\n" << endl;
    Obj1.Merge_List(Obj2);
    Obj1.Print();

    getch();
    return 0;
}