Wednesday, November 23, 2011

CAUTION!!!

வணக்கம்!!!

Just Before now on I have Posted all of the Ten programs in the Data Structures Particles. So there will be nine Blog posts from now on: Numbered. All of them are in CPP. I have Compiled the Programs and Checked them. They are Error Free. But I don't Guarantee it. 


It is my Humble Opinion it will be of Great use to You. If U have any comments or suggestions or Errors.... Please put it up as Comments.


Dont Forget to Share it with your Friends.


Anyway ThankU for visiting my Blog :-)


நன்றி!!!


UPDATE:
Date: 26th November 2011
Time: 21:20::30


I admit that there were Lot of Errors in the Programs, which were posted before in this BLOG. I am deeply SORRY.


But now there is no Problem. I have Compiled all the PROGRAMS and all the Errors have been neatly Patched in the POSTS itself.


If U find any more errors kindly COMMENT upon!!! 


:-)))))))

10 All Pair Shortest Path


#include<iostream>

using namespace std;

int min(int a,int b);
int cost[10][10],a[10][10],i,j,k,c;

int main()
{
  int n,m;

  cout <<"enter no of vertices";
  cin >> n;

  cout <<"enter no of edges";
  cin >> m;

  cout<<"enter the\nEDGE Cost\n";
   for(k=1;k<=m;k++)
  {
  cin>>i>>j>>c;
  a[i][j]=cost[i][j]=c;
  }

 for(i=1;i<=n;i++)
 for(j=1;j<=n;j++)
{
if(a[i][j]== 0 && i !=j)
a[i][j]=31999;
}

for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);

cout <<"Resultant adj matrix\n";
 for(i=1;i<=n;i++)
 {
  for( j=1;j<=n;j++)
  {
  if(a[i][j] !=31999)
  cout << a[i][j] <<" ";
  }
cout <<"\n";
}

return 0;
}

int min(int a,int b)
{
if(a<b)
return a;
else
return b;
} 

Tuesday, November 22, 2011

9 Merge Sort


#include<iostream>

using namespace std ;


int merge(int[],int,int[],int,int[]) ;

int main()
{
int A[50], B[50], C[50], mn=0, m, n,i ;

cout<<"\nEnter the no. of elements of 1st array: ";
cin>> m ;

cout<<"\nEnter the 1st array: ";
 for(i=0 ; i<m ; i++)
 cin>> A[i] ;

cout<<"\nEnter the no. of elements of 2nd array: ";
cin>> n ;
 
mn=m+n ;

cout<<"\nEnter the 2nd array: ";
 for(i=0 ; i<n ; i++)
 cin>> B[i] ;
 
merge(A,m,B,n,C) ;
   
cout<<"\nThe merged array is: " ;
 for(i=0 ; i<mn ; i++)
 cout<<C[i]<<" " ;

return 0;


}

int merge(int A[],int m,int B[],int n,int C[])
{
int a, b, c;

 for(a=0, b=0, c=0; a<m && b<n; )
 {
 if(A[a]<=B[b])
 C[c++]=A[a++] ;
 else
 C[c++]= B[b++] ;
 }

 if(a<m)
 {
 while(a<m)
  C[c++] = A[a++] ;
 }
 else
 {
 while(b<n)
  C[c++] = B[b++] ;
 }

return 0;

}

7 Binary Transversal

Preorder Inorder Postorder (Using RECURSION)


#include<iostream>
#include<cmath>

using namespace std;

void getdata();
void inorder(int);
void preorder(int);
void postorder(int);

int t[50];
int n,i,l;

int main()
{
getdata();

cout<<"\nINORDER";
inorder(1);

cout<<"\nPREORDER";
preorder(1);

cout<<"\nPOSTORDER";
postorder(1);
cout<<"\n";

return 0;

}

void getdata()
 {
 cout<<"\nEnter the no.of levels of the binary tree:";
 cin>>l;

 n=(pow(2,l))-1;
 cout<<"\nThe maximum no.of the nodes for the given level of the binary tree is"<<" "<<n<<"\n";
 cout<<"\nEnter the nodes of the tree:";
 for(i=1;i<=n;i++)
 cin>>t[i];
 }

 void inorder(int i)
 {
  if(i<=n)
  {
  inorder(i*2);
  if(t[i]!=0)
  cout<<"\t"<<t[i];
  inorder(i*2+1);
  }
 }

 void preorder(int i)
 {
  if(i<=n)
  {
  if(t[i]!=0)
  cout<<"\t"<<t[i];
  preorder(i*2);
  preorder(i*2+1);
  }
 }

 void postorder(int i)
 {
  if(i<=n)
  {
  postorder(i*2);
  postorder(i*2+1);
  if(t[i]!=0)
  cout<<"\t"<<t[i];
  }
 }

8 Quick Sort


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

using namespace std;

void exit(int status);
void qusort(int numbers[], int array_size);
void qsort(int numbers[], int left, int right);

