Saturday, 16 March 2019

Program of Trees in C++ language

#include<iostream>
#include<conio.h>
using namespace std;

struct node{
int data;
node *left=NULL;
node *right=NULL;
};

class tree{
private:
node *root=NULL;
public:
void insert(int value);
std
};

void tree::insert(int value){
node *temp = new node;
temp->data = value;
if(root == NULL){
root = temp;
cout<<"\n**Root Inserted**\n";
}else{
node *current = root; // holds the address of nodes from root
node *ptr = NULL; //temporary pointer which hold the address of left/right node which is null
while(current != NULL){
ptr = current; //assigning the address of current node
if(value > current->data){
current=current->right;
}else if(value< current->data){
current = current->left;
}else{
cout<<"\n***Data already exist***\n";
return;
}
} // while ends here
if(value < ptr->data){
ptr->left = temp;
cout<<"Data inserted at Left after "<<ptr->data<<endl;
}else{
ptr->right = temp;
cout<<"Data inserted at Right after "<<ptr->data<<endl;
}
} // outer else ends here
}
int main(){
tree t1;
char c;
do{
int data;
cout<<"\n=======New Data==============\n\n";
cout<<"Enter data : ";
cin>>data;
t1.insert(data);
cout<<"\nDo you want to run this program again?(y/n) : ";
cin>>c;
}while(c=='y'||c=='Y');
getch();
}

output:



Share:

Friday, 7 July 2017

C++ program of bubble sort

//Program starts

#include<iostream>

using namespace std;

int main()
{
int a,b,arr[a];

cout<<"How many indexes you wants to create : ";
cin>>a;
cout<<"Enter data in dissorted form "<<endl;
int m;
for(int y=0;y<=a-1;y++)
{
cin>>arr[y];
}


for(int i=0;i<=a-2;i++)
{
for(int j=0;j<=a-2-i;j++)
{
if(arr[j]>arr[j+1])
{
m=arr[j];
arr[j]=arr[j+1];
arr[j+1]=m;
}

}
}
cout<<"\n Data in sorted form \n\n";
for(int k=0;k<=a-1;k++)
{
cout<<"Data at Index ["<<k <<"] = "<<arr[k]<<endl;
}


}
/Program ends

OUTPUT:
Share:

C++ program of selection sort

//Program starts

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int n=5,a[n],b,min=0;
for(int i=0;i<n;i++)
{
cout<<"Enter data at ["<<i<<"] : ";
cin>>a[i];
}
for(int j=0;j<n;j++)
{
min=j;
for(int i=j+1;i<=n-1;i++)
{

if(a[i]<a[min])
{
min=i;
}
}

b=a[min];
a[min]=a[j];
a[j]=b;
}
cout<<"\nData in sorted form \n \n";
for(int i=0;i<n;i++)
{
cout<<"Data at ["<<i<<"] = "<<a[i]<<endl;
}
}
//Program ends

OUTPUT:

Share:

C++ program of insertion sort

//Program starts

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int n=5,a[n],j,key;
for(int i=0;i<n;i++)
{
cout<<"Enter data at ["<<i<<"] : ";
cin>>a[i];
}

for(int i=1;i<n;i++)
{
key=a[i];
j=i-1;

while(a[j]>key && j>=0)
{
a[j+1]=a[j];
j--;
}

a[j+1]=key;
}
cout<<"\nData in sorted form \n \n";
for(int i=0;i<n;i++)
{
cout<<"Data at ["<<i<<"] : "<<a[i]<<endl;

}

}
//Program ends

OUTPUT:

Share:

Sunday, 2 July 2017

C++ program of binary search

#include<iostream>
#include<conio.h>
using namespace std;
int a[10];
void binary(int p, int r, int data)
{
        
         int q;
         q=(p+r)/2;
        
         if(a[q]==data)
         {
                 cout<<"found";
         }
         else if(data<a[q])
         {
                 binary(p,q-1,data);
         }
         else
         {
                 binary(q+1,r,data);
         }
        
}

int main()
{
         int d;

        
         for(int i=0;i<10;i++)
         {
                 cout<<"Enter data at ["<<i<<"] : ";
                 cin>>a[i];
         }
         cout<<"Enter data to find : ";
         cin>>d;
        
         binary(0,9,d);
}


