Tuesday, November 22, 2011

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