int numbers[150];

int main()
{
   int i,n;
   cout<<"How many numbers you want to sort: ";
   cin>>n;
   cout<<"Enter "<<n<<" numbers.\n";
   for (i = 0; i<n; i++)
      cin>>numbers[i];
//perform quick sort on array
   qsort(numbers,0,n-1);

   cout<<"Numbers are sorted\n";
   for (i = 0; i<n; i++)
      cout<<numbers[i]<<"   ";
   return(0);
}

// Function to sort
void qsort(int numbers[], int left, int right)
{
   int pivot, l_hold, r_hold;
   l_hold = left;
   r_hold = right;
   pivot = numbers[left];
   while (left < right)
   {
      while ((numbers[right] >= pivot) && (left < right))
      right--;
      if (left != right)
      {
     numbers[left] = numbers[right];
     left++;
      }
      while ((numbers[left] <= pivot) && (left < right))
     left++;
      if (left != right)
      {
      numbers[right] = numbers[left];
      right--;
      }
   }
   numbers[left] = pivot;
   pivot = left;
   left = l_hold;
   right = r_hold;
   if (left < pivot)
      qsort(numbers, left, pivot-1);
   if (right > pivot)
      qsort(numbers, pivot+1, right);
}

6 Dijkstra's Algorithm


#include<iostream>
using namespace std;

int cost[20][20];

int main()
{
int i,j,c[10][10],dist[10],s[10];
int temp,tempcost,n,src,minm;

cout<<"\n\nEnter the no. of vertices:";
cin>>n;

cout<<"\nGive the cost:";

for(i=1;i<=n;i++)
 {
 for(j=1;j<=n;j++)
 {
  if(i==j)
  {
  c[i][j]=0;
  }
  else
  {
  cout<<"Enter the cost:"<<i<<"to"<<j<<"::";
  cin>>c[i][j];
  }
 }
}

cout<<"\nEnter the vertex number to find the shortest path for all paths:";
cin>>src;

cout<<"\nThe adjacency matrix given is:";
for(i=1;i<=n;i++)
{
 for(j=1;j<=n;j++)
 {
 cout<<c[i][j]<<"\t";
 }
 cout<<"\n";
}

for(i=1;i<=n;i++)
{
s[i]=0;
dist[i]=c[src][i];
}

s[src]=1;dist[src]=0;

for(i=1;i<=n;i++)
{
 for(j=1;j<=n;j++)
 {
  if(s[j]==0)
  {
  minm=dist[j];
  temp=j;
  break;
  }
 }
}

for(j=1;j<=n;j++)
{
 if(s[j]==0 && minm>dist[j])
 {
 minm=dist[j];
 temp=j;
 }
}

s[temp]=1;

for(j=1;j<=n;j++)
{
 if(s[j]==0)
 {
 tempcost=dist[temp]+c[temp][j];
  if(dist[j]>tempcost)
  {
  dist[j]=tempcost;
  }
 }
}

cout<<"\n\nThe vertices are:"<<"\n";
for(i=1;i<=n;i++)
cout<<"\t"<<i;

cout<<"\nThe distances are:"<<"\n";
for(i=1;i<=n;i++)
cout<<dist[i]<<"\t";

return 0;
}

5 Binary - Insertion Search & Deletion


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

using namespace std;

struct node
{
int d;
struct node *left;
struct node *right;
};

struct node *root;


void init();
void empty();
int insert(int);
void search(int);
void remove(int);
 
void init()
{
root=(struct node *)malloc(sizeof(struct node));
root=NULL;
}

int insert(int x)
{
struct node *p,*previous,*current;
p=(struct node *)malloc(sizeof(struct node));
 if (p==NULL)
 {
 cout<<"\nOut of Memory";
 return 0;
 }
p->d=x;
p->left=NULL;
p->right=NULL;
 if(root==NULL)
 {
 root=p;
 return 1;
 }
previous=NULL;
current=root;

 while (current!=NULL)
 {
 previous=current;
  if (p->d<current->d)
  current=current->left;
  else
  current=current->right;
 }

 if(p->d<previous->d)
 previous->left=p;
 else
 previous->right=p;

return 1;
}

void remove(int x)
{
struct node *ptr=root, *parent = NULL, *t1, *t2, *temp;
 while(ptr!=NULL && ptr->d!=x)
 {
 parent=ptr;
 if(x<ptr->d)
  ptr=ptr->left;
 else
  ptr=ptr->right;
 }

 if(ptr==NULL)
 {
 cout<<"\nDelete Element Not found";
 return;
 }
 
 if(ptr->left==NULL)
  t1=ptr->right;
 else if(ptr->right==NULL)
  t1=ptr->left;
 else
 {
 t2=ptr;
 t1=ptr->right;
 temp=t1->left;
 
  while(temp!=NULL)
  {
  t2=t1;
  t1=temp;
  temp=t1->left;
  }
 
  if(t2!=ptr)
  {
  t2->left=t1->right;
  t1->right=ptr->right;
  }

  t1->left=ptr->left;
 }

 if(parent==NULL)
  root=t1;
 else
 {
  if(parent->left==ptr)
  parent->left=t1;
  else
  parent->right=t1;
 }

free(ptr);
}


