C++ Programming Tutorial

 
 
 

Data File Structure Programs

Array

Insert, edit, delete, append, display, Srch. Insert, delete, merge, delete multiple occurrences Arrays as a Stack in graphics

Stack

Stack operations using array Stack using static memory allocation Stack using dynamic memory allocation Double ended link list as a stack Lnked list as a Stack Infix expr. to Postfix expr. Postfix expr. into an Infix expr. Arrays as a Stack in graphics Stack as an Arithmetic expr. Evaluater Graphical Rep. of Stack Stack to traverse - inodr, postodr, preodr

Queue

Queue using static memory allocation Queue using dynamic memory allocation Circular queue Linked list as a Queue Double Ended linked list as a Queue Graphical Rep. of Queue Arrays as a Linear Queue Array as a Circular Queue Arrays as a Linear Queue ( in graphics ) Arrays as a Circular Queue ( in graphics )

Linked List

Singly link list Circular linked list Doubly link list Linked list as a Queue Linked list as a Stack Double Ended linked list as a Queue Double Ended linked list as a Stack Infix to Postfix - Linked List as Stack Circular doubly link list Single Ended Linked List - Sorting in both odr Hashing - double ended Linked List Sort of link list

Tree

Linked List as a Binary Srch. Tree Set Class using Binary Srch. Tree Maximum depth of Binary Srch. Tree Minimum Spaning Tree Prims algo - minimum spanning tree Traverse binary tree - inodr, preodr, post Find number in binary Srch. tree display levell

Sorting

Bubble Sort Selection Sort Insertion Sort Radix Sort Merge Sort Quick Sort Heap Sort Linear Sort Shell Sort Topological Sort

Searching

Linear Srch. or Sequential Srch. Binary Search Breadth First Search Traversal Depth First Search Traversal Shortest Path-Given Source-Destination-Dijkstras

Hashing

Simple implementation of Hashing Hashing using double-ended Linked List Hashing using Mid-Square Method Example of Hashing n term of the fibonacci series using recursion

Recursion

