Sunday, 14 February 2016

Tree Insertion

#include <iostream>
using namespace std;



struct node

{
    int data;
    node*left;
    node* right;
};

class tree
{
private: node *parent, *start, *cur, *temp;

public:
    tree()
    {
        start = NULL;
    }

    void create(int x)
    {
        if (start == NULL)
        {
            start = new node;
            start->left = NULL;
            start->data = x;
            start->right = NULL;
        }
        else
        {
            cur = start;
            while (cur != NULL)
            {
                if (cur->data == x)
                {
                    cout << "element already exist";

                }
                else if (cur->data < x)
                {
                    parent = cur;
                    cur = cur->right;
                }
                else
                {
                    parent = cur;
                    cur = cur->left;
                }
                temp = new node;
                temp->data = x;
                temp->left = NULL;
                temp->right = NULL;
           
            if (parent == NULL)
            {
                parent = temp;
            }
            else  if (parent->data > x)
                    parent->left = temp;
                else
                    parent->right = temp;
        }
    }
  }
};

int main()
{
    int val;
    tree obj;
    for (int i = 0; i < 6; i++)
    {
        cout << "Enter vale to Tree: ";
        cin >> val;
         obj.create(val);
    }
}

Wednesday, 10 February 2016

Insertion, Specific Insertion, and Specific Deletion in Single linked list.

#include<iostream>
using namespace std;
struct node

{
    int data;
    node*link;
};
class linked_list
{
    private:
        node*start,*cur,*temp;
        public:
            linked_list()
            {
                start=NULL;
            }
        void   Append(int n)
            {
            if(start==NULL)   
            {
                start=new node;
                start->data=n;
                start->link=NULL;
            }
            else
            {
                cur=start;
                while(cur->link!=NULL)
                {
                     
                      cur=cur->link;
                          }
                  temp=new node;
                  temp->data=n;
                  temp->link=NULL;
                  cur->link=temp;
               
            }
        }
        void sp_insert(int n,int loc)
            {
                cur=start;
                for(int j=1;j<loc-1;j++)
                {
                    cur=cur->link;
                }
                if(cur==NULL)
                cout<<"Invalid position";
                else
                {
                    temp=new node;
                    temp->data=n;
                        temp->link=cur->link;
                    cur->link=temp;
                }
            }
        void   sp_del(int SE)
            {
                    cur=temp=start;
                    while(cur->link!=NULL)
                    {
                        if(cur->data==SE)
                        {
                            cout<<"Element found and deleted"<<endl;
                            temp->link=cur->link;
                            delete cur;
                            break;
                        }
                        else
                        {
                            temp=cur;
                            cur=cur->link;
                       
                        }
                   
                        if(cur->link==NULL)   
                        cout<<"Element not found"<<endl;
                    }
            }
                void print()
        {
            if(start==NULL)
            cout<<"List is Empty";
            else
            {
                cur=start;
                while(cur->link!=NULL)
                {
                    cout<<cur->data;
                    cur=cur->link;
                   
                }
                cout<<cur->data;
            }
        }
       
};
 int main()
{
    int loc, SE, val, value;
   
    linked_list obj;
    cout<<"Please, Enter five values"<<endl;
    for(int i=0;i<5;i++)
        {
        cin>>value;
        obj.Append(value);
        }
    cout << endl;
    cout << "Your Entered numbers in linked list are: ";
    obj.print();
    cout<<endl;
    cout<<"Please, Enter value and location for specific insertion"<<endl;
    cin>>val>>loc;
    obj.sp_insert(val,loc);
    obj.print();
    cout<<endl;
   
    for(int i=1;i<=2;i++)
    {
    cout << "Now, Enter number for deletion from linked list" << endl;
    cin>>SE;
    obj.sp_del(SE);
    }
    cout<<"Now your Remaining list is"<<endl;
    obj.print();
}


Insertion in Double Linked List

#include<iostream>
using namespace std;