OUTPUT:
Share:

C++ program of sequential search

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

using namespace std;

class sort{
         private:
                 int d,c;
                 int a[20];
         public:
                 void create();
                 void seq();
};

void sort::create()
{
         for(int i=0;i<5;i++)
         {
                 cout<<"Enter data at ["<<i<<"] : ";
                 cin>>a[i];
         }
}

void sort::seq()
{
         c=-1;
         cout<<"Enter data to be find : ";
         cin>>d;
         for(int i=0;i<20;i++)
         {
                 if (a[i]==d)
                 {
                          cout<<"Data found at index ["<<i<<"] ";
                          c=i;
                          break;
                 }
                
         }
         if(c==-1)
         {
                 cout<<"Data not found";
         }
}

int main()
{
         sort s;
         s.create();
         s.seq();
}


OUTPUT:
Share:

C++ program of linked list

In this program we will insert,delete and print data of linked list
//Program Starts

#include<iostream>
#include<conio.h>
using namespace std;
struct node{
          int data;
          node *next;
};
class linked{
          private:
                    node *ptr,*head,*tail,*cur,*prev;
                    int d,loc;
          public:
                    void create();
                    void insatbeg();
                    void insatend();
                    void insatspec();
                    void delatbeg();
                    void delatend();
                    void delatspecind();
                    void delofspecdat();
                    void show();
};

void linked::create()
{
          head=new node;
          head->data=0;
          head -> next=NULL;
          tail=head;
          for(int i=1;i<10;i++)
          {
                    ptr= new node;
                    ptr->data=i;
                    ptr->next=NULL;
                    tail->next=ptr;
                    tail=ptr;
          }
         
}

void linked::insatbeg()
{
          cout<<"Enter data at begining : ";
          cin>>d;
          ptr= new node;
          ptr->data=d;
          ptr->next=head;
          head=ptr;
}

void linked::insatend()
{
          cout<<"Enter data at end : ";
          cin>>d;
          ptr= new node;
          ptr->data=d;
          ptr->next=NULL;
          cur=head;
         
          while(cur->next!=NULL)
          {
                    cur=cur->next;
          }
          cur->next=ptr;
}

void linked::insatspec()
{
          cout<<"Enter Location : ";
          cin>>loc;
          cout<<"Enter data: ";
          cin>>d;
          ptr= new node;
          ptr->data=d;
          prev=head;
          cur=head->next;
         
          for(int i=1;i<=loc-2;i++)
          {
                    prev= cur;
                    cur=cur->next;
          }
          prev->next=ptr;
          ptr->next=cur;
}

void linked::delatbeg()
{
          cur = head;
          head=head->next;
          delete cur;
}

void linked::delatend()
{
          prev=head;
          cur=head->next;
          while(cur->next!=NULL)
          {
                    prev=cur;
                    cur=cur->next;
          }
          prev->next=NULL;
          delete cur;
}

void linked::delatspecind()
{
          cout<<"Enter location to be deleted : ";
          cin>>loc;
         
          prev=head;
          cur=head->next;
          for(int i=1;i<=loc-2;i++)
          {
                    prev=cur;
                    cur=cur->next;
          }
          prev->next=cur->next;
          delete cur;
}

void linked::delofspecdat()
{
          cout<<"Enter data to be deleted : ";
          cin>>d;
         
          prev=head;
          cur=head->next;
          while(cur->data!=d)
          {
                    prev=cur;
                    cur=cur->next;
          }
          prev->next=cur->next;
          delete cur;
}

void linked::show()
{
          cur=head;
          while(cur!=NULL)
          {
                    cout<<cur->data<<" -> ";
                    cur=cur->next;
          }
}


int main()
{
          linked l1;
          int c;
          l1.create();
          do{
                    cout<<"\n\nPress 1 to insert at beg\nPress 2 to insert at end\nPress 3 to insert at specific location\nPress 4 to show\nPress 5 to delete data from begining\nPress 6 to delete data from End\nPress7 to delete data from specific location\nPress 8 to delete specific data\nPress 9 to exit\n Value==== ";
                    cin>>c;
                   
                    switch(c)
                    {
                              case 1:
                                        l1.insatbeg();
                                        break;
                              case 2:
                                        l1.insatend();
                                        break;
                              case 3:
                                        l1.insatspec();
                                        break;
                              case 4:
                                        l1.show();
                                        break;
                              case 5:
                                        l1.delatbeg();
                                        break;
                              case 6:
                                        l1.delatend();
                                        break;
                              case 7:
                                        l1.delatspecind();
                                        break;
                              case 8:
                                        l1.delofspecdat();
                                        break;
                              case 9:
                                        return 1;
                    }
          }
          while(1);
         

}

