Wednesday, March 26, 2014

ternory search tree in C++

                                                             TERNORY SEARCH TREE

having two data field and three pointers to the childs.


#include<iostream>
using namespace std;
struct tnode
{
int data1,data2;
tnode *left,*right,*mid;
};
void add(tnode *&t,int k1,int k2)
{
if(t==NULL)
{
t=new tnode;
t->data1=k1;
t->data2=k2;
t->right=NULL;
t->left=NULL;
t->mid=NULL;
}
else
{
if(k1<t->data1)
add(t->left,k1,k2);
if(k1>t->data2)
add(t->right,k1,k2);
else
add(t->mid,k1,k2);
}}
void search(tnode *t,int k)

{
if(t->data1==k||t->data2==k)
cout<<"found";
else
{
if(k<t->data1)
search(t->left,k);
else if(k>t->data2)
search(t->right,k);
else if(k>t->data1&&k<t->data2)
search(t->mid,k);
}
}
void output(tnode *t)
{
if(t!=NULL)
{
cout<<t->data1<<" "<<t->data2<<" ";
output(t->left);
output(t->mid);
output(t->right);
    }
}
int main()
{
tnode *t;
t=new tnode;
t=NULL;
cout<<"enter the key values";
int k1,k2;
cin>>k1>>k2;
do
{
add(t,k1,k2);
cin>>k1>>k2;
}while(k1!=-1&&k2!=-1);
output(t);
cout<<"\nenter the element to be searched";
int a;
cin>>a;
search(t,a);

return 0;
}

Sunday, March 23, 2014

HEAP sort in C++

#include<iostream>
using namespace std;
int n,g[20],ctr=0;;
void heapsort(int a[],int n);
void heapify(int a[],int n);
void delheap(int a[])
{
int i,t,p;
g[ctr++]=a[0];
a[0]=a[n];
n--;
p=(n%2==1)?(n-1)/2:(n-2)/2;
if(a[p]>a[0])
{
swap(a[p],a[0]);
delheap(a);
}
}
int main()
{
int a[20];
int i=0,j=0;
cout<<"enter the elements";
cin>>n;
while(n!=-1)
{
a[i++]=n;
cin>>n;
}
n=i;
heapsort(a,n);
cout<<"\nsorted numbers are ";
delheap(a);
for(i=0;i<n;i++)
cout<<g[i]<<" ";
return 0;
}
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void heapsort(int a[],int n)
{
int i,t;
for(i=n-1;i>0;i--)
{
int p;
p=(i%2==1)?(i-1)/2:(i-2)/2;
if(a[p]>a[i])
{
swap(a[p],a[i]);
heapsort(a,p);
}
}
}

Saturday, March 15, 2014

color change of screen in 8086

color change of screen if press 1
.model small
.stack 64
.data
clor db 10h
.code
main proc near

mov ax,@data
mov ds,ax

mov ax,3h           ; clear screen
int 10h

a10:
mov ax,0600h         ;set disply
mov bh,clor
mov cx,0000h           ; x1,y1
mov dx,184fh             ;x2,y2
int 10h
add clor,10h                  ;change background color

mov ah,8h                  ; ikeyboard nput
int 21h
cmp al,'1'                      ; if 1 press then color change otherwise exit
je a10

mov ax,4c00h
int 21h

main endp
end main

moving screen box in 8086

.model small
.stack 64
.data
point1a dw 510h
point1b dw 1520h      ;coordinate of box
tik dw ?
.386
.code
main proc near

mov ax,@data
mov ds,ax



a10:
mov ax,0600h          ;set disply
mov bh,52h              ; 5 background color ,2 forehead color
mov cx,point1a          ; x1, y1
mov dx,point1b           ;x2, y2
int 10h

call delay                      ;delay of 1 sec

inc point1a                 ; move point forward for   next disply
inc point1b
mov dl,cl                
mov ax,0600h            ; erase starting row of box
mov bh,00h
int 10h

cmp cl,63          
jb a10


mov ax,4c00h
int 21h
main endp

delay proc near

pusha