Factorial of the given number using recursion Mystery of Towers of Hanoi using recursion
 
 
 # include <iostream.h>
 # include   <stdlib.h>
 # include   <string.h>
 # include    <conio.h>

 /**************************************************************************/
 //--------------------------  Class Definition  --------------------------//
 /**************************************************************************/

 //--------------------------------  Set  ---------------------------------//

 class Set
 {
    private:
       int data;
       int size;

       static int flag;
       static int value;
       static int cur;

       Set *LeftNode;
       Set *RightNode;

    public:
       Set *RootNode;
       Set *Location;
       Set *Parent;

       Set( );
       Set* InsertElement(Set*, const int);
       void DeleteElementWith0Or1Child( );
       void DeleteElementWith2Child( );
       void getElementAtPos(Set*, const int);
       int getElementAt(Set*, const int);
       void Print(Set*);
       void Print( );
       void PrintAscending(Set*);
       void PrintDescending(Set*);

       int Size( );
       void Empty( );
       int IsEmpty( );
       void Read(const char*);
       void AddElement(const int);
       void DeleteElement(const int);
       void PrintAscending( );
       void PrintDescending( );
       int IsAnElement(const int);
       Set Union(const Set set);
       Set Intersection(const Set set);
       int IsASubSet(const Set subset);
       int operator==(const Set set);
       Set operator-(const Set set);
 };

 int Set::flag = 1;
 int Set::value = 0;
 int Set::cur = 0;


 //------------------------  Function Definitions  -----------------------//


 //-------------------------------  Set( )  ------------------------------//

 Set::Set( )
 {
    RootNode = NULL;
    LeftNode = NULL;
    RightNode = NULL;

    data = 0;
    size = 0;
 }

 //--------------------------  InsertElement( )  ------------------------//

 Set* Set::InsertElement(Set *Node, const int data)
 {
    if (Node == NULL)
    {
       Set *Temp;

       Temp = new Set;

       Temp->data = data;
       Node = Temp;
       Node->LeftNode = NULL;
       Node->RightNode = NULL;

       size ++;
    }

    else
    {
       Parent = Node;

       if (data > Node->data)
       {
      if (Node->RightNode == NULL)
         Node->RightNode = InsertElement(Node->RightNode, data);

      else
         InsertElement(Node->RightNode, data);
       }

       else
       {
      if (Node->LeftNode == NULL)
         Node->LeftNode = InsertElement(Node->LeftNode, data);

      else
         InsertElement(Node->LeftNode,data);
       }
    }

    return Node;
 }

 //------------------------------  Size( )  -------------------------------//

 int Set::Size( )
 {
    return size;
 }

 //-----------------------------  IsEmpty( )  -----------------------------//

 int Set::IsEmpty( )
 {
    if (RootNode == NULL)
       return 1;

    return 0;
 }

 //-------------------------------  Empty( )  -----------------------------//

 void Set::Empty( )
 {
    for (int i = size; i >= 1; i--)
    {
       int num = getElementAt(RootNode, i);

       flag = 0;

       DeleteElement(num);

       flag = 1;
    }

    RootNode = NULL;
    size = 0;
 }

 //---------------------------  getElementAtPos( )  -----------------------//

 void Set::getElementAtPos(Set *Node, const int req)
 {
    cur ++;

    if (cur == req)
       value = Node->data;

    if (Node->LeftNode != NULL)
       getElementAtPos(Node->LeftNode, req);

    if (Node->RightNode != NULL)
       getElementAtPos(Node->RightNode, req);
 }

 int Set::getElementAt(Set* Node, const int req)
 {
    value = 0;
    cur = 0;

    getElementAtPos(Node, req);

    return value;
 }

 //----------------------------------  Print( )  --------------------------//

 void Set::Print(Set *Node)
 {
    if (Node == NULL)
    {  }

    else
    {
       cout << \" \" << Node->data;

       if (Node->LeftNode != NULL)
      Print(Node->LeftNode);

       if (Node->RightNode != NULL)
      Print(Node->RightNode);
    }
 }

 void Set::Print( )
 {
    Print(RootNode);
 }

 //---------------------------  PrintAscending( )  ------------------------//

 void Set::PrintAscending(Set *Node)
 {
    if (Node==NULL)
    {  }

    else
    {
       if (Node->LeftNode != NULL)
      PrintAscending(Node->LeftNode);

       cout << \" \" << Node->data;

       if (Node->RightNode != NULL)
      PrintAscending(Node->RightNode);
    }
 }

 void Set::PrintAscending( )
 {
    PrintAscending(RootNode);
 }

 //---------------------------  PrintDescending( )  -----------------------//

 void Set::PrintDescending(Set *Node)
 {
    if (Node == NULL)
    {  }

    else
    {
       if (Node->RightNode != NULL)
      PrintDescending(Node->RightNode);

       cout << \" \" << Node->data;

       if (Node->LeftNode != NULL)
      PrintDescending(Node->LeftNode);
    }
 }

 void Set::PrintDescending( )
 {
    PrintDescending(RootNode);
 }

 //---------------------------  IsAnElement( )  ---------------------------//

 int Set::IsAnElement(const int key)
 {
    int depth = 1;
    int left_right = 0;

    Set *Pointer = NULL;
    Set *Save = NULL;

    Location = NULL;
    Parent = NULL;

    if (RootNode == NULL)
    {
       Location = NULL;
       Parent = NULL;
    }

    else if (key == RootNode->data)
    {
       Location = RootNode;
       Parent = NULL;

       depth = 1;
    }

    else
    {
       if (key < RootNode->data)
       {
      Pointer = RootNode->LeftNode;

      left_right = 1;
       }

       else
       {
      Pointer = RootNode->RightNode;

      left_right = 2;
       }

       Save = RootNode;

       while (Pointer != NULL)
       {
      depth += 1;

      if (key == Pointer->data)
      {
         Location = Pointer;
         Parent = Save;

         break;
      }

      else if(key < Pointer->data)
      {
         Save = Pointer;
         Pointer = Pointer->LeftNode;
      }

      else if(key > Pointer->data)
      {
         Save = Pointer;
         Pointer = Pointer->RightNode;
      }
       }
    }

    if (flag == 1)
    {
       if (Location == NULL)
       {
      Parent = NULL;

      cout << \"\\n\\n*** \" << key << \" is not found in the Set. \" << endl;
       }

       else if (Location != NULL)
       {
      if (left_right == 0)
         cout << \"\\n\\n*** \" << key << \" is the Root Node.\" << endl;

      else if (left_right == 1)
         cout << \"\\n\\n*** \" << key << \" lies at the Left side of the Root Node \";

      else if (left_right == 2)
         cout << \"\\n\\n*** \" << key << \" lies at the Right side of the Root Node \";

      if (left_right)
         cout << \"and at the depth level \" << depth << endl;
       }
    }

    if (Location != NULL)
       return 1;

    return 0;
 }

 //--------------------  DeleteElementWith0Or1Child( )  -------------------//

 void Set::DeleteElementWith0Or1Child( )
 {
    Set *Child;

    if (Location->LeftNode == NULL && Location->RightNode == NULL)
       Child = NULL;

    else if (Location->LeftNode != NULL)
       Child = Location->LeftNode;

    else
       Child = Location->RightNode;

    if (Parent != NULL)
    {
       if (Location == Parent->LeftNode)
      Parent->LeftNode = Child;

       else
      Parent->RightNode = Child;
    }

    else
       RootNode = Child;
 }

 //----------------------  DeleteElementWith2Child( )  --------------------//

 void Set::DeleteElementWith2Child( )
 {
    Set *Pointer = Location->RightNode;
    Set *Save = Location;

    Set *Sucessor;
    Set *Parent_sucessor;

    while (Pointer->LeftNode != NULL)
    {
       Save = Pointer;
       Pointer = Pointer->LeftNode;
    }

    Sucessor = Pointer;
    Parent_sucessor = Save;

    Set *temp_loc = Location;
    Set *temp_par = Parent;

    Location = Sucessor;
    Parent = Parent_sucessor;

    DeleteElementWith0Or1Child( );

    Location = temp_loc;
    Parent = temp_par;

    if (Parent != NULL)
    {
       if (Location == Parent->LeftNode)
      Parent->LeftNode = Sucessor;

       else
      Parent->RightNode = Sucessor;
    }

    else
       RootNode = Sucessor;

    Sucessor->LeftNode = Location->LeftNode;
    Sucessor->RightNode = Location->RightNode;
 }

 //--------------------------  DeleteElement( )  --------------------------//

 void Set::DeleteElement(const int key)
 {
    IsAnElement(key);

    if (RootNode == NULL)
    {
       if (flag == 1)
      cout << \"\\n\\n*** Error : Set is empty. \\n\" << endl;
    }

    else if (Location == NULL)
    {
       if (flag == 1)
      cout << \"\\n\\n*** \" << key << \"  does not exists in the Set \\n\" << endl;
    }

    else
    {
       if (Location->RightNode != NULL && Location->LeftNode != NULL)
      DeleteElementWith2Child( );

       else
      DeleteElementWith0Or1Child( );

       size --;

       if (flag == 1)
      cout << \"*** \" << key << \" is deleted from the Set.\" << endl;
    }
 }

 //----------------------------  AddElement( )  ---------------------------//

 void Set::AddElement(const int num)
 {
    if (RootNode == NULL)
       RootNode = InsertElement(RootNode, num);

    else
    {
       flag = 0;

       IsAnElement(num);

       flag = 1;

       if (Location==NULL)
      InsertElement(RootNode, num);
    }
 }

 //--------------------------------  Read( )  -----------------------------//

 void Set::Read(const char* Numbers)
 {
    char Temp[255] = {NULL};

    strcpy(Temp, Numbers);

    char* Ptr = strtok(Temp,\",\");

    int num = 0;

    flag = 0;

    while (Ptr != NULL)
    {
       num = atoi(Ptr);

       AddElement(num);

       Ptr = strtok(NULL, \",\");
    }

    flag = 1;
 }

 //----------------------------------  Union( )  --------------------------//

 Set Set::Union(const Set set)
 {
    Set C;

    flag = 0;

    for (int i = 1; i <= size; i ++)
       C.AddElement(getElementAt(RootNode, i));

    for (int j = 1; j <= set.Size( ); j ++)
       C.AddElement(getElementAt(set.RootNode, j));

   flag = 1;

    return C;
 }

 //---------------------------  Intersection( )  --------------------------//

 Set Set::Intersection(const Set set)
 {
    Set C;

    for (int i = 1; i <= size; i ++)
    {
       int num = getElementAt(RootNode, i);

       flag = 0;

       if (set.IsAnElement(num) == 1)
      C.AddElement(num);

       flag = 1;
    }

    return C;
 }

 //----------------------------  Operator==( )  ---------------------------//

 int Set::operator==(const Set set)
 {
    if (size != set.Size( ))
       return 0;

    int eFlag = 1;

    for (int i = 1; i <= size; i ++)
    {
       int num = getElementAt(RootNode, i);

       flag = 0;

       if (set.IsAnElement(num) == 0)
       {
      eFlag = 0;

      break;
       }

       flag = 1;
    }

    return eFlag;
 }

 //-----------------------------  Operator-( )  ---------------------------//

 Set Set::operator-(const Set set)
 {
    Set C;

    for (int i = 1; i <= size; i ++)
       C.AddElement(getElementAt(RootNode, i));

    for (int j = 1; j <= set.Size( ); j ++)
    {
       int num = getElementAt(set.RootNode, j);

       flag = 0;

       C.DeleteElement(num);

       flag = 1;
    }

    return C;
 }

 //-----------------------------  IsASubSet( )  ---------------------------//

 int Set::IsASubSet(const Set subset)
 {
    if (size == 0)
       return 0;

    if (subset.Size( ) == 0)
       return 1;

    int eFlag = 1;

    for (int i = 1; i <= size; i ++)
    {
       int num = getElementAt(subset.RootNode, i);

       flag = 0;

       if (IsAnElement(num) == 0)
       {
      eFlag = 0;
      break;
       }

       flag = 1;
    }

    return eFlag;
 }


 //-------------------------------  main( )  -----------------------------//


 int main( )
 {
    clrscr( );

    Set A;

    cout << \"Read() & AddElement() Demonstration\" << endl;
    cout << \"***********************************\" << endl;

    cout << \"\\nRead(\\\"5,3,1,2,0\\\")\" << endl;
    A.Read(\"5,3,1,2,0\");

    cout << \"AddElement(4)\" << endl;
    cout << \"AddElement(9)\" << endl;
    cout << \"AddElement(4)\" << endl;
    A.AddElement(4);
    A.AddElement(9);
    A.AddElement(4);

    cout << \"\\nSet A =\";
    A.Print( );

    getch( );

    cout << \"\\n\\n\\nPrintAscending() Demonstration\" << endl;
    cout << \"******************************\" << endl;

    cout << \"\\nAscending Order :\";
    A.PrintAscending( );

    getch( );

    cout << \"\\n\\n\\nPrintDescending() Demonstration\" << endl;
    cout << \"*******************************\" << endl;

    cout << \"\\nDescending Order :\";
    A.PrintDescending( );

    getch( );

    cout << \"\\n\\n\\nDeleteElement() Demonstration\" << endl;
    cout << \"*****************************\" << endl;

    cout << \"DeleteElement(5)\" << endl;
    A.DeleteElement(5);

    cout << \"\\nSet =\";
    A.Print( );

    getch( );

    cout << \"\\n\\n\\nIsAnElement() Demonstration\" << endl;
    cout << \"***************************\" << endl;

    A.IsAnElement(2);

    getch( );

    cout << \"\\n\\n\\nSize() Demonstration\" << endl;
    cout << \"********************\" << endl;

    cout << \"\\nSet Size = \" << A.Size( ) << endl;

    getch( );

    cout << \"\\n\\n\\nIsEmpty() Demonstration\" << endl;
    cout << \"******************************\" << endl;

    if (A.IsEmpty( ) == 1)
       cout << \"\\nThe Set is Empty\" << endl;

    else
       cout << \"\\nThe Set Is Not Empty\" << endl;

    getch( );

    cout << \"\\n\\n\\nUnion() Demonstration\" << endl;
    cout << \"*********************\" << endl;

    Set B, C;

    B.Read(\"1,7,0,4,6,8,5\");

    cout << \"\\nSet A =\";
    A.Print( );

    cout << \"\\nSet B =\";
    B.Print( );

    C = A.Union(B);

    cout << \"\\n\\nC = A u B =\";
    C.Print( );

    getch( );

    cout << \"\\n\\n\\nIntersection() Demonstration\" << endl;
    cout << \"****************************\" << endl;

    C = A.Intersection(B);

    cout << \"\\n\\nC = A n B =\";
    C.Print( );

    getch( );

    cout << \"\\n\\n\\nEmpty() Demonstration\" << endl;
    cout << \"*********************\" << endl;

    C.Empty( );

    if (C.IsEmpty( ) == 1)
       cout << \"\\n\\nThe Set C is Empty\" << endl;

    else
       cout << \"\\n\\nThe Set C Is Not Empty\" << endl;

    getch( );

    cout << \"\\n\\n\\nOperator == Demonstration\" << endl;
    cout << \"*************************\" << endl;

    if (A == B)
      cout << \"\\nSet A = Set B\" << endl;

    else
      cout << \"\\nSet A != Set B\" << endl;

    getch( );

    cout << \"\\n\\n\\nOperator - Demonstration\" << endl;
    cout << \"************************\" << endl;

    C = (A - B);

    cout << \"\\nC = A - B = \";
    C.Print( );

    getch( );

    cout << \"\\n\\n\\nIsASubSet() Demonstration\" << endl;
    cout << \"*************************\" << endl;

    B.Empty( );

    B.Read(\"4,1,2\");

    cout << \"\\n\\nSet A =\";
    A.Print( );

    cout << \"\\nSet B =\";
    B.Print( );

    if (A.IsASubSet(B) == 1)
       cout << \"\\n\\nSet B is a Subset of Set A\" << endl;

    else
       cout << \"\\n\\nSet B is not Subset of Set A\" << endl;

    getch( );
    return 0;
 }

    Related Post:
  1. Program to illustrate the use of nested structures

  2. Program to solve the Towers of Hanoi Problem (using Recursive Algorithm)

  3. Program to illustrate the Bubble Sorting of arrays

  4. Program to read a Non-Linear equation in one variable, then evaluate it using Bisection Method and display its kD accurate root

  5. Program to implement the Kurskals Algorithm to solve Minimum Cost Spanning Tree Problem (MST)

  6. Program that prints first 20 integers (using while loop)

  7. Program to show the projection of 3D objects using General Perspective Projection

  8. Program to draw a circle using MidPoint Circle Algorithm

  9. Program to computes the n_th term of the fibonacci series using Dynamic Programming Technique

  10. Program that will allow a spy to encode and decode messages

  11. Program that computes and display the sum of first ten integers. (using for loop)

  12. Program to search an element in an array using Linear search or Sequential Search

  13. Program that find the distance between two points in 2D and 3D space using function overloading

  14. Program to illusrate data conversion b/w built-in data types and user defined data types(in char)

  15. Program to illustrate the use of function overloading

  16. Program that take font and background color and text input from a user and display it in center aligned

  17. Program to interchange the values of two int , float and char using function overloading

  18. Program that prints odd numbers, even numbers and the numbers that are divisible by 5 b/w 0 to 50

  19. Program to illustrate the relationship b/w union and structure

  20. Program to rotate the word Computer by 1 results in rCompute

 
 
Didn't find what you were looking for? Find more on Program to show the implementation of Binary Search Tree as Sets