OUTPUT:
Share:

C++ program of circular queue

In this program we will see how to insert,delete and show data in Circular Queue.

//Program starts

#include<conio.h>
#include<iostream>
using namespace std;

class cirq{
          private:
                    int d,size,cq[5],counter,front,rear;
          public:
                    cirq();
                    void create();
                    void incq(int d);
                    void delcq();
                    void show();
};

cirq::cirq()
{
          size=5;
          counter=0;
          front=-1;
          rear=-1;
}

void cirq::create()
{
          for(int i=0;i<5;i++)
          {
                    cout<<"Enter data at ["<<i<<"] : ";
                    cin>>cq[i];
                   
          }
          front=0;
          rear=4;
          counter=5;
         
}

void cirq::incq(int d)
{
          if(counter==5)
          {
                    cout<<"Queue is full\n";
          }
          else
          {
                    cout<<"Enter data : ";
                    cin>>d;
                    rear=(rear+1)%size;
                    cq[rear]=d;
                    counter++;
          }
}

void cirq::delcq()
{
          if(counter==-1)
          {
                    cout<<"Queue is empty\n";
          }
          else
          {
                    front=(front+1)%size;
                    counter--;
          }
}

void cirq::show()
{
          for(int i=0;i<counter;i++)
          {
                    cout<<"Data at ["<<(front+i)%5<<"] = "<<cq[(front+i)%5]<<endl;
          }
}


int main()
{
          cirq c; //Creating Object
          int b,d;
          c.create();    
do{
          cout<<"\n Press 1 to show data\n Press 2 to Delete data\n Press 3 to Insert data \n Press 4 to EXIT the program\nValue= ";
          cin>>b;
          switch(b)
          {
                    case 1:
                              c.show();
                              break;
                    case 2:
                              c.delcq();
                              break;
                    case 3:
                              c.incq(d);
                              break;
                    case 4:
                              cout<<"Bye! Bye!";
return 1;
          }
}
while(1);
         
         
}


OUTPUT:
Share:

C++ program of Stack

In this program we will discuss about working of stack and we will see how to insert and delete data from stack.

//Program starts
#include<iostream>
#include<conio.h>
using namespace std;

class stack{
          private:
                    int d,n,a[5],top;
          public:
                    stack();
                    void create();
                    void push();
                    void pop();
                    void show();
};
stack::stack()
{
          n=5;
          top=-1;
}

void stack::create()
{
          for(int i=0;i<n;i++)
          {
                    cout<<"Enter data at ["<<i<<"] : ";
                    cin>>a[i];
          }
         
          top=4;
}

void stack::push()
{
          if(top==4)
          {
                    cout<<"\n Stack is full \n";
          }
          else
          {
                    cout<<"Enter data : ";
                    cin>>d;
                    top++;
                    a[top]=d;
          }
}

void stack::pop()
{
          if(top==-1)
          {
                    cout<<"\n Stack underflow \n";
          }
          else
          {
                    top--;
          }
}

void stack::show()
{
          if(top==-1)
          {
                    cout<<"\nStack is empty\n ";
          }
          else
          {
         
          for(int i=0;i<=top;i++)
          {
                    cout<<"Data at ["<<i<<"] = "<<a[i]<<endl;
          }
}
}

int main()
{
          int c;
          stack s1;
          s1.create();
         
          do{
                    cout<<"\nPress 1 to insert data\nPress 2 to delete data\n Press 3 to show data\nPress 4 to exit\nValue = ";
                    cin>>c;
                   
                    switch(c)
                    {
                              case 1:
                                        s1.push();
                                        break;
                                       
                              case 2:
                                        s1.pop();
                                        break;
                                       
                              case 3:
                                        s1.show();
                                        break;
                                       
                              case 4:
                                        return 0;
                                        break;
                    }       
          }
          while(1);
}

 OUTPUT:




Share: