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
 
 

Image

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

int display_menu();
int array_no=1; //CURRENT ARRAY 1 OR 2
class array
{
 int arr[20];
 int n;
 public:
    array()
    {
     n=0;
    }
    void add_n(int);
    int getval(int);
    int getsize();
    void getelements();
    void display();
    void insert();
    void delete_element();
    void merge_array(array);
};

int array :: getsize()
{
 return n;
}
void array :: add_n(int x)
{
 n=n+x;
}
int array :: getval(int i)
{
 return (arr[i]);
}
void array :: getelements()
{
  cout<<\"Enter Number of elements you want to enter :\";
  cin>>n;
  for(int i=0;i<n;i++)
  {
   cout<<\"a[\"<<i<<\"] :=>\";
   cin>>arr[i];
  }
}

void array :: display()
{
  cout<<endl;
  if(n==0)
  {
   cout<<endl<<\"No Element !!!\"<<endl;
  }

  for(int i=0;i<n;i++)
  {
    cout<<\"a[\"<<i<<\"] :\"<<arr[i]<<endl;
  }
}

void array :: insert()
{
  display();
  int pos;
  cout<<endl<<\"[Enter Number Between 0 \"<<\"And \"<<n<<endl;;
  cout<<endl<<\"Enter Position Where you want to Insert :\";
  cin>>pos;
  if(pos < 0 || pos >n)
  {
   cout<<\"Out of Range !!!\";
  }
  else
  {
  for(int i=n;i>pos;i--)
  {
   arr[i]=arr[i-1];
  }
  cout<<\"Enter Number :\";
  cin>>arr[pos];
  cout<<\"Number Inserted at the Position \"<<pos<<endl;
  n=n+1;
  }
}

void array :: delete_element()
{
  display();
  //Delete Multiple Occurance of a Number in an Array
  int no,i,pos,j;
  cout<<endl;
  cout<<\"Enter Number to Delete :\";
  cin>>no;

  for(i=0;i<n;i++)
  {
   pos=i;
   if(arr[i]==no)
   {
    for(j=pos;j<n-1;j++)
    {
     arr[j]=arr[j+1];
    }
    n=n-1;
    i--;
   }
  }
}

void array :: merge_array(array a1)
{
  for(int i=0;i<a1.getsize();i++)
  {
    arr[i+n]=a1.getval(i);
  }
  add_n(a1.getsize());
  cout<<\"Second Array merged with current array ...\";
}






void main()
{
 clrscr();
  array a1,a2;
 int arr_no;
 while(1)
 {
 switch(display_menu())
 {
  case 1: cout<<\"Select Array  [ 1 Or 2 ]\";
      cin>>arr_no;
      if(arr_no==1 || arr_no==2)
      {
       array_no=arr_no;
      }
      else
      {
       cout<<\"Invalid Number entered !!!!\";
       getch();
      }
      break;
  case 2 : if(array_no==1)
       {
        a1.getelements();
       }
       else
       {
        a2.getelements();
       }
       break;
  case 3 : if(array_no==1)
       {
         a1.insert();
       }
       else
       {
         a2.insert();
       }
       getch();
       break;
  case 4 : if(array_no==1)
       {
         a1.delete_element();
       }
       else
       {
        a2.delete_element();
       }
       getch();
       break;

  case 5 : if(array_no==1)
       {
        a1.display();
       }
       else
       {
        a2.display();
       }
       getch();
       break;









  case 6 : if(array_no==1)
       {
         a1.merge_array(a2);
       }
       else
       {
         a2.merge_array(a1);
       }
       getch();
       break;
  case 7 : exit(1);
 }
 }
}

int display_menu()
{
  int ch;
  clrscr();
  cout<<endl<<\"\\t\\t\\t[ Current Array is :\"<<array_no<<\"]\"<<endl;
  cout<<\"\\t\\t\\t| 1 | - Select Array\"<<endl;
  cout<<\"\\t\\t\\t| 2 | - Enter Elements\"<<endl;
  cout<<\"\\t\\t\\t| 3 | - Insert\"<<endl;
  cout<<\"\\t\\t\\t| 4 | - Delete\"<<endl;
  cout<<\"\\t\\t\\t| 5 | - Display\"<<endl;
  cout<<\"\\t\\t\\t| 6 | - Merge Second Array\"<<endl;
  cout<<\"\\t\\t\\t| 7 | - Exit\"<<endl;
  cout<<\"\\t\\t\\tEnter Your Choice :\";
  cin>>ch;
  return(ch);
}