Friday 22 January 2016

DDA algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include<iostream.h>
int main(void)
{   int gdriver = DETECT, gmode;
   initgraph(&gdriver, &gmode, "C:\\TC\\bgi");

  float x1,y1,x2,y2,dx,dy,length,xinr,yinr,x,y;
cout<<"enter the x coordinate of the first point";
cin>>x1;
cout<<"enter the y coordinate of the first point ";
cin>>y1;
cout<<"enter the x coordinate of the second point ";
cin>>x2;
cout<<"enter the y coordinate of the second point ";
cin>>y2;
     dx=x2-x1;
     dy=y2-y1;
     if(abs(dy)>abs(dx))
     length=abs(dy);
     else
     length=abs(dx);
       xinr=dx/length;
       yinr=dy/length;
       x=x1;
       y=y1;
for(int i=0 ;i<length;i++)
{   putpixel ( x,y,2);
x+=xinr;
y+=yinr;

}




   getch();
   closegraph();
   return 0;
}

Bresenham algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include<iostream.h>
int main(void)
{   int gdriver = DETECT, gmode;
   initgraph(&gdriver, &gmode, "C:\\TC\\bgi");

  float x1,y1,x2,y2,dx,dy,length,xinr,yinr,x,y,e;
cout<<"enter the x coordinate of the first point";
cin>>x1;
cout<<"enter the y coordinate of the first point ";
cin>>y1;
cout<<"enter the x coordinate of the second point ";
cin>>x2;
cout<<"enter the y coordinate of the second point ";
cin>>y2;
     dx=x2-x1;
     dy=y2-y1;
       if(abs(dy)>abs(dx))
     length=abs(dy);
     else
     length=abs(dx);
e=2*dy-dx;
      x=x1;
      y=y1;
      for(int i=0;i<length;i++)
      { while(e>0)
      {
y+=1;
e-= 2*dx;

      }
x+=1;
e=e+2*dy;
    putpixel(x,y,3);
      }

   getch();
   closegraph();
   return 0;
}

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