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.

Saturday, December 29, 2018

PROGRAM FOR BINARY SEARCHING IN TREES using RECURSION.



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

    int BTree_Search(Node *P, int Value)
    {
        if(P == NULL)
        return 0;

        else if(P->Info == Value)
            return Value;

        else if(Value > P->Info)
            BTree_Search(P->Right,Value);

        else
            BTree_Search(P->Left, Value);
    }

    void Searching()
    {
        int No;

        cout << "\nEnter Any Value to Search: " ;
        cin >> No;

        if(BTree_Search(Root, No) == 0)
            cout << "\nValue Not Found...!!!\n" << endl;

        else
            cout << "\nValue Found...!!!\n" << endl;
    }
};

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

    B_Tree Obj;

    Obj.Make_Tree();
     Obj.Searching();

    getch();
    return 0;
}

PROGRAM TO COUNT SUM OF ALL NODES using RECURSION.



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

    int Recursion_Sum(Node *P)
    {
        if(P == NULL)
            return 0;

            int Total = P->Info + Add(P->Left) + Add(P->Right);

        return Total;
    }

    void Add_Sum()
    {
        cout << "\nSum Of All Nodes Is: " << Recursion_Sum(Root) << endl;
    }
};

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

    B_Tree Obj;

    Obj.Make_Tree();
     Obj.Add_Sum();

    getch();
    return 0;
}

PROGRAM FOR SORTING ARRAY USING QUICK SORT.



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

using namespace std;

class Array
{
private:
int *Arr, Size, Right;

public:
Array(int Siz)
{
Arr = new int[Siz];

for (int I = 0; I <Siz; I++)
Arr[I] = 0;

Right = 0;
Size = Siz;
}

void Input()
{
int No;

cout << "Enter Size Of Array: ";
cin >> No;

Right = No;

cout << "\nEnter Elements Of Array: " << endl;

for (int I = 0; I <Right; I++)
cin >> Arr[I];
}

void Quick_Sort()
{
    Sort(Arr, 0, Right -1);
}

    void Sort(int Temp[], int Beg, int End)
    {
        if(Beg >= End)
            return;

        int Mid = Partition(Temp, Beg, End);

        Sort(Temp, Beg, Mid -1);
        Sort(Temp, Mid +1, End);
    }

     int Partition (int Temp[], int Beg, int End)
    {
        int Pivot = Temp[Beg];
        int Index = Beg ;

        for (int J = Beg +1; J <= End; J++)
        {
            if (Temp[J] < Pivot)
            {
                Index++;
                Swap(&Temp[Index], &Temp[J]);
            }
        }

        Swap(&Temp[Index], &Temp[Beg]);
        return Index;
    }

    void Swap(int *Value1, int *Value2)
    {
        int Temp = *Value1;
        *Value1 = *Value2;
        *Value2 = Temp;
    }

void Print()
{
cout << "\nAfter Sorting Elements Of Array is: " << endl;

for (int I = 0; I <Right; I++)
cout << Arr[I] << endl;
}

~Array()
{
cout << "\n\n\n\n************PROGRAM ENDED***************";
}
};

int main()
{
Array Obj(20);

Obj.Input();
Obj.Quick_Sort();
Obj.Print();

getch();
return 0;
}

PROGRAM FOR SORTING ARRAY USING MERGE SORT.



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

using namespace std;

class Array
{
private:
int *Arr, Size, Right;

public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I <Siz; I++)
{
Arr[I] = 0;
}

Right = 0;
Size = Siz;
}

void Input()
{
int No;

cout << "Enter Size Of Array: ";
cin >> No;

Right = No;

cout << "\nEnter Elements Of Array: " << endl;
for (int I = 0; I <Right; I++)
{
cin >> Arr[I];
}
}

void Merge_Sort()
{
    Sorting(Arr, 0, Right -1);
}

void Sorting(int Temp[], int Beg, int End)
{
    int Mid;
    Mid = (Beg + End) /2;

    if(Beg >= End)
            return;

        Sorting(Temp, Beg, Mid);
        Sorting(Temp, Mid +1, End);

        Merge_Array(Temp, Beg, Mid, End);
}