mov ah,00h         ;give system time
int 1ah

mov tik,dx           ;in dx  
add tik,12h         ; store in tik  (1 sec= 12h value)  ,system time+1sec
b10:
mov ah,00h
int 1ah

cmp tik,dx            ;execute loop until 1sec over
jge b10

popa
ret
 delay endp
end main

quicksort in c++

#include<iostream>
using namespace std;
void swap(int& a,int& b)
{
int c=a;
a=b;
b=c;
}
int partion(int a[10],int low,int hi)
{
int i,j,ce,lo,h1,b[3];
 ce=(low+hi)/2;

if((a[ce]>=a[low] && a[ce]<=a[hi])||(a[ce]<=a[low] && a[ce]>=a[hi]))
swap(a[low],a[ce]);
else if((a[hi]>=a[low] && a[hi]<=a[ce])||(a[hi]<=a[low] && a[hi]>=a[ce]))
swap(a[low],a[hi]);
lo=low+1;
h1=hi;
while(lo<=hi)
{
while(a[lo]<a[low])
lo++;
while(a[hi]>a[low] && hi>=lo)
hi--;
if(lo<hi)
{
swap(a[lo],a[hi]);
hi--;lo++;
}
}

swap(a[low],a[hi]);

return hi;
}
void quick(int a[10],int low,int hi)
{
int ce;
if(low<hi)
{
ce=partion(a,low,hi);
    quick(a,low,ce-1);
    quick(a,ce+1,hi);
}

}

int main()
{
int data[20],n,co=0;
cout<<"enter data\n";
cin>>n;
while(n!=-1)             //-1 for end inputs
{
data[co++]=n;
cin>>n;
}
quick(data,0,co-1);            
for(int i=0;i<co;i++)               //output in sorted order
cout<<data[i]<<" ";
return 0;
}

queue in c++




#include<iostream>
using namespace std;
class queue
{
 public :
 int front,rear,size,a[10];
 void enque(int c);
 int delque();
 queue()
 {
  front=-1;
  rear=-1;
  size=4;
 }
};

void queue::enque(int c)
{
 if((rear+1)%size==front)
 cout<<"full";
else if(front==-1)
{
 rear++;
 a[++front]=c;
}
else
{
 rear=(rear+1)%size;
 a[rear]=c;
}
}
int queue::delque()
{int d;
if(front<=-1)
{
cout<<"empty";
exit(1);
}
else if(front==rear)
{ d=a[front];
front=-1;rear=-1;
return d;
}
else
{d=a[front];
front=(front+1)%size;
return d;
}
}
int main()
{queue q;
int e;
cout<<"enter 4 no.s";
for(int i=0;i<4;i++)
{cin>>e;
q.enque(e);
}
cout<<"after insertion store data in queue retrive";
for(int i=0;i<4;i++)
{
e=q.delque();
cout<<e<<" ";
}
return 0;
}

moving ball in java



import java.applet.*;
import java.awt.*;
import java.util.Random;

public class Bouncing_ball extends Applet implements Runnable {


int x1=100,xrate=1;
int y1=200,yrate=3;
int radius1=20;
Random re;
Thread th;
public void start(){
setBackground(Color.red);
setSize(400,400);
re=new Random();
th=new Thread(this);
th.start();
}
int t=200;
public void run(){
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while( true){
if(x1<=0|| x1>=360){

xrate=-xrate;
if(yrate<0)
yrate=-re.nextInt(4);
else
yrate=re.nextInt(4);
}

if(y1<=0|| y1>=360){

yrate=-yrate;
if(xrate<0)
xrate=-re.nextInt(4);
else
xrate=re.nextInt(4);
}

x1+=xrate;
y1+=yrate;
repaint();
try{

Thread.sleep(20);
}
catch(InterruptedException ex){

}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}
}
public void paint(Graphics g){
g.setColor(Color.green);
g.fillOval(x1,y1,radius1*2,radius1*2);

}

}

Spiral motion code in java

import java.applet.*;
import java.awt.*;

