Monday 26 October 2015

Binary tree

#include <iostream>

using namespace std;

struct  node
{
   node* leftchild;
   node* rightchild;
   int data;
};

int count;

node* insert(node*ptr,int x)

     if(ptr==NULL)
       {
          node* temp= new node;
          temp->leftchild=NULL;
          temp->rightchild=NULL;
          temp->data=x;
          ptr=temp;
          count++;
       }
      else if(count%2==0)
          ptr->leftchild=insert(ptr->leftchild,x);

      else
          ptr->rightchild=insert(ptr->rightchild,x);

return ptr;

}

void preorder(node* root)
{
    if(root!=NULL)
{
    cout<<root->data<<endl;
    preorder(root->leftchild);
    preorder(root->rightchild);

}
}

void inorder(node *root)
{
    if (root!= NULL)
    {
        inorder(root->leftchild);
        cout<<root->data<<endl;
        inorder(root->rightchild);
    }
}

void postorder( node *root)
{
    if (root != NULL)
    {
        postorder(root->leftchild);

        postorder(root->rightchild);
        cout<<root->data<<endl;
    }
}

int main()
{
    count=0;int x;
    node* root=NULL;

    char ch='y';
    while(ch!='n')
    {
        cout<<"Enter the element:";
        cin>>x;
        root=insert(root,x);
        cout<<"Do you want to enter more elements? y/n";
        cin>>ch;
    }

cout<<"Preorder traversal:";
preorder(root);
cout<<"Inorder traversal:";
inorder(root);
cout<<"Postorder traversal";
postorder(root);
}

Binary search tree

#include <iostream>

using namespace std;
struct node
{
    int data;
     node* left;
     node* right;
};

void preorder(struct node *root)
{
    if (root != NULL)
    {   cout<<root->data<<endl;
        preorder(root->left);

        preorder(root->right);
    }
}
void inorder(struct node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        cout<<root->data<<endl;
        inorder(root->right);
    }
}

void postorder(struct node *root)
{
    if (root != NULL)
    {
        postorder(root->left);

        postorder(root->right);
        cout<<root->data<<endl;
    }
}

node* insert(node* ptr, int x)
{

    if (ptr == NULL)
       {
            node* temp =  new node;

            temp->data = x;
            temp->left = temp->right = NULL;
            ptr=temp;

       }

    else  if (x < ptr->data)
        ptr->left  = insert(ptr->left,x);
    else if (x> ptr->data)
        ptr->right = insert(ptr->right,x);

    return ptr;
}
int main()
{int x;

    struct node *root = NULL;
    char ch='y';
    while(ch!='n')
    {
        cout<<"Enter the element:";
        cin>>x;
        root= insert(root,x);
        cout<<"Do you want to insert more elements y/n:";
        cin>>ch;

    }

cout<<"Preorder traversal:";
preorder(root);
cout<<"Inorder traversal:";
inorder(root);
cout<<"Postorder traversal";
postorder(root);

    return 0;
}

Sunday 11 October 2015

Simple calculator using switch case

#include <iostream>

using namespace std;

int main()
{long first,second;
char ch;
cout<<"ENTER FIRST INTEGER:";
cin>>first;
cout<<"ENTER SECOND INTEGER";
cin>>second;
cout<<"ENTER THE OPERAND";
cin>>ch;
switch(ch)
{
     case '+' : cout<<"THE SUM IS:"<<first +second;
                break;
     case '-' : cout<<"THE DIFFERENCE IS:"<<first -second;
                break;
     case '/' : cout<<"THE QUOTIENT IS:"<<first/second;
                cout<<"THE REMAINDER IS:"<<first%second;
                break;
     case '*' : cout<<"THE PRODUCT IS:"<<first*second;
                break;

}

}

Monday 21 September 2015

Reversal of linked list

#include <iostream>
using namespace std;
struct node
          {
             int data ;
             node* next;
          };