void Merge_Array(int Temp[], int Beg, int Mid, int End)
{
    int Size1 = Mid - Beg +1;
    int Size2 = End - Mid;

    int Array1[Size1], Array2[Size2];

    for(int I = 0; I <Size1; I++)
            Array1[I] = Temp[Beg +I];

        for(int J = 0; J <Size2; J++)
            Array2[J] = Temp[Mid +1 +J];

    int I, J, K;
        I = J = 0;
        K = Beg;

        while(I < Size1 && J < Size2)
        {
            if(Array1[I] < Array2[J])
                Temp[K] = Array1[I++];

                else
                    Temp[K] = Array2[J++];

                K++;
        }

        while(I <Size1)
            Temp[K++] = Array1[I++];

        while(J <Size2)
            Temp[K++] = Array2[J++];
}

void Print()
{
cout << "\nElements Of Array is: " << endl;

for (int I = 0; I <Right; I++)
cout << Arr[I] << endl;
}

~Array()
{
cout << "\n\n\n\n************PROGRAM ENDED***************";
}
};

int main()
{
Array Obj(20);

Obj.Input();
Obj.Merge_Sort();
Obj.Print();

getch();
return 0;
}

Saturday, December 1, 2018

PROGRAM TO IMPLEMENT INORDER TRAVERSAL using STACK.



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

using namespace std;

class Node;
class B_Tree;

template <class Type>

class Stack
{
private:
Type *Arr;
int Top, Size;

public:
Stack(int Siz)
{
Size = Siz;

Arr = new Type[Size];
for (int I = 0; I <Size; I++)
{
Arr[I] = 0;
}

Top = -1;
}

void Push(Type Num)
{
if (Top == Size - 1)
{
cout << "\nStack Overflowed..........." << endl;
exit(1);
}

Arr[++Top] = Num;
}

Type Pop()
{
if (IsEmpty())
{
cout << "\nStack Underflowed..........." << endl;
return 0;
}

return Arr[Top--];
}

inline bool IsEmpty()
{
return (Top == -1);
}
};

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 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 Make_Tree()
    {
        int No;
        Node *P, *Q;

        cout << "\nEnter 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(No == P->Info)
            {
                cout << "\nDuplicate Numbers Occurred...!!!\n" << endl;
                exit(1);
            }

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

            else
                Set_Right(P, No);
        }
    }

    void InTraverse_2()
    {
        Stack <Node*> Obj(20);

        Node *New = Root;

        do
        {
            while(New != NULL)
            {
                Obj.Push(New);
                New = New->Left;
            }

            if(!Obj.IsEmpty())
            {
                New = Obj.Pop();

                cout <<  New->Info << " ";

                New = New->Right;
            }
        }
        while(!Obj.IsEmpty() || New != NULL);
    }
};


int main()
{
    B_Tree Obj;

    cout << "\t\t\tEnter 25 to End Making Nodes....." << endl;

    Obj.Make_Tree();

    cout << "\nTree In InOrder Traverse is: ";
    Obj.InTraverse_2();

    getch();
    return 0;
}

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

Friday, October 26, 2018

PROGRAM TO FIND NUMBER AND IT'S OCCURRENCES USING LINK LIST.



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

using namespace std;

class Chain;

template <class Type>

class Node
{
private:
Type Info;
Node<int> *Link;
friend class Chain;
};

class Chain
{
private:
Node<int> *First;

public:
Chain()
{
First = NULL;
}

void Head_Insert(int Value)
{
Node<int> *Temp = new Node<int>;

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

First = Temp;
}

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

for (Node<int> *Temp = First; Temp != NULL; Temp = Temp = Temp->Link)
cout << "Value of Node is: " << Temp->Info << endl;
}

void Search_Occur(int Value)
{
int Count;

for (Node<int> *Temp = First; Temp != NULL; Temp = Temp->Link)
{
if (Temp->Info == Value)
Count++;
}

cout << "\nValue found " << Count << " times in LIST." << endl;
}

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

int main()
{
Chain Obj;

for (int I = 0; I < 10; I++)
{
Obj.Head_Insert(I);
}

Obj.Print();

Obj.Search_Occur(1);

getch();
return 0;
}

PROGRAM FOR SEARCHING IN LINK LIST.



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

using namespace std;

class Chain;

template <class Type>

class Node
{
private:
Type Info;
Node<int> *Link;
friend class Chain;
};

class Chain
{
private:
Node<int> *First;

public:
Chain()
{
First = NULL;
}

void Head_Insert(int No)
{
Node<int> *Temp = new Node<int>;

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

First = Temp;
}

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

for (Node<int> *Temp = First; Temp != NULL; Temp = Temp->Link)
cout << "Value of Node is: " << Temp->Info << endl;
}

Node<int>* Search(int Value)
{
bool Find = false;

Node<int> *Temp;
for (Temp = First; Temp != NULL && !Find; Temp = Temp->Link)
{
if (Temp->Info == Value)
Find = true;
}

return Temp;
}

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

int main()
{
Chain Obj;

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

Node<int> *Obj1 = Obj.Search(2);

if (Obj1 == NULL)
cout << "\nValue Not Found In LIST....!!!!\n" << endl;

else
cout << "\nValue Found...!!! \n" << endl;

getch();
return 0;
}

LINKED LISTS TO PRINT ELEMENTS IN REVERSE ORDER USING RECURSION.



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

using namespace std;

class Chain;

template <class Type>

class Node
{
private:
Type Info;
Node<int> *Link;
friend class Chain;
};

class Chain
{
private:
Node<int> *First;

public:
Chain()
{
First = NULL;
}

void Head_Insert(int Value)
{
Node<int> *Temp = new Node<int>;

Temp->Info = Value;
Temp->Link = First;
First = Temp;
}

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

for (Node<int> *Temp = First; Temp != NULL; Temp = Temp->Link)
cout << "Value of Node is: " << Temp->Info << endl;
}

void Recursion(Node<int> *Ptr)
{
if (Ptr == NULL)
return;

cout << "Value is: " << Ptr->Info << endl;
Recursion(Ptr->Link);
}

void Reverse_Order()
{
Recursion(First);
}

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

int main()
{
Chain Obj;

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

Obj.Print();

getch();
return 0;
}

PROGRAM FOR THE LINKED LISTS TO COUNT EVEN NODES.



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

using namespace std;

class Chain;

template <class Type>

class Node
{
private:
Type Info;
Node<int> *Link;
friend class Chain;
};

class Chain
{
private:
Node<int> *First;

public:
Chain()
{
First = NULL;
}

void Head_Insert(int Value)
{
Node<int> *Temp = new Node<int>;

Temp->Info = Value;
Temp->Link = First;
First = Temp;
}


void Print()
{
int Count = 0;

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

for (Node<int> *Temp = First; Temp != NULL; Temp = Temp->Link)
{
if (Temp->Info % 2 == 0)
Count++;

cout << "Value of Node is: " << Temp->Info << endl;
}

cout << "\nThere are " << Count << " Even Nodes in List.\n" << endl;
}

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

int main()
{
Chain Obj;

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

Obj.Print();

getch();
return 0;
}

PROGRAM FOR THE LINKED LISTS TO COUNT NODES.



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

using namespace std;

class Chain;

template <class Type>

class Node
{
private:
Type Info;
Node<int> *Link;
friend class Chain;
};

class Chain
{
private:
Node<int> *First;

public:
Chain()
{
First = NULL;
}

void Head_Insert(int Value)
{
Node<int> *Temp = new Node<int>;

Temp->Info = Value;
Temp->Link = First;
First = Temp;
}

void Print()
{
int Count = 0;

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

for (Node<int> *Temp = First; Temp != NULL; Temp = Temp->Link)
{
cout << "Value of Node is: " << Temp->Info << endl;
Count++;
}

cout << "\nThere are " << Count << " Nodes in List.\n" << endl;
}

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

int main()
{
Chain Obj;

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

Obj.Print();

getch();
return 0;
}

PROGRAM FOR THE LINKED LISTS.



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

using namespace std;

class Chain;

template <class Type>

class Node
{
private:
Type Info;
Node<int> *Link;
friend class Chain;
};

class Chain
{
private:
Node<int> *First;

public:
Chain()
{
First = NULL;
}

void Head_Insert(int Value)
{
Node<int> *Temp = new Node<int>;

Temp->Info = Value;
Temp->Link = First;
First = Temp;
}

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

for (Node<int> *Temp = First; Temp != NULL; Temp = Temp->Link)
cout << "Value of Node is: " << Temp->Info << endl;
}

void Head_Del()
{
Node<int> *Temp = First;

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

First = Temp->Link;
cout << "Delete Node of value: " << Temp->Info << endl;
delete(Temp);
}

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

int main()
{
Chain Obj;

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

Obj.Print();

Obj.Head_Del();
Obj.Head_Del();
Obj.Head_Del();
Obj.Head_Del();
Obj.Head_Del();

getch();
return 0;
}

Sunday, October 21, 2018

CHECK WHETHER STRING IS PALINDROME USING STACK.



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

using namespace std;

template <class Type>

class Palindrom
{
private:
int Top, Size;
Type *Arr;

public:
Palindrom(int Siz)
{
Top = -1;
Size = Siz;
Arr  = new Type[Size];

strcpy(Arr, "\0");
}

void Push(Type Ch)
{

if(Top == Size-1)
{
cout << "\nStack Overflowed..........." << endl;
exit(1);
}

Arr[++Top] = Ch;
}

Type Pop()
{
if(isEmpty())
{
cout << "\nStack Underflowed............" << endl;
exit(1);
}

return Arr[Top--];
}

inline bool isEmpty()
{
return (Top == -1);
}

void Read_Check(char *Arrays)
{
    for(int I =0; I <strlen(Arrays); I++)
        {
            char Ch = Arrays[I];
            Push(Ch);
        }

        for(int I =0; I <strlen(Arrays); I++)
           Arrays[I]  = Pop();

        if(strcmp(Arr, Arrays) == 0)
            cout << "\nString is Palindrome.\n" << endl;

        else
            cout << "\nIt is Not Palindrome.\n" << endl;
}

~Palindrom()
{
cout << "\n\n\n----------PROGRAM ENDED---------------\n\n\n" << endl;
}
};

int main()
{
    char Array[100];
Palindrom<char> Obj1(5);

cout << "Enter Your String: ";
cin >> Array;

Obj1.Read_Check(Array);

getch();
return 0;
}

CHECK BRACKETS OF EXPRESSION ARE BALANCED or NOT using STACK.



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

using namespace std;

template <class Type>

class Brackets
{
private:
Type *Stack, *String;
bool Valid;
int Size, Top;

public:
Brackets(int Siz)
{
Size = Siz;

Stack = new Type[Size];
strcpy(Stack, "\0");

Valid = true;
Top = -1;
}

void Push(Type Ch)
{
if (Top == Size - 1)
{
cout << "\nString Overflowed......." << endl;
exit(1);
}

else
Stack[++Top] = Ch;
}

Type Pop()
{
if (IsEmpty())
{
cout << "\nStack Underflowed........" << endl;
exit(1);
}

else
return Stack[Top--];
}

void chkBrkt(Type Chr)
{
char Ch1, Ch2;

Ch1 = Chr;
if (Ch1 == '(' || Ch1 == '{' || Ch1 == '[')
Push(Ch1);

if (Ch1 == ')' || Ch1 == '}' || Ch1 == ']')
{
if (IsEmpty())
Valid = false;

else
{
Ch2 = Pop();

if (!((Ch2 == '(' && Ch1 == ')') || (Ch2 == '{' && Ch1 == '}') || (Ch2 == '[' && Ch1 == ']')))
Valid = false;
}
}
}

void Check()
{
if (!IsEmpty())
Valid = false;

if (Valid == true)
cout << "\nExpression's Brackets are Balanced and Expression Is Valid......\n" << endl;

else
cout << "\nExpression's Brackets are Unbalanced and Expression Is Invalid......\n" << endl;
}

inline bool IsEmpty()
{
return (Top == -1);
}

~Brackets()
{
cout << "\n\n\n\n******************PROGRAM ENDED******************\n\n\n\n" << endl;
}
};

int main()
{
Brackets<char> Obj(100);
char *Ch = new char[100];

cout << "Enter Expression: ";
cin >> Ch;

for (int I = 0; Ch[I] != '\0'; I++)
{
Obj.chkBrkt(Ch[I]);
}

Obj.Check();

getch();
return 0;
}