public class Sprial extends Applet{
public void paint(Graphics g){
int x=0,y=10,xrate=240,yrate=210;
setBackground(Color.green);
g.setColor(Color.black);
for(int i=1;i<=15;i++){
int m=x;
int n=y;
if(i%4==1){
x+=xrate;
xrate-=30;
}
else if(i%4==2){
y+=yrate;
yrate-=30;
}
else if(i%4==3){
x-=xrate;
xrate-=30;
}
else
{
y-=yrate;
yrate-=30;
}
g.drawLine(m, n, x, y);
}
}
}

stack using link list

#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class stack
{
public:
node *p=NULL,*q,*t;
void push(int d);
int pop();
};
void stack::push(int d)
{
if(p==NULL)
{
p=new(node);
p->data=d; p->next=NULL;
}
else
{
t=new(node);
t->data=d;
t->next=p;
p=t;
}
}
int stack:: pop()
{
int a;
if(p!=NULL)
{
    a=p->data;
p=p->next;
return a;
}
}
int main()
{ stack s;
int i,j,n;
cout<<"enter 4 no";
for(i=0;i<4;i++)
{
cin>>n;
s.push(n);
}
cout<<"in reverse ";

for(i=0;i<4;i++)
{

cout<<s.pop();
}
return 0;
}

sort using stack

#include<iostream>
using namespace std;

class stack
{public:
int top,s,a[10];
void push(int d);
int pop();
stack(){
top=-1,s=4;
}
};

void stack::push(int d)
{if(top>=s-1)
cout<<"stack is full\n";
else
a[++top]=d;
}
int stack::pop()
{int c;
if(top<=-1)
{
cout<<"stack is empty\n";
return 0;
}
else
{ c =a[top--];

return c;
}}
int main()
{stack s,s1; int i,j,n,a,b;
cout<<"enter ";
for(i=0;i<4;i++)                   //enter 4 numbers
{cin>>n;
s.push(n);
}
for(i=3;i>0;i--)
{
for(j=i;j>0;j--)
{a=s.pop();               // pop 2 numbers in stack
b=s.pop();
if(a>=b)
{s1.push(a);             //store minimum into 2nd stack s1
s.push(b);
}
else
{s1.push(b);
s.push(a);
}
}
while(s1.top>-1)        
{n=s1.pop();            
s.push(n);
    }


}
while(s.top>-1)
cout<<s.pop();
return 0;
}

BUBBLE SORT USING QUEUE

                                             






#include<iostream>
#include<cstdlib>
using namespace std;
class queue
{
 public :
 int front,rear,size,a[10];
 void enque(int c);
 int delque();
 queue()
 {
  front=-1;
  rear=-1;
  size=4;
 }
};

void queue::enque(int c)
{
 if((rear+1)%size==front)
 cout<<"full";
else if(front==-1)
{
 rear++;
 a[++front]=c;
}
else
{
 rear=(rear+1)%size;
 a[rear]=c;
}
}
int queue::delque()
{int d;
if(front<=-1)
{
cout<<"empty";
exit(1);
}
else if(front==rear)
{ d=a[front];
front=-1;rear=-1;
return d;
}
else
{d=a[front];
front=(front+1)%size;
return d;
}
}
int main()
{queue q;
int e,f,i,j,k,l,m,n,p;
cout<<"enter 4 no.s";
for(i=0;i<4;i++)
{cin>>e;
q.enque(e);                              //INSERT INTO QUEUE
}
for(i=4;i>0;i--)
{
    m=0;k=0;
    for(j=0;j<i;j++)
    {
    n=q.delque();                
       if(m<n)                                   // IF MAXIMUM THEN BEFORE THEN STORE POSITION
     {                                                //AND CHANGE     M     VALUE
     k=j;
     m=n;
     }
     q.enque(n);
    }
     for(l=0;l<k;l++)
    {
       p=q.delque();              // GO TO THAT POSITION WHICH HAVE TO SHOW AND AGAIN
       q.enque(p);                //STORE IN QUEUE
    }
    cout<<q.delque()<<" ";
}
return 0;
}

AVL tree in C++

AVL tree is a balanced tree.