void search(int sno)
{
struct node*t;
t=(struct node *)malloc(sizeof(struct node));
t=root;
 while(t!=NULL && t->d !=sno)
 {
  if(t->d>sno)
  { t=t->left; }
  else
  { t=t->right; }
 }

 if(t)
 cout<<"\nThe Search Element is Present";
 else
 cout<<"\nElement not Present";
}

int main()
{
int c,no,x;
init();

 do
 {
 cout<<"\n1.Insert";
 cout<<"\n2.Deletion";
 cout<<"\n3.Search";
 cout<<"\n4.Exit";
 cout<<"\nEnter Your Choice:\t";
 cin>>c;
 
  switch(c)
  {
  case 1:cout<<"\nEnter the Element to Insert:\t";
         cin>>no;
         x=insert(no);
          if(x==1)
          cout<<"\nsucessful Insertion";
          else
          cout<<"\nError in insertion";
         break;
  case 2:cout<<"\nEnter the Element to Delete:";
         cin>>x;
         remove(no);
         break;
  case 3:cout<<"\nEnter the Element to Search: \t";
         cin>>no;
         search(no);
         break;
  case 4: exit(0); break;
  default: cout<<"\nEnter Any No. Between 1-7"; break;
  }
 }while(c!=4);
return 0;
}

4 Queue - Linked List


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

using namespace std;

struct node
{
int data;
struct node *link;
};
struct node *cur,*first,*last;

void insert();
void delte();
void display();

void insert()
{
 if(first==NULL)
 {
 cout<<"\nENTER THE FIRST ELEMENT: ";
 cur=(struct node *)malloc(sizeof(struct node));
 cin>>cur->data;
 cur->link=NULL;
 first=cur;
 last=cur;
 }
 else
 {
 cout<<"\nENTER THE NEXT ELEMENT: ";
 cur=(struct node *)malloc(sizeof(struct node));
 cin>>cur->data;
 cur->link=NULL;
 last->link=cur;
 last=cur;
 }
}

void delte()
{
 if(first==NULL)
 {
 cout<<"\t\nQUEUE IS EMPTY\n";
 }
 else
 {
 cur=first;
 first=first->link;
 cur->link=NULL;
 cout<<"\n DELETED ELEMENT IS:";
 cout<<cur->data;
 free(cur);
 }
}

void display()
{
 if(first==NULL)
 cout<<"\nQueue Empty";
 else
 {
 cur=first;
 cout<<"\n";
  while(cur!=NULL)
  {
  cout<<"\t"<<cur->data;
  cur=cur->link;
  }
 }
}

int main()
{
 int ch;
 do
 {
 cout<<"\n 1.INSERT \n 2.DELETE  \n 3.Display \n 4.EXIT ";
 cout<<"\nENTER YOUR CHOICE : ";
 cin>>ch;

  switch(ch)
  {
  case 1: insert(); break;
  case 2: delte(); break;
  case 3: display(); break;
  case 4: exit(0);
  default:cout<<"\nInvalid Choice";
  }
 }while(ch!=4);
return 0;
}

3 Stack - Linked List


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

using namespace std;

void push();
void pop();
void display();


struct node
{
int info;
struct node *link;
} *top=NULL;

int main()
{
int choice;

 do
 {
 cout<<"\n\n1.Push\n";
 cout<<"2.Pop\n";
 cout<<"3.Display\n";
 cout<<"4.Quit\n";
 cout<<"Enter your choice : ";
 cin>>choice;

  switch(choice)
  {
  case 1:push();break;
  case 2:pop();break;
  case 3:display();break;
  case 4:exit(1);
  default :cout<<"Wrong choice\n";
  }
 }while(choice!=4);
return 0;
}

void push()
{
struct node *tmp;
int pushed_item;

tmp = (struct node *)malloc(sizeof(struct node));

cout<<"Value to Push:";
cin>>pushed_item;

tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}

void pop()
{
struct node *tmp;
 if(top == NULL)
 cout<<"Stack is empty\n";
 else
 {
 tmp=top;
 cout<<"Popped item is "<<tmp->info;
 top=top->link;
 free(tmp);
 }
}

void display()
{
struct node *ptr;
ptr=top;
 if(top==NULL)
 cout<<"Stack is empty\n";
 else
 {
 cout<<"Stack elements :\n";
  while(ptr!= NULL)
  {
  cout<<"\n"<<ptr->info;
  ptr = ptr->link;
  }
 }
}

2 Queue - Array


# include<iostream>

# define MAX 5



using namespace std;