struct node
{
    int data;
    node*n, *p;
};
class double_llist
{
private:node*start, *cur, *temp;
    public:
        double_llist()
        {
            start=NULL;
        }
        void Append(int val)
        {
            if(start==NULL)
            {
                start=new node;
                start->p=NULL;
                start->n=NULL;
                start->data=val;
            }
            else
            {
                cur=start;
                while(cur->n!=NULL)
                {
                cur=cur->n;
                }
                temp=new node;
                temp->n=NULL;
                temp->p=cur;
                temp->data=val;
                cur->n=temp;
               
            }
        }
        void print()
        {
            if(start==NULL)
            cout<<"List is empty";
            else
            {
                cur=start;
                while(cur->n!=NULL)
                {
                    cout<<cur->data;
                    cur=cur->n;
                }
                cout<<cur->data;
            }
           
        }
    };
    int main()
    {
        double_llist obj;
        int value;
        cout<<"Please,Enter 10 values"<<endl;
        for(int i=0;i<=9;i++)       
        {
            cout<<"Enter number at loc"<<i<<endl;
            cin>>value;
            obj.Append(value);
        }
        cout<<endl;
        obj.print();
    }

Specific Insertion in Single Linked list

#include<iostream>
using namespace std;

struct node
{
    int data;
    node*link;
};
class linked_list
{
    private:
        node*start,*cur,*temp;
        public:
            linked_list()
            {
                start=NULL;
            }
        int  Append(int n)
            {
            if(start==NULL)   
            {
                start=new node;
                start->data=n;
                start->link=NULL;
            }
            else
            {
                cur=start;
                while(cur->link!=NULL)
                {
                     
                      cur=cur->link;
                  }
                  temp=new node;
                  temp->data=n;
                  temp->link=NULL;
                  cur->link=temp;
               
            }
        }
        void  sp_insert(int n,int loc)
            {
                cur=start;
                for(int j=1;j<loc-1;j++)
                {
                    cur=cur->link;
                }
                if(cur==NULL)
                cout<<"Invalid location";
                else
                {
                    temp=new node;
                    temp->data=n;
                    temp->link=cur->link;
                    cur->link=temp;
                }
            }
                void print()
        {
            if(start==NULL)
            cout<<"List is Empty";
            else
            {
                cur=start;
                while(cur->link!=NULL)
                {
                    cout<<cur->data;
                    cur=cur->link;
                   
                }
                cout<<cur->data;
            }
        }
       
};
 int main()
{
    int loc, val, value;
   
    linked_list obj;
    cout<<"Please, Enter five values";
    for(int i=0;i<5;i++)
        {
        cin>>value;
        obj.Append(value);
        }
    cout<<"Your Entered Numbers in list are: ";
    obj.print();
    cout << endl;
    cout<<"Please, Enter number and location for specific insertion";
    cin>>val>>loc;
    obj.sp_insert(val,pos);
    cout << endl;
    obj.print();
   
}

Thursday, 28 January 2016

Insertion in Single Linked List

#include <iostream>
using namespace std;

struct node

{
    int data;
    node*link;
};

class linked_list
{
private: node *start, *cur, *temp;

public:

    linked_list()
  {
    start = NULL;
  }
        void append(int n)
        {
            if (start == NULL)
            {
                start = new node;
                start->data = n;
                start->link = NULL;
            }
            else
            {
                cur = start;
                while (cur->link != NULL)
                {
                    cur = cur->link;
                }
                temp = new node;
                temp->data = n;
                temp->link = NULL;
                cur->link = temp;
            }
        }
void print()
        {
            if (start == NULL)
                cout << "List empty";
            else
                cur = start;
            while (cur->link != NULL)
            {
                cout << cur->data;
                cur=cur->link;
            }
            cout << cur->data;
        }
};

int main()
{
    int n;
   linked_list obj;
    for (int i = 0; i < 5; i++)
    {
        cout << "Please Enter value for insertion: ";
        cin >> n;
        obj.append(n);
    }
    cout <<endl<< "Your Inserted values are: ";
    obj.print();
}

Friday, 15 January 2016

Double Ended Que Implementation

#include <iostream>
using namespace std;


class deque
{
private: int q[5], F, R;
public:

    deque()