DEFINITION: An AVL tree is a binary search tree with the additional
balance property that, for any node in the tree, the height of the left and
right subtrees can differ by at most 1. As usual, the height of an empty
subtree is -1.


#include<iostream>
 using namespace std;
 struct avlnode                       //node structure of node
{ int data;
 avlnode *lchild;
 avlnode *rchild;
};
int height(avlnode *t)              //height of node
{
if(t==NULL)
return -1;
else
{
int a=height(t->lchild);
int b=height(t->rchild);
if(a>b)
return a+1;
else return b+1;

}
}
 avlnode *rotatelchild(avlnode *t)  //rotate left
{
char k1,k2;
k1=t->data;
k2=t->rchild->data;
t->data=k2;
t->lchild->data=k1;
t->rchild=t->rchild->rchild;
return t;
}
avlnode *rotaterchild(avlnode *t)  //rotate right
{
char k1,k2;
k1=t->data;
k2=t->lchild->data;
t->data=k2;
t->rchild->data=k1;
t->lchild=t->lchild->lchild;
return t;
}
  void doublerotatelchild(avlnode *&k3)     //double rotation  right to left
   {
    rotaterchild(k3->lchild);
    rotatelchild(k3);
  }
   void doublerotaterchild(avlnode *&k3)   //double rotation left to  right
    {
rotatelchild(k3->rchild);
     rotaterchild(k3);
    }
 void creatavltree(avlnode *&t,int k)
{
    if(t==NULL)
  {
  t=new(avlnode);
        t->data=k;
        t->lchild=NULL;
        t->rchild=NULL;
}
else if(k<t->data)
  {
  creatavltree(t->lchild,k);
  if((height(t->lchild)-height(t->rchild))==2||height(t->lchild)-height(t->rchild)==-2)
  {
  if(k<t->lchild->data)
    rotatelchild(t);
  else doublerotatelchild(t);
}
}
    else if(k>t->data)
    {
    creatavltree(t->rchild,k);
if((height(t->rchild)-height(t->lchild))==2||height(t->rchild)-height(t->lchild)==-2)
{
  if(t->rchild->data<k)
  rotaterchild(t);
  else doublerotaterchild(t);
}
    }
  }
   void printpreorder(avlnode *t)
{
if(t!=NULL)
{
  cout<<t->data<<" ";
  printpreorder(t->lchild);
  printpreorder(t->rchild);
  }
 }
  int main()
  {
    avlnode *t;
  t=NULL;
    int d,h;
  cout<<"enter the elements of avl tree and -1 to terminate"<<endl;
  cin>>d;
while(d!=-1)
  {
    creatavltree(t,d);
    cin>>d;
    }
  cout<<"\nthe height of avl tree is\n";
h=height(t);
  cout<<h<<endl;
  cout<<"\nthe elements of avl tree is\n";
printpreorder(t);
return 0;
 }

HEAPSORT in C++

Heap is an array which stores elements in unsorted order. When an element is poped out it gives the minimum or maximum element (eg. 5,8,2,9,1) it will give 1,2,5,8,9.


#include<iostream>
using namespace std;
int n,g[20],ctr=0;;
void heapsort(int a[],int n);
void heapify(int a[],int n);
void delheap(int a[])
{
int i,t,p;
g[ctr++]=a[0];
a[0]=a[n];
n--;
p=(n%2==1)?(n-1)/2:(n-2)/2;
if(a[p]>a[0])
{
swap(a[p],a[0]);
delheap(a);
}
}
int main()
{
int a[20];
int i=0,j=0;
cout<<"enter the elements";
cin>>n;
while(n!=-1)
{
a[i++]=n;
cin>>n;
}
n=i;
heapsort(a,n);
cout<<"\nsorted numbers are ";
delheap(a);
for(i=0;i<n;i++)
cout<<g[i]<<" ";
return 0;
}
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void heapsort(int a[],int n)
{
int i,t;
for(i=n-1;i>0;i--)
{
int p;
p=(i%2==1)?(i-1)/2:(i-2)/2;
if(a[p]>a[i])
{
swap(a[p],a[i]);
heapsort(a,p);
}
}
}