node* head;
void reverse()
        {

             node* previous;
             node* current;
             node* front;
             previous=current=front=head;
                 while(front!=NULL)
                      {

                           if(current==head)
                             {

                                   front=current->next;
                                   current->next=NULL;

                              }
                           current=front;
                           front=current->next;
                           current->next=previous;
                           previous=current;
                      }

            head=current;

   }

void push(int x)
    {   

        node * temp=new node();
        temp->data=x;
        if(head==NULL)
             {
                 head =temp;
                 temp->next=NULL;
             }
  else
     {   temp->next=head;
        head=temp;
     }
}


void show()

node*temp1=head;

while(temp1!=NULL)

      {

            cout<<temp1->data<<"->";
            temp1=temp1->next;
       }

  cout<<"NULL"<<endl;
    }


int main()
{

    head=NULL;
    int x;
    int n;
    cout<<"How many elements do you want to enter";
    cin>>n;
    while(n--)
    {

         cout<<"Enter data";
         cin>>x;
         push(x);
      }
    cout<<"The linked list is"<<endl;
   
    show();
    reverse();
    cout<<"The reversed linked list is"<<endl;
    show();

}

Stack implementation using linked list

#include <iostream>
using namespace std;
struct node
   {
     int data ;
     node* next;
   };
node* head;
void insert(int x)
    {   node * temp=new node();
        temp->data=x;
          if(head==NULL)
             {
                 head =temp;
                 temp->next=NULL;
             }
  else
     {   temp->next=head;
        head=temp;
     }
}
void del()
{
     if(head==NULL)
        cout<<"Stack is empty"<<endl; 
    else
    head=head->next;
   
}
void show()
{ node*temp1=head;
while(temp1!=NULL)
{cout<<temp1->data<<"->";
       
       
    temp1=temp1->next;
    }
    cout<<"NULL"<<endl;
    }
int main()
{head=NULL;
char ch='y';int n;
    int x;
while(ch=='y'||ch=='Y')
    {
        cout<<"Enter 1 for insertion"<<endl;
        cout<<"Enter 2 for deletion"<<endl;
        cin>>n;
         switch (n)
        { case 1 : cout<<"Enter data:";
                   cin>>x;
                
           insert (x);
            break;
case 2 : del();
        break;
}
        cout<<"The the Stack is:"<<endl;
        show();
        cout<<"Do you want to continue operation  y/n"<<endl;
    cin>>ch;
    }
}

Linked list insertion and deletion at begining

#include <iostream>
using namespace std;
struct node
   {
     int data ;
     node* next;
   };
node* head;
void insert(int x)
    {   node * temp=new node();
        temp->data=x;
          if(head==NULL)
             {
                 head =temp;
                 temp->next=NULL;
             }
  else
     {   temp->next=head;
        head=temp;
     }
}
void del()
{
     if(head==NULL)
        cout<<"Linked is          empty"<<endl; 
    else
    head=head->next;
   
}
void show()
{ node*temp1=head;
while(temp1!=NULL)
{cout<<temp1->data<<"->";
       
       
    temp1=temp1->next;
    }
    cout<<"NULL"<<endl;
    }
int main()
{head=NULL;
char ch='y';int n;
    int x;
while(ch=='y'||ch=='Y')
    {
        cout<<"Enter 1 for insertion"<<endl;
        cout<<"Enter 2 for deletion"<<endl;
        cin>>n;
         switch (n)
        { case 1 : cout<<"Enter data:";
                   cin>>x;
                
           insert (x);
            break;
case 2 : del();
        break;
}
        cout<<"The linked list is:"<<endl;
        show();
        cout<<"Do you want to continue operation  y/n"<<endl;
    cin>>ch;
    }
}

Linked list insertion and deletion at begining

#include <iostream>
using namespace std;
struct node
   {
     int data ;
     node* next;
   };