int qu[MAX];

int r = -1;

int f = -1;

void insert();
void del();
void disply();



int main()

{

int choice;


 do

 {

 cout<<"1.Insert\n";

 cout<<"2.Delete\n";

 cout<<"3.Display\n";

 cout<<"4.Quit\n";

 cout<<"Enter your choice : ";

 cin>>choice;



  switch(choice)

  {

  case 1 :insert();break;

  case 2 :del();break;

  case 3:display();break;

  case 4:exit(1);

  default:
cout<<"Wrong choice\n";

  }

 }while(choice!=4);

return 0;
}



void insert()

{

int added_item;


 if (r==MAX-1)

 cout<<"Queue Overflow\n";

 else

 {

  if (f==-1)
  f=0;

  cout<<"Input the element for adding in queue : ";

  cin>>added_item;

  r=r+1;

  qu[r] = added_item ;

 }

}



void del()

{

 if (f == -1 || f > r)

 {

 cout<<Queue Underflow\n";

 return ;

 }

 else

 {

 cout<<"Element deleted from queue is :"
 cout<<"\n"<<qu[f];

 f=f+1;

 }

}



void display()

{

int i;

 if (f ==-1 )

 cout<<Queue is empty\n";

 else

 {

 cout<<"Queue is :\n";

 for(i=f;i<= r;i++)

 cout<<qu[i]<<"\n";

 }

}

1 Stack - Array


#include<iostream>

#define MAX 5



using namespace std;

int top = -1;

int stack[MAX];



void push();
void pop();
void display();


int main()

{

int choice;


 do

 {

 cout<<"\n\n1.Push\n";

 cout<<"2.Pop\n";

 cout<<"3.Display\n";

 cout<<"4.Quit\n";

 cout<<"Enter your choice : ";

 cin>>choice;



  switch(choice)

  {

  case 1 :push();
break;

  case 2:pop();
break;

  case 3:display();
break;

  case 4:exit(1);


  default:cout<<"Wrong choice\n";

  }
 }while(choice!=4);
return 0;
}



void push()

{

int p;


if(top == (MAX-1))

cout<<"Stack Overflow\n";

 else

 {

 cout<<"Enter the item to be pushed in stack : ";

 cin>>p;
 top=top+1;

 stack[top] = p;

 }

}



void pop()

{

 if(top == -1)

 cout<<Stack Underflow\n";

 else

 {

 cout<<"Popped element is : "
 cin>>stack[top];

 top=top-1;

 }

}



void display()

{

int i;

 if(top == -1)

 cout<<"Stack is empty\n";

 else

 {

 cout<<"Stack elements :\n";

 for(i = top; i >=0; i--)

 cout<<"\n"
 cout<<stack[i];

 }

}

Friday, November 4, 2011

Binary Tree

Here the Program given Below Does all of the Following Functions In BINARY TREE:
  1. Inserting a Element
  2. Deleting a Element
  3. Preorder
  4. Inorder
  5. Postorder
  6. Searching an Element
(NOTE: the Inorder, PreOrder,PostOrder are in recursion ONLY!!!)

Thanks to தமிழரசு for the book DATA  STRUCTURES USING C - P.RadhGanesan from where this Program is written

#include<iostream>

#include<stdlib.h>


using namespace std;



struct node

{

int d;

struct node *left;

struct node *right;

};



struct node *root;


void init();

void empty();

int insert(int);

void search(int);

void print(int);
void preorder(struct node *);
void inorder(struct node *);
void postorder(struct node *);
void remove(int);
 



void init()

{

root=(struct node *)malloc(sizeof(struct node));

root=NULL;

}



int insert(int x)

{

struct node *p,*previous,*current;


p=(struct node *)malloc(sizeof(struct node));



 if (p==NULL)

 {

 cout<<"\nOut of Memory";

 return 0;

 }


p->d=x;

p->left=NULL;

p->right=NULL;


 if(root==NULL)

 {

root=p;

 return 1;

 }
 


previous=NULL;

current=root;



 while (current!=NULL)

 {

 previous=current;

 
if (p->d<current->d)
 
  current=current->left;

  else
 
  current=current->right;

 }



if(p->d<previous->d)
 
previous->left=p;

else
 
previous->right=p;


return 1;


}



void inorder(struct node *p)
{
 if(p!=NULL)
 {
 inorder(p->left);
 cout<<"\t"<<p->d;
 inorder(p->right);
 }
}

void preorder(struct node *p)
{
 if(p!=NULL)
 {
 cout<<"\t"<<p->d;
 preorder(p->left);
 preorder(p->right);
 }
}

void postorder(struct node *p)
{
 if(p!=NULL)
 {
 postorder(p->left);
 postorder(p->right);
 cout<<"\t"<<p->d;
 }
}