Friday, March 14, 2014

COMPARE 2 TREES in C++

Comparing two tree means check each and every node of tree on each level that it is equal or not.
for two similar trees parent as well as both child node should be equal.
the code for comparing two trees is given below.


first tree           and second tree
we copare node 'a' with 'x' and its child subtrees with 'others child subtree.







#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct btree                                     //defined the node structure
{
btree *lchild;
char data;
btree *rchild;
};
btree* bcreate(btree *t,char k);
void printsort(btree *t,char b[15]);
int l=0,m=0;
int main()
{
btree *p=NULL,*q=NULL;
char d,a[15],b[15],c[15];
int n,i;
cout<<"tree data\n";
cin>>d;
p=bcreate(p,d);
printsort(p,a);
a[l]='\0';
n=l;
l=0;
cout<<"tree data\n";
cin>>d;
q=bcreate(q,d);
printsort(q,b);
b[l]='\0';
for(i=0;i<l; i++)
{
c[i]=b[l-1-i];
}
c[l]='\0';
if(!strcmp(a,b))
cout<<"similar";
else if(!strcmp(a,c))
cout<<"mirror";
else
cout<<"nonsimilar";
return 0;
}
btree* bcreate(btree* t,char k)                     //to get input from user
{

                                             btree *s=NULL,*r=NULL;char c1,c2;
if(t==NULL && k!='.')                        //k='.' used  here for stop making nodes in tree
{
t=new(btree);
t->data=k;
t->rchild=NULL; t->lchild=NULL;
cout<<"enter left and right child of "<<k<<endl;
cin>>c1>>c2;
t->lchild=bcreate(s,c1);                     //recursive call for create child
t->rchild=bcreate(r,c2);
}
return t;
}
void printsort(btree *t,char b[15])                       // f\print the tree content in sorted order  nodes                                                                                              // (left child,parent ,right child) and store in character array
{                                                                          
if(t!=NULL)
if(t->lchild==NULL && t->rchild==NULL)
b[l++]=t->data;
else
{
printsort(t->lchild,b);
b[l++]=t->data;
printsort(t->rchild,b);
}
}

POTATO (circular queue) using LINKEDLIST using C++

#include<iostream>
using namespace std;
class circular
{
    public:
    struct node
    {
        int data;
        node *next;
    }*front,*rear;
    void add();
    void out();
    void delete();
    void initial();
};
void circular::initial()
{
    front=NULL;
    rear=NULL;
}
void circular:: add()
{
    node *k;
    k=new node;
    int d;
    cout<<"enter";
    cin>>d;
    k->data=d;
    if(front==NULL)
    rear=front=k;
   
    else
    {
        front->next=k;
        front=k;
        front->next=rear;
    }
}
void circular::delete()
{
    node *k;
    k=new node;
    rear=rear->next;rear=rear->next;
    k=rear;
    rear=rear->next;rear=rear->next;
    k->next=rear;
    rear=k;
}
void circular::out()
{
    cout<<rear->data;
}
int main()
{
    circular c;
    c.initial();
    int a,b=0;
    do
    {
        c.add();
        b++;
        cout<<"enter 1 to add more and 0 to terminate";
        cin>>a;
    }while(a==1);
    for(int i=0;i<b;i++)
    {
        c.delete();
    }
    c.out();
}

LARGE NUMBER ADDITION using LINKEDLIST in C++

Suppose we have to add two very large number like for eg. (43653465365365+74365365436765).
we store both the numbers in two linked lists we start adding the content of each unit from right hand side and store in third linked list.