    {
        R = F = -1;
    }

    void qinsert(int val)

    {
        if (R > 4)
            cout << "que full";
        else
        {
            R++;
            q[R] = val;

        }
        if (F == -1)
            F = 0;

    }

    int qdel()
    {
        int temp;
        if (F == -1)
            cout << "que empty";
        else
        {
            temp = q[F];
            if (F == R)
                F = R = -1;
            F++;
            return temp;
        }

    }


    void deq_insert(int val)
    {
        if (F < 0)

            cout << "Que full";
        else
        {
            F--;
            q[F] = val;
        }
    }



    int deq_del()
    {
        int temp;

        temp = q[R];
        R--;
        return temp;

        if (R == -1)
            cout << "que empty";
    }


   
    void print()
    {

        for (int i = F; i <= R; i++)
            cout << q[i] << "-";

    }
};

int main()
{
    int n;
    deque obj;

    for (int i = 0; i <= 4; i++)
        {
        cout << "Enter value to Que: ";
        cin >> n;
        obj.qinsert(n);

        }
        cout << "The Que is: ";
        obj.print();
    {
        for (int k = 0; k < 2; k++)

        {
            cout << endl << "After Removing Number " << obj.qdel() << endl;
            cout << "The Que is: ";
            obj.print();
        }
            cout << endl;
    }
        for (int k = 0; k < 2; k++)
        {

            cout << "Enter value to Que again: ";
            cin >> n;
            obj.deq_insert(n);

        }

            cout << "Now The Que is: ";
            obj.print();

        for (int k = 0; k < 2; k++)
            {
            cout << endl << "After Removing Number " << obj.deq_del() << endl;
            cout << "The Que is: ";
            obj.print();
            }
            cout << endl;
}

Thursday, 14 January 2016

Circular Que Implementation

#include <iostream>
using namespace std;
class cirrcular_que

{
private:
    int cq[5], F, R;
public:

    cirrcular_que()
    {
        R = F = -1;
    }

    void cqinsert(int val)
    {
        if (((F == 0) && (R == 4)) || (R == F - 1))

            cout << "Que is full" << endl;

        else
        {
            if (R == 4)
                R = 0;
            else

                R++;
            cq[R] = val;

            if (F == -1)
                F = 0;

        }

    }


    int cqdel()
    {
        int temp;
        if (F == -1)

            cout << "Cque is empty";

        else
        {
            temp = cq[F];
            if (F == R)

                F = R = -1;

            else if (F == 4)

                F = 0;

            else
                F++;

        }

        return temp;

    }

    void print()
    {
        if (F == -1)
            cout << "Que is empty";
        else
        {
            if (F <= R)
            {
                for (int i = F; i <= R; i++)
                    cout << cq[i];
            }
            else
            {

                if (F>R)
                    for (int j = 0; j <= R; j++)

                        cout << cq[j] << endl;

                for (int k = F; k <= 4; k++)

                    cout << cq[k] << endl;

            }
        }

    }

};

int main()

{
    int n;
    cirrcular_que obj;

    for (int i = 0; i <= 4; i++)
    {
        cout << "Enter number to Que at '" << i << "'" << "location: ";
        cin >> n;
        obj.cqinsert(n);
    }

    cout << "Your Entered Que is: ";
    obj.print();
    cout << endl;
    for (int j = 0; j<2; j++)
    {
        cout << "The Number Deleted from '" << j << "'" << " location is : " << obj.cqdel() << endl;
    }

    cout << "After Removing Ist 2 values, the Remaining values in Que are: ";
    obj.print();
    cout << endl;
    cout << "Now we can add more numbers at the empty locations by following Cirrcular Que, So"<< endl;

    for (int k = 0; k<2; k++)
    {
        cout << "Enter number again for '" << k << "'" << " location: ";
        cin >> n;
        obj.cqinsert(n);
    }
    cout << endl;
    cout << "Now the Que is: ";
    obj.print();
    system("pause");
}

Tree Insertion

#include <iostream> using namespace std; struct node {     int data;     node*left;     node* right; }; class tree { priva...