void remove(int x)
{
struct node *ptr=root, *parent = NULL, *t1, *t2, *temp;
 while(ptr!=NULL && ptr->d!=x)
 {
 parent=ptr;
 if(x<ptr->d)
  ptr=ptr->left;
 else
  ptr=ptr->right;
 }

 if(ptr==NULL)
 {
 cout<<"\nDelete Element Not found";
 return;
 }
 
 if(ptr->left==NULL)
  t1=ptr->right;
 else if(ptr->right==NULL)
  t1=ptr->left;
 else
 {
 t2=ptr;
 t1=ptr->right;
 temp=t1->left;
 
  while(temp!=NULL)
  {
  t2=t1;
  t1=temp;
  temp=t1->left;
  }
 
  if(t2!=ptr)
  {
  t2->left=t1->right;
  t1->right=ptr->right;
  }

  t1->left=ptr->left;
 }

 if(parent==NULL)
  root=t1;
 else
 {
  if(parent->left==ptr)
  parent->left=t1;
  else
  parent->right=t1;
 }

free(ptr);
}


void print(int x)
{
if(x==1)
 preorder(root);
else if(x==2)
 inorder(root);
else
 postorder(root);
}


void search(int sno)

{

struct node*t;

t=(struct node *)malloc(sizeof(struct node));

t=root;



 while(t!=NULL && t->d !=sno)

 {

if(t->d>sno)
{ t=t->left; }

else
 { t=t->right; }

}



if(t)

cout<<"\nThe Search Element is Present";

else

cout<<"\nElement not Present";

}




int main()

{

int c,no,x;

init();



do

{

cout<<"\n1.Insert";

cout<<"\n2.PreOrder";

cout<<"\n3.InOrder";
cout<<"\n4.PostOrder";
cout<<"\n5.Deletion";
cout<<"\n6.Search";
cout<<"\n7.Exit";

cout<<"\nEnter Your Choice:\t";

cin>>c;



switch(c)

{

case 1: cout<<"\nEnter the Element to Insert:\t";
       
        cin>>no;

        x=insert(no);

        if(x==1)

         cout<<"\nsucessful Insertion";

        else

         cout<<"\nError in insertion";

        break;

case 2:if(root!=NULL)
       {
       cout<<"\nPreOrder is:";
       print(1);
       cout<<"\n";
       }
       else
       cout<<"\nNo Element to Display";
       break;
case 3:if(root!=NULL)
       {
       cout<<"\nInOrder is:";
       print(2);
       cout<<"\n";
       }
       else
       cout<<"\nNo Element to Display";
       break;
case 4:if(root!=NULL)
       {
       cout<<"\nPostOrder is:";
       print(3);
       cout<<"\n";
       }
       else
       cout<<"\nNo Element to Display";
       break;
case 5:cout<<"\nEnter the Element to Delete:";
       cin>>x;
       remove(no);
       break;
case 6: cout<<"\nEnter the Element to Search: \t";

        cin>>no;

        search(no);

        break;

case 7: exit(0); break;

default: cout<<"\nEnter Any No. Between 1-7"; break;

}


}while(c!=7);


return 0;

}



Quick Sort

#include<iostream>
using namespace std;

#include <stdlib.h>

void exit(int status);

void quickSort(int numbers[], int array_size);
void q_sort(int numbers[], int left, int right);

int numbers[150];

int main()
{
   int i,n;
   cout<<"How many numbers you want to sort: ";
   cin>>n;
   cout<<"Enter "<<n<<" numbers.\n";
   for (i = 0; i<n; i++)
      cin>>numbers[i];
//perform quick sort on array
   q_sort(numbers,0,n-1);

   cout<<"Numbers are sorted\n";
   for (i = 0; i<n; i++)
      cout<<numbers[i]<<"   ";
   return(0);
}

// Function to sort
void q_sort(int numbers[], int left, int right)
{
   int pivot, l_hold, r_hold;
   l_hold = left;
   r_hold = right;
   pivot = numbers[left];
   while (left < right)
   {
      while ((numbers[right] >= pivot) && (left < right))
      right--;
      if (left != right)
      {
     numbers[left] = numbers[right];
     left++;
      }
      while ((numbers[left] <= pivot) && (left < right))
     left++;
      if (left != right)
      {
      numbers[right] = numbers[left];
      right--;
      }
   }
   numbers[left] = pivot;
   pivot = left;
   left = l_hold;
   right = r_hold;
   if (left < pivot)
      q_sort(numbers, left, pivot-1);
   if (right > pivot)
      q_sort(numbers, pivot+1, right);
}

Merge Sort

#include<iostream>
using namespace std ;
void merge(int[],int,int[],int,int[]) ;
int main()
{    int A[50], B[50], C[50], mn=0, m, n ;
    cout<<"\nEnter the no. of elements of 1st array: ";
   cin>> m ;
   cout<<"\nEnter the 1st array: ";
   for(int i=0 ; i<m ; i++)
       cin>> A[i] ;
   cout<<"\nEnter the no. of elements of 2nd array: ";
   cin>> n ;
   mn=m+n ;
   cout<<"\nEnter the 2nd array: ";
    for(int i=0 ; i<n ; i++)
       cin>> B[i] ;
      merge(A,m,B,n,C) ;
      cout<<"\nThe merged array is: " ;
      for(int i=0 ; i<mn ; i++)
           cout<<C[i]<<" " ;
return 0; 
}