#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
    int data;
    node *next;
};
int main()
{
    int a2[10],m=0,n=0,a1[10],x=0,y=0,r1[10],r2[10],i=0,j=0;
    char c1[10],c2[10],b;
    cout<<"enter 1st number";
    cin.getline(c1,10);
    while(c1[i]!='\0')
    {
        a1[i]=c1[i]-48;
        i++;m++;
    }
    cout<<"\n enter 2nd number";
    cin.getline(c2,10);
    while(c2[j]!='\0')
    {
        a2[j]=c2[j]-48;
        j++;n++;    }
        for(i=0;i<m;i++)
        r1[m-i-1]=a1[i];
        for(j=0;j<n;j++)
        r2[n-j-1]=a2[j];
       
        node *l1,*l2,*l3,*p,*q,*t3,*t1,*t2;
        l1=new node;
        l1->data=r1[0];
        l1->next=NULL;
        p=l1;
        do
        {
            t1=new node;
            t1->data=r1[++x];
            t1->next=NULL;
            l1->next=t1;
            l1=t1;
        }while(x<m-1);
            l1=p;
            l2=new node;
            l2->data=r2[0];
            l2->next=NULL;
            q=l2;
            do
        {
            t2=new node;
            t2->data=r2[++y];
            t2->next=NULL;
            l2->next=t2;
            l2=t2;
        }while(y<n-1);
            l2=q;
       
        node *z;
        int c,a=0;
        int aa;
        int carry=0;
        l3=new node;
         aa=(l1->data)+(l2->data);   
        if(aa<10)
        l3->data=aa;
        else{
        l3->data=(aa%10);
        carry=1;}
        l3->next=NULL;
        z=l3;
        l1=l1->next;
        l2=l2->next;
   
        while((l1!=NULL)&&(l2!=NULL))
        {
       
            aa= (l1->data)+(l2->data)+carry;
            t3=new node;
            if(aa<10)
            {
            t3->data=aa;
            carry=0;}
            else
            {
                t3->data=aa%10;
                carry=1;
            }
            t3->next=NULL;
            l3->next=t3;
            l1=l1->next;
            l2=l2->next;
            l3=t3;
        }
            while((l1!=NULL&&l2==NULL))
            {
            aa=((l1->data)+carry);
            carry=0;
            t3=new node;
            if(aa<10)
            t3->data=aa;
            t3->next=NULL;
            l3->next=t3;
            l3=t3;
            l1=l1->next;
       
    }
        while((l2!=NULL&&l1==NULL))
            {
            aa=((l2->data)+carry);
            carry=0;
            t3=new node;
            if(aa<10)
            t3->data=aa;
            t3->next=NULL;
            l3->next=t3;
            l3=t3;
            l2=l2->next;
    }
    l3=z;
        int k=0,d[11];
        while(l3!=NULL)
        {
            d[k++]=l3->data;
            l3=l3->next;
        }
        cout<<"\n the addition of two large number is\n";
        for(int s=0;s<k;s++)
        {
            cout<<d[k-s-1]<<" ";
        }
        return 0;
       
}

LINKED LIST in C++

Linkedlist have one data storing part and one pointer . basic operation like adding deleting can be performed easily. linked list has one head-starting and tail -last end.pointer points to the next unit.
last pointer is NULL usually.
code in C++ is given below.

basic structure is shown in diagram.




#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
int input(node *L)
{
int d;
cout<<"enter a value";
cin>>d;
node *K;
L->data=d;
while(d!=-1)
      {
      K=new(node);
L->next=K;
L=K;
cout<<"enter a value";
cin>>d;
L->data=d;
}
if(d==-1)
L->next=NULL;
}

void addafter(node *L,int k,int m)
{
node *T;
while(L->data!=k)
{
L=L->next;
}
L->next=T;
L=T;
L=new(node);
L->data=m;
L->next=T;
}
int out(node *L)
{while(L!=NULL){
if(L->data!=-1)
cout<<" "<<L->data;
L=L->next;}
}
int addend(node *L)
{node *K;
while(L->next!=NULL)
{L=L->next;
}
K=new(node);
int d;
L->next=K;
K=L;
cout<<"enetr the number to inserted at end";
cin>>d;
L->data=d;
L->next=NULL;
}
int main()
{ int d,b,n;
node *L,*K,*t;
L=new(node);
K=L;
input(L);
L=K;
addend(L);
K=L;
cout<<"after which element you want to insert";
cin>>b;
cout<<"input";
int x;
cin>>x;
node *s;
addafter(L,b,x);
out(L);
return 0;
}