node* head;
void insert(int x)
    {   node * temp=new node();
        temp->data=x;
          if(head==NULL)
             {
                 head =temp;
                 temp->next=NULL;
             }
  else
     {   temp->next=head;
        head=temp;
     }
}
void del()
{
     if(head==NULL)
        cout<<"Linked is          empty"<<endl; 
    else
    head=head->next;
   
}
void show()
{ node*temp1=head;
while(temp1!=NULL)
{cout<<temp1->data<<"->";
       
       
    temp1=temp1->next;
    }
    cout<<"NULL"<<endl;
    }
int main()
{head=NULL;
char ch='y';int n;
    int x;
while(ch=='y'||ch=='Y')
    {
        cout<<"Enter 1 for insertion"<<endl;
        cout<<"Enter 2 for deletion"<<endl;
        cin>>n;
         switch (n)
        { case 1 : cout<<"Enter data:";
                   cin>>x;
                
           insert (x);
            break;
case 2 : del();
        break;
}
        cout<<"The linked list is:"<<endl;
        show();
        cout<<"Do you want to continue operation  y/n"<<endl;
    cin>>ch;
    }
}

Linked list insertion and deletion at end

#include <iostream>
using namespace std;
struct node
   {
     int data ;
     node* next;
   };
node* head;
void insert(int x)
    {   node * temp=new node();
        temp->data=x;
    node*temp2=head;
          if(head==NULL)
             {
                 head =temp;
                 temp->next=NULL;
             }
  else
     {   while(temp2->next!=NULL)
        temp2=temp2->next;
        temp2->next=temp;
temp->next=NULL;
     }
}
void del()
{ node*temp2=head;
     if(head==NULL)
        cout<<"Linked is          empty"<<endl; 
    else
   
    while(temp2->next->next!=NULL)
       temp2=temp2->next;
     temp2->next=NULL;
   
    }
void show()
{ node*temp1=head;
while(temp1!=NULL)
{cout<<temp1->data<<"->";
       
       
    temp1=temp1->next;
    }
    cout<<"NULL"<<endl;
    }
int main()
{head=NULL;
char ch='y';int n;
    int x;
while(ch=='y'||ch=='Y')
    {
        cout<<"Enter 1 for insertion"<<endl;
        cout<<"Enter 2 for deletion"<<endl;
        cin>>n;
         switch (n)
        { case 1 : cout<<"Enter data:";
                   cin>>x;
                
           insert (x);
            break;
case 2 : del();
        break;
}
        cout<<"The linked list is:"<<endl;
        show();
        cout<<"Do you want to continue operation  y/n"<<endl;
    cin>>ch;
    }
}

Wednesday 16 September 2015

Doubly linked list

#include <iostream>

using namespace std;
struct node
{
    int data;
    node* next;
    node* previous;
};

node* head;
void insert(int x)
{
    node* temp=new node();
    temp->data=x;
    if(head==NULL)
    {
        head=temp;
        head->next=NULL;
        head->previous=NULL;
    }
    else

    temp->next=head;
    head->previous=temp;
    head=temp;
}
void showstart()
{
   node* temp1=head;
   while(temp1!=NULL)
   {
       cout<<temp1->data<<"->";
       temp1=temp1->next;
   }
   cout<<"NULL"<<endl;
}
void showend()
{
    node* temp2=head;
    while(temp2->next!=NULL)
    {
        temp2=temp2->next;
    }

    while(temp2!=head)
    {
        cout<<temp2->data<<"->";
        temp2=temp2->previous;
    }
    cout<<head->data;

}
int main()
{
    head=NULL;
    int n,x;
    cout<<"How many elements do you want to enter?"<<endl;
    cin>>n;
    while(n--)
      {
          cout<<"Enter data :";
          cin>>x;
          insert(x);
      }
cout<<"Doubly linked list from the start is :"<<endl;
showstart();

cout<<"Doubly linked list from the end is:"<<endl;
showend();
}

Tuesday 15 September 2015

Queue implementation using linked list.

#include <iostream>
using namespace std;

struct queue
  {
    int data;
    queue* next;

  };
queue* front;
queue* rear;
void enqueue(int x)
  {

     queue* temp= new queue();
     temp->data=x;
     if(front==NULL)
       {
             front=temp;
             rear=temp;
             rear->next=NULL;

       }
     else
       {
              temp->next=front;
              front=temp;
       }
   }
void dequeue()
   {
       if(rear==NULL||front->next==NULL)
            {  cout<<"Queue is empty"<<endl;
               front=NULL;
               }
       else
            {
               queue* temp1;
               temp1=front;
               while(temp1->next!=rear)
                     {
                       temp1=temp1->next;
                     }
               rear=temp1;
               rear->next=NULL;
            }
    }
void display()
{cout<<"The queue is:";
   queue* temp2=front;
   while (temp2!=NULL)
   {
       cout<<temp2->data<<"->";
       temp2=temp2->next;
   }
   cout<<"NULL"<<endl;
}
int main()
{   int n,x;
    front=NULL;
    rear=NULL;
    char ch='y';
    while(ch=='y'||ch=='Y')
        {
              cout<<"1.To insert an element in the queue."<<endl;
              cout<<"2.To delete an element from the queue."<<endl;
              cin>>n;
              switch(n)
                  {
                       case 1 : cout<<"Enter Data:";
                                cin>>x;
                                enqueue(x);
                                break;
                       case  2: dequeue();
                                break;
                  }
              display();
              cout<<"Do you want to continue operation?y/n"<<endl;
              cin>>ch;
         }
    return 0;
}

Sunday 9 August 2015

Selection sort using c++

# include<iostream>
using namespace std;
int main()
{       int n,min,i,j,k,temp;
        cout<<"Enter the size of array";
        cin>>n;
        int a[n];
        cout<<"Enter the elements of array";
        for(i=0;i<n;i++)
       cin>>a[i];
        for(j=0;j<n-1;j++)
            {
                  min=j;
                  for(k=j+1;k<n;k++)
                      {
                           if(a[k]<a[min])
                           min=k;
                      }
      
                  temp=a[min];
                 a[min]=a[j];
                 a[j]=temp;
       
           }
    cout<<"The sorted array by selection sort is";
    for(i=0;i<n;i++)
    cout<<a[i]<<endl;
}

Saturday 8 August 2015

Linear search using c++

#include<iostream>
using namespace std;
int lsearch(int a[],int n,int x)
{       int i;
    for(i=0;i<n;i++)
    {   if(a[i]==x)
        return i;
    }
    return -1;
}
int main()
{
     int n,x,i;
     int a[n];
   
     cout<<"Enter the size of  array";
     cin>>n;
    cout<<"Enter the elements of    array";
    for(i=0;i<n;i++)
    cin>>a[i];
    cout<<"The array you entered  is";
    for(i=0;i<n;i++)
    cout<<a[i]<<endl;
    cout<<"Enter the Element you want to find";
    cin>>x;
     int t=lsearch(a,n,x); 
     if(t==-1)
    cout<<"Element does not exist in the array";
    else
       {cout<<"The element exists    at index";
        cout<<t;
        }
   
}

Binary search code in C++

#include<iostream>
using namespace std;
int bsearch(int a[],int n,int x)
{       int start =0;
        int end= n-1;int mid;
        while(start<=end)
              {  
                  mid =(start+end)/2;
                  if(a[mid]==x) 
                     {
                         cout<<"element found at  index";
                        return mid;
                     }
                else if(x<a[mid])
                end=mid-1;
                else
                start=mid+1;
            }
    return -1;
}
int main()
{
     int n,x,i;
     int a[n];
   
     cout<<"Enter the size of array";
     cin>>n;
    cout<<"Enter the elements of  array in"      
                <<" ascending order";
    for(i=0;i<n;i++)
    cin>>a[i];
    cout<<"The array you entered  is";
    for(i=0;i<n;i++)
    cout<<a[i]<<endl;
    cout<<"Enter the Element you want to find";
    cin>>x;
     int t=bsearch(a,n,x); 
     if(t==-1)
    cout<<"Element does not exist in the array";
    else
       {cout<<"The element exists    at index";
        cout<<t;
        }
   
}