void merge(int A[],int m,int B[],int n,int C[])
{    int a, b, c;
    for(a=0, b=0, c=0; a<m && b<n; )
   {    if(A[a]<=B[b])     C[c++]=A[a++] ;
           else C[c++]= B[b++] ;
   }
   if(a<m)
       {    while(a<m)
              C[c++] = A[a++] ;
      }
   else
       {    while(b<n)
          C[c++] = B[b++] ;
      }
}

Friday, October 28, 2011

Binary Search TREE

#include<iostream>

#include<stdlib.h>


using namespace std;



struct node

{

int d;

struct node *left;

struct node *right;

};



struct node *root;


void init();

void empty();

int insert(int);

void search(int);


 



void init()

{

root=(struct node *)malloc(sizeof(struct node));

root=NULL;

}



int insert(int x)

{

struct node *p,*previous,*current;


p=(struct node *)malloc(sizeof(struct node));



 if (p==NULL)

 { 

 cout<<"\nOut of Memory";

 return 0;

 }


p->d=x;

p->left=NULL;

p->right=NULL;


 if(root==NULL)

 {

root=p;

 return 1;

 }
 


previous=NULL;

current=root;



 while (current!=NULL)

 {

 previous=current;

 
if (p->d<current->d)
 
  current=current->left;

  else
 
  current=current->right;

 }



if(p->d<previous->d)
 
previous->left=p;

else
 
previous->right=p;


return 1;


}








void search(int sno)

{

struct node*t;

t=(struct node *)malloc(sizeof(struct node));

t=root;



 while(t!=NULL && t->d !=sno)

 {

if(t->d>sno)
{ t=t->left; }

else
 { t=t->right; }

}



if(t)

cout<<"\nThe Search Element is Present";

else

cout<<"\nElement is not Present";

}




int main()

{

int c,no,x;

init();



do

{

cout<<"\n1.Insert";
cout<<"\n2.Search";
cout<<"\n3.Exit";

cout<<"\nEnter Your Choice:\t";

cin>>c;



switch(c)

{

case 1: cout<<"\nEnter the Element to Insert:\t";
        
        cin>>no;

        x=insert(no);

        if(x==1)

         cout<<"\nsucessful Insertion";

        else

         cout<<"\nError in insertion";

        break;

case 2: cout<<"\nEnter the Element to Search: \t";
        cin>>no;

        search(no);

        break;

case 3: exit (0); break;

default: cout<<"\nEnter Any No. Between 1/3";

}


}while(c<3);


return 0;

}

Saturday, February 26, 2011

Files


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

void main()
{
fstream ob;

ob.open("stud1.dat",ios::out|ios::binary);

int rollno,mark,entry;

clrscr();

cout<<"\n\n\n\n\t\t\tEnter the serial no[0 to exit]:";
cin>>entry;

 while(entry!=0)
 {
 cout<<endl<<"\n\n\t\tEnter the stud no: ";
 cin>>rollno;

 cout<<"\n\n\t\tEnter the mark: ";
 cin>>mark;

 cout<<endl;

 ob<<setw(0)<<rollno<<setw(10) <<mark<<endl;

 cout<<"\n\n\n\t\t\tEnter serial no[0 to exit]";
 cin>>entry;

 }

ob.close();

getch();

}

Friday, February 25, 2011

Inheritance


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

class shape
{
public:
float a;
};

class triangle: public shape
{
int a,b,c;
float s;

public:
 triangle(int x,int y,int z)
 {
 a=x;b=y;c=z;
 s=(a+b+c)/2.0;
 }

 void area()
 {
 a=sqrt(s*(s-a)*(s-b)*(s-c));
 }

 void show()
 {
 cout<<"\nThe Area of the Triangle is : "<<a;
 }

};

class rectangle: public shape
{
int l,b;

public:
 rectangle(int x,int y)
 {
 l=x;
 b=y;
 }

 void area()
 {
 a=l*b;
 }

 void show()
 {
 cout<<"\n"<<"The Area of the Rectangle is : "<<a;
 }

};

class circle: public shape
{
int r;
public:

 circle(int x)
 {
 r=x;
 }

 void area()
 {
 a=3.14*r*r;
 }

 void show()
 {
 cout<<"\n"<<"Area of the Circle is : "<<a;
 }

};

void main()
{
clrscr();

triangle ob(20,20,20);
ob.area();
ob.show();

rectangle ob1(6,5);
ob1.area();
ob1.show();

circle ob2(7);
ob2.area();
ob2.show();

getch();
}