Thursday, March 13, 2014

EULAR PATH in C++



                                                        


#include<iostream>
using namespace std;
struct vertex
{
 int num,visit,dist,vi;
};
int n,edge[7][7],ov[2]={0,0};
struct node
{
int key;
node* next;
};
 void gcreate(vertex v1[7],int mt[7][7],int n)
{
int i,j,k,a,b,w;
char ch='y',wg,dg;

for(i=0;i<n;i++)
for(j=0;j<n;j++)
mt[i][j]=0;

for(i=0;i<n;i++)
{
v1[i].dist=99;
v1[i].visit=0;
v1[i].vi=-1;
v1[i].num=i+1;
}
cout<<"weight graph or not & directed or not";
cin>>wg>>dg;
while(ch=='y')
{
cout<<"enter 1st,2nd vertex";
cin>>a>>b;
if(wg=='w')
{
cout<<"weight";
cin>>w;
mt[a-1][b-1]=w;
}
else mt[a-1][b-1]=1;
if(dg!='d')
mt[b-1][a-1]=mt[a-1][b-1];
cout<<"again ";
cin>>ch;
}
}
node* eular(node *q,int a)
{
int i,m=1,z=0,u=0; node *s,*t,*p=NULL;
while(m==1 && z<20)
{m=0;z++;
for(i=0;i<n;i++)
     if(edge[a-1][i]!=0)
      {m=1;
      edge[a-1][i]=0;
      edge[i][a-1]=0;
      a=i+1;
      if(p==NULL)
      {
      p=new(node);
      p->key=a;
      p->next=NULL;
      s=p;
      }
      else
      {
      t=new(node);
 t->key=a;
 t->next=NULL;
 p->next=t;
 p=t;
      }
      break;
      }
}
p->next=q->next;
q->next=s;
p=q;z=0;
while(p!=NULL && z<20)
{z++;
for(i=0;i<n;i++)
if(edge[p->key-1][i]!=0)
{
if(z!=1)
p=eular(p,p->key);
else
u=1;
break;
}
p=p->next;
}
if(u==1)
{
p=q;
while(p->next!=NULL)
p=p->next;
t=new(node);
t->next=NULL;
if(p->key==ov[0])
t->key=ov[1];
else
t->key=ov[0];
p->next=t;
}
return q;
}
int main()
{
vertex v[7]; node *p=NULL,*q=NULL;
int i,j,a,v1[7],z,u=0;
cout<<"enter no. of vertex";
cin>>n;
gcreate(v,edge,n);
for(i=0;i<n;i++)
{z=0;
for(j=0;j<n;j++)
{
cout<<edge[i][j]<<" ";
if(edge[i][j]!=0)
z++;
}
if(z%2==1)
ov[u++];
cout<<endl;
}
cout<<"enter vertex for starting for eular path\n";
cout<<"if odd degree vertex is there start from that vertex ";
cin>>a;
cout<<"eular path";
p=new(node);
p->key=a;
p->next=NULL;
p=eular(p,a);
while(p!=NULL)
{
cout<<p->key<<" ";
p=p->next;
}
return 0;
}

minimum spanning tree in C++

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int i,k,j,l,mtix[7][7],v[7],flag[7][7],visit[7],vi[7],dist[7],path[6],addr,a,b,w,n,mini=0,count;
char ch='y';
cout<<"enter no. of vertex: ";
cin>>n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
mtix[i][j]=0; flag[i][j]=0;
}

for(i=0;i<n;i++)
{
dist[i]=99;
visit[i]=0;
vi[i]=-1;
}
while(ch=='y')
{
cout<<"for edge 1st vertex,weight, 2nd vertex ";
cin>>a>>w>>b;
mtix[a-1][b-1]=w;
mtix[b-1][a-1]=w;
cout<<"enter choice::";
cin>>ch;
}

cout<<"vertex from minimum spanning tree start";
cin>>a;
count=0;
v[count++]=a-1;

for(i=1;i<=n;i++)
{
mini=0;
for(j=0;j<i;j++)
{
for(k=0;k<n;k++)
for(l=0;l<n;l++)
 if(mtix[k][l]<mini && flag[k][l]!=1)
 {
  mini=mtix[k][l];
  a=k; b=l;
 }
}
flag[a][b]=1;

}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<mtix[i][j]<<" ";
cout<<endl;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<flag[i][j]<<" ";
cout<<endl;
}

return 0;
}

Wednesday, March 12, 2014

BINARY SEARCH TREE in C++

Binary search tree has one data variable and two pointer pointing to child.

structure of binary search tree can be represented like this.

 
                                                 



#include<iostream>
using namespace std;
struct bst
{
bst *lc;
int data;
bst *rc;
};
int findmin(bst *t)
{
if(t->lc==NULL)
return t->data;
else
{
return findmin(t->lc);
}

}
void insert(bst *&t, int k)
{
if(t==NULL)
{
t=new bst;
t->data=k;
t->lc=NULL;
t->rc=NULL;
}
else if(k>t->data)
{
insert(t->rc,k);
}
else
{
insert(t->lc,k);
}
}
 void del(bst *&t,int k)
{
int c;

if(k>t->data)
{
del(t->rc,k);
}
else if(k<t->data)
{
del(t->lc,k);
}
else
{
if(t->lc==NULL&&t->rc==NULL)
t=NULL;
else if(t->lc==NULL&&t->rc!=NULL)
{
t=t->rc;
}
else if(t->lc!=NULL&t->rc==NULL)
{
int c=findmin(t->lc);
t->data=c;
del(t->lc,c);
}
else if(t->lc!=NULL&t->rc!=NULL)
{
int c=findmin(t);
t->data=c;
del(t->lc,c);
}
}
}
void max(bstnode *t)
{
while(t->rchild!=NULL)
t=t->rchild;cout<<"\nmaximum is\n"<<t->data;

}
void min(bstnode *t)
{
while(t->lchild!=NULL)
t=t->lchild;cout<<"\nminimum is\n"<<t->data;
}
void printleaf(bstnode *t)
{
if(t->lchild==NULL&&t->rchild==NULL)
cout<<t->data;
if(t->lchild!=NULL)
printleaf(t->lchild);
if(t->rchild!=NULL)
printleaf(t->rchild);
}
void show(bst *t)
{
if(t!=NULL)
{
show(t->lc);
cout<<t->data<<" ";
show(t->rc);
}
}
int main()
{
int k;
bst *t;
t=NULL;
cout<<"enter ";
cin>>k;
while(k!=-1)
{
insert(t,k);
cin>>k;
}
show(t);
cout<<"\nenter the node you want to delete";
cin>>k;
del(t,k);
show(t);

}

basic expression solving in C++

#include<iostream>
using namespace std;
int main()
{
int b;
b=5+3*4%2;
cout<<b;
return 0;

}

variable size STACK in C++

#include<iostream>
#include<cstdlib>
using namespace std;
class stack
{
public:
int top,size,a[10];
void push(int b);
int pop();
int isfull();
int isempty();
stack (int c)
{
size=c;
top=-1;
}
stack()
{
size=0;
}
};

void stack::push(int b)
{

if(isfull())
cout<<"stack is full";
else
a[++top]=b;
}int stack::pop()
{
if(isempty())
cout<<"stack is empty";
else
{top--;
return(a[top+1]);
}
}
int stack :: isfull()
{
if(top>=size)
return 1;
else return 0;
}
int stack::isempty()
{if(top<=-1)
return 1;
else return 0;

}
int main()
{
int n,c,b[10],i=0;
cout<<"stack size";
cin>>c;
char ch;
stack s(c);
cout<<"enter the exp";
do
{
cout<<"enter";
cin>>n;
s.push(n);
cout<<"do you want to enter more";
cin>>ch;
}while(ch=='y'||ch=='Y');
i=0;
cout<<"in reverse order";
while(!s.isempty())
{
cout<<s.pop();
}
}
 

Contributors

Translate