Thursday, February 24, 2011

Constructors and Destructors


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

class fixed
{
int a,b;
public:

 fixed()
 {
 cout<<"\nThe Constructor has Zero Arguments\n";
 cout<<"\n";
 }

 fixed(int x)
 {
 a=x;
 cout<<"The Constructor has One Argument"<<"\n\n";
 cout<<a<<"\n\n";
 }

 fixed(int x,int y)
 {
 a=x;
 b=y;
 cout<<"This Constructor Has Two Arguments:"<<"\n\n";
 cout<<a<<"\n"<<b;
 }

 ~fixed()
 {
 cout<<"\n Objects Destroyed";
 cout<<"\n";
 }

};

void main()
{
clrscr();

fixed ob;
fixed ob1(10);
fixed ob2(20);
fixed ob3(10,20);

getch();
}

Default Arguments


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

class find
{
public:
int a,b;

 void get(int x,int y=20)
 {
 a=x;
 b=y;
 }

 void display()
 {
 cout<<"\n A : " <<a<<"\n";
 cout<<"\n B : " <<b<<"\n";
 }

};

void main()
{
clrscr();

find ob;

ob.get(10);

ob.display();
find ob1;

ob1.get(20,30);

ob1.display();

getch();

}


Wednesday, February 23, 2011

Classes and Objects


CPP Programs. Banking Program

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

class banking
{
public:
char name[25];
char type[30];
char number[20];
long int balance;
long int dep,with;

 void create()
 {
 cout<<"\n\t\tEnter name: ";
 cin>>name;
 cout<<"\n\t\tEnter account type: ";
 cin>>type;
 cout<<"\n\t\tEnter account number: ";
 cin>>number;
 cout<<"\n\t\tInitial balance: ";
 cin>>balance;
 }

 void deposit()
 {
 cout<<"\n\t\tEnter the amount to be deposited: ";
 cin>>dep;
 balance=balance+dep;
 cout<<"\n\t\tCurrent balance: "<<balance;
 }

 void withdraw()
 {
 cout<<"\n\t\tEnter the amount to be withdrawn: ";
 cin>>with;
 balance=balance-with;
 cout<<"\n\t\tCurrent Balance: "<<balance;
 }

 void display()
 {
 cout<<"\n\t\tName of the customer: "<<name;
 cout<<endl;
 cout<<"\n\t\tAccount Number: "<<number;
 cout<<endl;
 cout<<"\n\t\tType of account: "<<type;
 cout<<endl;
 cout<<"\n\t\tBalance: "<<balance;
 }

};

void main()
{
banking ob;
int x;

clrscr();


 do
 {
 cout<<endl;
 cout<<"\n\n\n\t\t\tWelcome to State Bank  Of India ";
 cout<<"\n\n\t\t 1.Create a new account";
 cout<<"\n\n\t\t 2.Deposit amount";
 cout<<"\n\n\t\t 3.Withdraw amount";
 cout<<"\n\n\t\t 4.Display";
 cout<<"\n\n\t\t 5.Exit";
 cout<<"\n\n\t\t Select your choice: ";
 cin>>x;
 
  switch(x)
  {
  case 1:ob.create();break;
  case 2:ob.deposit();break;
  case 3:ob.withdraw();break;
  case 4:ob.display();break;
  case 5:break;
  default:cout<<"Enter any value b/w 1-5";
  }

 }while(x!=5);

}



Tuesday, February 22, 2011

Palindrome Program!!

Palindrome is a word which reads the same from both sides. Example is MADAM. This program uses the built-in functions from the directory 'string.h' , to determine whether the given string is a palindrome or not. 

The command 'strrev' reverses the string and 'strcpy' copies one string to another for comparision.


PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[80],b[80];
clrscr();
printf("\nEnter the String:");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
printf("\nThe String is Palindrome");
else
printf("\nThe String is Non-Palindrome");
getch();
}


String Reverse Program!!

This program uses the built-in function to reverse the given string. It is defined in the header file 'string.h' with the command 'strrev( )'

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[80];
int l;
clrscr();
printf("\nEnter the String:");
gets(a);
l=strlen(a);
printf("\nThe Length of %s is %d",a,l);
getch();
} 


String Lenth Program!!

The programs given below calculate the lenth of the given string. (a) is the program for calculating the length without using the built-in functions , whereas (b) uses the built-in function from the header file <string.h>


(a)
The length of the string is the number of spaces it occupies. It is calculated using the for control structure. When the sring ends the system assingns '\0' for the next space.

For example, Let us take the string 'an' , a[0]='a' ; a[1]='n' ; a[2]='\0'.
 Hence by finding the ith position in which a[i]='\0', the string length is determined.


PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char a[80];
int l;
clrscr();
printf("\nEnter the String:");
gets(a);
l=sl(a);
printf("\nLength of String A is %d",l);
getch();
}

int sl(char str[])
{
int i;
  for (i=0;i<=80;i++)
   {
     if (str[i]=='\0')
     return(i);
   }
}


Matrix Program!!

This Program helps us to add a 3*3 matrix. The concept of 'arrays'is used here. For loops are used to get the inputs and print them. To add matrix of order n*n change all the 3 to n.

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
 {
  int a[3][3],b[3][3],c[3][3];
  int i,j;
  clrscr();
  printf("\nEnter the first matrix:");
  for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      scanf("%d",&a[i][j]);
  printf("\nThe first matrix is");
  for(i=0;i<3;i++)
   {
    printf("\n");
    for(j=0;j<3;j++)
     printf ("\t%d",a[i][j]);
   }
  printf ("\nEnter the second matrix is");
  for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      scanf("\t%d",&b[i][j]);
  printf("\nThe Second Matrix is:");
  for(i=0;i<3;i++)
   {
    printf("\n");
    for(j=0;j<3;j++)
     printf ("\t%d",b[i][j]);
   }
  for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      c[i][j]=a[i][j]+b[i][j];
  printf("\nThe Resultant matrix is:");
  for(i=0;i<3;i++)
   {
    printf("\n");
    for(j=0;j<3;j++)
     printf ("\t%d",c[i][j]);
   }
  getch();
}

Monday, February 21, 2011

Functions Program!!

This Program illustrates the use of  'functions'. The first function adds while the second subtracts the given 2 numbers

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
 {
  int a,b;
  clrscr();
  printf("Enter A:");
  scanf("%d",&a);
  printf("Enter B:");
  scanf("%d",&b);
  sum(a,b);
  diff(a,b);
  getch();
 }

sum(int x,int y)
 {
  int ad;
  ad=x+y;
  printf("Sum=%d",ad);
 }

diff(int x,int y)
 {
  int su;
  su=x-y;
  printf("Difference=%d",su);
 } 


Fibonacci Series Program!!

This program generates the fibonacci series upto the number of terms which we desire. 'for' Control Structure is used.

LOGIC:
Fibonacci Series=Fn  
were the seed values are
 

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
  int i,j,k,n,x;
  clrscr();
  i=0;
  j=1;
  printf("Enter the number of terms:");
  scanf("%d", &x);
  printf("%d    %d    ",i,j);
  for(n=0;n<=x;n++)
  {
    k=i+j;
    i=j;
    j=k;
    printf("%d    ",k);
  }
getch();
}

Factorial Program!!

This program computes the factorial of the given number and displays it. The control structure 'for' is used to determine the factorial. When there is a number of iteration is carried out till a desired output or condition is solved , we go for the 'for' statement.

LOGIC:
 Factroial of 'n' is denoted as 'n!' 

 

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
j=1;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
j=j*i;
}
printf("\n Factorial of %d is :%d",n,j);
getch();
}

Choices Program!!

This Program mainly illustrates the use of 'Switch" control structure. When there are varied number of options and when we have to select 1 option , 'switch' is used.

We first get the values of 2 numbers and enter our choice to add,subtract,multiply or divde the numbers.

Important points to remember while using 'switch':
  • The value that is being switched must be an integer [x in this program].
  • There must be a space between the 'case' ant the no. [case 1:]
  • Every caes must end up with the 'break'.
  • 'default' is mandatory. 'break' is not necessary for default.
 PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
 {
  int a,b,x;
  float c;
  clrscr();
  printf("Enter A:");
  scanf("%d",&a);
  printf("Enter B:");
  scanf("%d",&b);
  printf("\n");
  printf("1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide\n");
  printf("Enter your choice:");
  scanf("%d",& x);
  switch(x)
   {
    case 1: c=a+b;
        printf("The Sum is:%f",c);
        break;
    case 2: c=a-b;
        printf("The difference is:%f", c);
        break;
    case 3: c=a*b;
        printf("The Product is:%f", c);
        break;
    case 4: c=a/b;
        printf("The Quotient is:%f",c);
        break;
    default: printf ("Invalid option");
   }
  getch();
 }
 


Sunday, February 20, 2011

Leap Year Program!!

This program determines whether the given year is a leap year or not.This problem also illustrates the use of the control statemen if-else!!

LOGIC:
If the year is divisible by 4 it is a leap year.
If the year is divisible by 100 it is not a leap year.
If the year is divisible by 400 it is a leap year.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
 int year;
 clrscr();
 printf("Enter the year:");
 scanf("%d",&year);
 if(year%100==0)
 {
   if(year%400==0)
  {
   printf("%d Is a leap year",year);
   }
 else
  {
   printf("%d Is a non-leap year",year);
   }
   }
  else
  {
   if(year%4==0)
  {
   printf("%d: It is a leap year",year);
   }
   else
  {
   printf("%d: It is not a leap year",year);
   }
  }
 printf("\n~!Anyway have a nice year!~");
 getch();
}