Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Monday, 24 October 2011

Program on Hashing Chain



#include<iostream>
#include<cstdlib>
using namespace std;
class hashch
{
      struct node
      {
             int value;
             node *link;
      };
     public:
            struct node *hashtable[10];
            void insert(int);
            void del(int);
            bool search(int);
            void display();
            void inp();

};
void hashch::inp()
{
 for(int i=0;i<10;i++)
     {
                      hashtable[i]=NULL;
     }
}
void hashch::insert(int val)
{
     int c,i;

     node *t;
     t=new node;
     t->value=val;
     t->link=NULL;
     c=val%10;
     if(hashtable[c]==NULL)
           hashtable[c]=t;
     else
     {
         node*p;
         p=new node;
         p=hashtable[c];
         if(val<p->value)
         {
                       t->link=p;
                       hashtable[c]=t;
         }
         else
         {
             while (val>p->link->value&&p->link!=NULL)
             p=p->link;
             if(p->link->link!=NULL)
            {
                t->link=p->link;
                p->link=t;
             }
             else if(val<p->link->value)
             {
                  t->link=p->link;
                  p->link=t;
             }
             else
             p->link->link=t;
         }
     }
}
void hashch::del(int val)
{
     int c;
     node *t,*p ;
     c=val%10;
     t=new node;
     p=new node;

     if(hashtable[c]!=NULL)
     {
                          if(t->value==val)
                          {
                            t=hashtable[c];
                            hashtable[c]=hashtable[c]->link;
                           delete t;
                          }
                          else
                          {
                                  p=hashtable[c];
                                  while(p->link->link!=NULL&&p->link->value!=val)
                                  {
                                       p=p->link;

                                  }
                                  if(t->link->link!=NULL)
                                  {
                                                          t=p->link;
                                                          p->link=p->link->link;
                                                          free(t);
                                  }
                                   else
                                   t->link=NULL;
                          }
}
                          else
                          cout<<"\nThe bucket doesnt exist\n";
}

bool hashch::search(int val)
{
     int c;
     node *t;
     c=val%10;
     t=hashtable[c];
     if(hashtable[c]!=NULL)
     {
                           while(t!=NULL)
                           {
                                         if(t->value==val)
                                         {
                                                          cout<<"Found\n";
                                                          return true;
                                         }
                                         else
                                         {
                                             t=t->link;
                                         }
                           }


                               cout<<"The value doesnt exist\n";
                                return false;

     }
     else
     cout<<"The bucket is empty\n";
}
void hashch::display()
{
    node *p[10];
   node *t;

     int i;
    for(i=0;i<10;i++)
{
     p[i]=hashtable[i];
}
    for(i=0;i<10;i++)
    {
             t=new node;
                     if(p[i]!=NULL)
                     {
                                   t=p[i];
                                    while(t!=NULL)
                                   {

                                   cout<<t->value<<"\t";
                                    t=t->link;
                                    }
                     cout<<"\n";
                     }
                     else
                     cout<<"NULL\n";
      }
}
int main()
{
    hashch ch;
    int option;
    int ele;

    bool ans;
    ch.inp();
    while(1)
    {
            cout<<"\nEnter your choice\n";
            cout<<"\n1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n";
            cin>>option;
            switch(option)
            {
                          case 1:cout<<"\nEnter what you want to insert\n";
                          cin>>ele;
                          ch.insert(ele);
                               break;
                          case 2:cout<<"\nEnter what you want to delete\n";
                               cin>>ele;
                               ch.del(ele);
                                break;
                          case 3:cout<<"\nEnter what you want to search\n";
                                 cin>>ele;
                                 ans=ch.search(ele);
                                 if(ans)
                                  cout<<"\nFound\n";
                                 else
                                  cout<<"\nMissing\n";
                                 break;
                          case 4: ch.display();
                                  break ;
                          case 5: exit(1);
            }
    }
    return 0;

}




OUTPUT:-

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
1

Enter what you want to insert
2

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
3

Enter what you want to search
2
Found

Found

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
1

Enter what you want to insert
4

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
4
NULL
NULL
2
NULL
4
NULL
NULL
NULL
NULL
NULL

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
1

Enter what you want to insert
6

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
4
NULL
NULL
2
NULL
4
NULL
6
NULL
NULL
NULL

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit

Program on add and multiplication of Polynomials


#include<iostream>
using namespace std;
#define n 100
class poly
{
 private:
                int a[n], b[n], add[n], mul[n], p, q, at;
        public:
                void init ( );
void input ( );
                void process ( );
                void display ( );
};
void poly :: init ( )
{
        int i;
        for( i = 0; i < n; i++)
                a[ i ] = b [ i ] = add[ i ] = mul[ i ] = 0;
}
void poly :: input ( )
{
        int i;
    cout<<"\nEnter Degree Of First Polynomial::";
        cin>>p;
        cout<<"\nEnter Degree Of Second Polynomial::";
        cin>>q;
        cout<<"\nEnter Values in First Polynomial\n";
        for( i = 0; i <= p; i++)
        {
                cout<<"\nEnter X^"<<i<<" The Coefficient";
                cin>>a[ i ];
        }
        cout<<"\nEnter Values in Second Polynomial\n";
        for( i = 0; i <= q; i++)
        {
                cout<<"\nEnter X^"<<i<<" The Coefficient";
                cin>>b[ i ];
        }
}
void poly :: process ( )
{
        int i, j;
        if( p > q )
                at = p;
        else
                at = q;
        for ( i = 0; i <= at;i++   )
                add[ i ] = a[ i ] + b[ i ];
                for( i = 0;  i <= p;  i++)
                        for( j = 0; j <= q; j++)
                                  mul[i+j]+= a[i]*b[j];
}
void poly :: display ( )
{
 int i;
cout<<"Addition Of Two Polynomial Expressions Are\n\n";
for( i = at; i >=0 ; i--)
cout<<add[i]<<"X^"<<i<<"+";
cout<<"\n\nMultiplication Of Two Polynomial Expressions Are\n\n";
for( i = p + q; i >= 0; i--)
cout<<mul[i]<<"X^"<< i <<"+";
}
int main()
{
poly ob;
ob.init ( );
ob.input ( );
ob.process ( );
ob.display ( );
return 0;
}


OUTPUT:-

Enter Degree Of First Polynomial::2

Enter Degree Of Second Polynomial::2

Enter Values in First Polynomial

Enter X^0 The Coefficient1

Enter X^1 The Coefficient2

Enter X^2 The Coefficient5

Enter Values in Second Polynomial

Enter X^0 The Coefficient6

Enter X^1 The Coefficient4

Enter X^2 The Coefficient7
Addition Of Two Polynomial Expressions Are

12X^2+6X^1+7X^0+

Multiplication Of Two Polynomial Expressions Are

35X^4+34X^3+45X^2+16X^1+6X^0+

Program on Graph Traversal


#include<iostream>
using namespace std;
class graphtraversal
{
        public:
                int size;
                int **adj;
                void readadj();
                void dfs();
                void bfs();
};
void graphtraversal::readadj()
{
        cout<<"enter no of vertices\n";
        cin>>size;
        adj=new int *[size];
        for(int i=0;i<size;i++)
                adj[i]=new int [size];
        cout<<"enter adj matrix:\n";
        for(int i=0;i<size;i++)
                for(int j=0;j<size;j++)
                        cin>>adj[i][j];
}
void graphtraversal::dfs()
{
        int stack[size],top=-1;
        int start,visited[size];
        for(int i=0;i<size;i++)
                visited[i]=0;
        cout<<"enter starting node:\n";
        cin>>start;
        stack[++top]=start;
        visited[start]=1;
        while(top!=-1)
        {
                start=stack[top--];
                cout<<start<<"  ";
                for(int i=0;i<size;i++)
                {
                        if(adj[start][i]==1 && visited[i]!=1)
                        {
                                stack[++top]=i;
                                visited[i]=1;
                        }
                }
        }
}
void graphtraversal::bfs()
{
        int queue[size],f=-1,r=0;
        int strt,visited[size];
        for(int i=0;i<size;i++)
                visited[i]=0;
        cout<<"enter starting node\n";
        cin>>strt;
        queue[r++]=strt;
        visited[strt]=1;
        while(f!=r-1)
        {
                strt=queue[++f];
                cout<<strt<<" ";
                for(int i=0;i<size;i++)
                {
                        if(adj[strt][i]==1 && visited[i]!=1)
                        {
                                queue[r++]=i;
                                visited[i]=1;
                        }
                }
        }
}
int main()
{
        graphtraversal ob;
        int op,n=1;
        while(n==1)
        {
                cout<<"\nenter the option\n";
                cout<<"1.read adj\n2.DFS\n3.BFS\n4.exit\n";
                cin>>op;
                switch(op)
                {
                        case 1:
                                ob.readadj();
                                break;
                        case 2:
                                ob.dfs();
                                break;
                        case 3:
                                ob.bfs();
                                break;
                        case 4:
                                n=0;
                                break;
                }
        }
}


OUTPUT:-

enter the option
1.read adj
2.DFS
3.BFS
4.exit
1
enter no of vertices
4
enter adj matrix:
0
^Z
[4]+  Stopped                 ./a.out
[b520@localhost ~]$ ./a.out

enter the option
1.read adj
2.DFS
3.BFS
4.exit
1
enter no of vertices
4
enter adj matrix:
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0

enter the option
1.read adj
2.DFS
3.BFS
4.exit
2
enter starting node:
0
0  2  3  1
enter the option
1.read adj
2.DFS
3.BFS
4.exit
3
enter starting node
0
0 2 3 1
enter the option
1.read adj
2.DFS
3.BFS
4.exit
4



Program on Hashing


#include<iostream>
#include<cstdlib>
using namespace std;
class ihash
{
      int *p;
      int d;
      public:
             ihash()
             {
                    d=10;
                    }
                    void insert(int);
                    bool search(int);
                    void del(int);
                    void display();
                    void inp();
};
void ihash::inp()
{
     p=new int[10];
     for(int i=0;i<10;i++)
       p[i]=-1;
}

void ihash::insert(int key)
{

     int i;
     i=key%d;
     if(p[i]==-1)
       p[i]=key;
       else
       {
           int j=i;
            i=(i+1)%d;
            while(i!=j)
            {
                       if(p[i]==-1)
                       {
                           p[i]=key;
                           break;
                           }
                        else
                        i=(i+1)%d;
            }
}
}
bool ihash::search(int key)
{
     int i,j;
     i=key%d;
     if(p[i]==key)
       return true;
     else
     {
         j=i;
         i=(i+1)%d;
         while(i!=j)
         {
                    if(p[i]==key)
                    return key;
                    else
                    i=(i+1)%d;
         }
         return false;
     }
}
void ihash::del(int key)
{
     int i,j;
     i=key%d;
     if(p[i]==key)
       p[i]=-1;
     else
     {
         j=i;
         i=(i+1)%d;
         while(i!=j)
         {
                    if(p[i]==key)
                      p[i]=-1;
                    else
                     i=(i+1)%d;
         }
     }

}
void ihash::display()
{
     for(int i=0;i<10;i++)
     {
             if(p[i]==-1)
             cout<<"NULL\n";
             else
             cout<<p[i]<<"\n";
             }
             }
int main()
{
    int option;
    bool ans;
    int key;
    ihash ih;
      ih.inp();

    while(1)
    {


            cout<<"\nEnter your choice\n";
            cout<<"\n1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n";
            cin>>option;
            switch(option)
            {
                          case 1:cout<<"\nEnter the value your want to insert\n";
                                 cin>>key;
                                 ih.insert(key);
                                 break;
                          case 2:cout<<"\nEnter the value you want to delete\n";
                                 cin>>key;
                                 ih.del(key);
                                 break;
                          case 3:cout<<"\nEnter the value you want to search\n";
                                 cin>>key;
                                 ans=ih.search(key);
                                 if(ans)
                                  cout<<"\nFound\n";
                                  else
                                  cout<<"\nMissing\n";
                                 break;
                          case 4:ih.display();
                                  break;
                          case 5:exit(1);
            }
    }
return 0;
}


OUTPUT:-

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
1

Enter the value your want to insert
2

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
1

Enter the value your want to insert
4

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
1

Enter the value your want to insert
6

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
4
NULL
NULL
2
NULL
4
NULL
6
NULL
NULL
NULL

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
3

Enter the value you want to search
4

Found

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
2

Enter the value you want to delete
6

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
4
NULL
NULL
2
NULL
4
NULL
NULL
NULL
NULL
NULL

Enter your choice

1.Insert
2.Delete
3.Search
4.Display
5.Exit
5

Sunday, 23 October 2011

Program for Quick Sort


#include<iostream>
#include<cstdlib>
using namespace std;
class sort
{
        int a[20],b[20];
        int low,high;
        public:
                int n;
                int get();
                void getdata();
                void quicksort(int,int);
                void display();

};
void sort::getdata()
{
        cout<<"enter the number of elements in the array";
        cin>>n;
        cout<<"enter the elements";
        for(int i=0;i<n;i++)
                cin>>b[i];
}
int sort::get()
{
   return n;
}
void sort::quicksort(int low,int high)
{
    int p,i,j,t;
    p=i=low,j=high;
    if(i<j)
    {
       while(i<j)
       {
          while(b[p]<b[j])
            j--;
            if(b[p]>b[j])
              {
                 t=b[p];
                 b[p]=b[j];
                 b[j]=t;
                 p=j;
              }
                while(b[p]>b[i])
                    i++;
                   if(b[p]<b[i])
                   {
                        t=b[i];
                        b[i]=b[p];
                        b[p]=t;
                        p=i;
                   }
                        if(i>=j)
                         {
                             quicksort(low,i-1);
                             quicksort(i+1,high);
                         }
        }
     }
}
void sort::display()
{
        cout<<"the sorted list is\n";
        for(int i=0;i<n;i++)
                                               {
                cout<<b[i]<<"\t";
        }
}
int main()
{
        sort a;
        int op;
        while(1)
        {
                cout<<"enter the option\n";
                cout<<"1.get data\n2.quick sort\n3.display\n4.exit\n";
                cin>>op;
                switch(op)
                {
                        case 1:
                                a.getdata();
                                break;
                        case 2:
                                a.get();
                                a.quicksort(0,a.n-1);
                                break;
                        case 3:
                                a.display();
                                break;
                        case 4:
                                exit(1);
                }
        }
}


OUTPUT:-

enter the option
1.get data
2.quick sort
3.display
4.exit
1
enter the number of elements in the array5
enter the elements2
1
3
6
7
enter the option
1.get data
2.quick sort
3.display
4.exit
3
the sorted list is
2       1       3       6       7       enter the option
1.get data
2.quick sort
3.display
4.exit
2
enter the option
1.get data
2.quick sort
3.display
4.exit
3
the sorted list is
1       2       3       6       7       enter the option
1.get data
2.quick sort
3.display
4.exit
4


Program for Linear search and Binary search


#include<iostream>
#include<cstdlib>
using namespace std;
class search
{
        int arr[10];
        public:
        bool linearsearch(int);
        bool binarysearch(int);
        void sort();
        void readarray();
};
bool search::linearsearch(int ele)
{
        for(int i=0;i<10;i++)
                if(arr[i]==ele)
                        return true;
        return false;
}
bool search::binarysearch(int ele)
{
        int low=0,high=9,mid;
        search ob;
        ob.sort();
        while(low<=high)
        {
                mid=(low+high)/2;
                if(ele==arr[mid])
                        return true;
                else if(ele<arr[mid])
                        high=mid-1;
                else
                        low=mid+1;
        }
        return false;
}
void search::sort()
{
        for(int i=0;i<9;i++)
                for(int j=0;j<9;j++)
                        if(arr[j]>arr[j+1])
                        {
                                int temp=arr[j];
                                arr[j]=arr[j+1];
                                arr[j+1]=temp;
                        }
}
void search::readarray()
{
        cout<<"Enter the elements of the array:";
        for(int i=0;i<10;i++)
                cin>>arr[i];
}
int main()
{
        search ob;
        int ele,choice;
        bool b;
        ob.readarray();
        cout<<"Enter the element to be found:";
        cin>>ele;
        cout<<"Which search would you like to use?\n\n1.Linear Search\n2.Binary search\n";
        cin>>choice;
        switch(choice)
        {
                case 1:
                        {
                        b=ob.linearsearch(ele);
                        if(b==true)
                                cout<<"Element found.";
                        else
                                cout<<"Element not found.";
                        }
                break;
                case 2:
                        {
                        b=ob.binarysearch(ele);
                        if(b==true)
                                cout<<"Element found.";
                        else
                                cout<<"Element not found.";
                        }
                break;
        }
}


OUTPUT:-


Enter the elements of the array:2
1
4
6
2
3
1
4
5
6
Enter the element to be found:5
Which search would you like to use?

1.Linear Search
2.Binary search
1
Element found.

Saturday, 22 October 2011

Program on Merge Sort


#include<iostream>
#include<cstdlib>
using namespace std;
class array
{
      int a[10],n,b[10];
      public:
             void create(int n);
             void display(int n);
             void sort(int low,int high);
             void mergesort(int low,int mid,int high);

};
void array::create(int n)
{

     cout<<"the elements are:\n";
     for(int i=0;i<n;i++)
     cin>>a[i];
}
void array::display(int n)
{
     for(int i=0;i<n;i++)
     cout<<a[i]<<endl;
}
void array::sort(int low,int high)
{
     if(low<high)
     {
        int mid=(low+high)/2;
        sort(low,mid);
        sort(mid+1,high);
        mergesort(low,mid,high);
     }
}
void array::mergesort(int low,int mid,int high)
{
     int h=low,k=0;
     int i=low;
     int j=mid+1;
     while(h<=mid&&j<=high)
     {
         if(a[h]<a[j])
                b[k++]=a[h++];
         else
                b[k++]=a[j++];
     }
     while(h<=mid)
     {
         b[k++]=a[h++];

     }
       while(j<=high)
            b[k++]=a[j++];
            j=0;
        for(int k=low;k<=high;k++)
        a[k]=b[j++];
}
int main()
{
    array a;
    int n;
    cout<<"the no.of elements in array are:\n";
    cin>>n;
    a.create(n);
    cout<<"the given array is:\n";
    a.display(n);
    int low=0;
    int high=n-1;
    a.sort(low,high);
    cout<<"the sorted array is:\n";
    a.display(n);
    return 0;
}


OUTPUT:-

the no.of elements in array are:
5
the elements are:
1
6
8
2
3
the given array is:
1
6
8
2
3
the sorted array is:
1
2
3
6
8

Program on Selection Sort


#include<iostream>
#include<cstdlib>
using namespace std;
class sort
{
        int a[20];
        int n,t;
        public:
                void getdata();
                void selectionsort();
                void display();
};
void sort::getdata()
{
        cout<<"enter the number of elements in the array\n";
        cin>>n;
        cout<<"enter the elements\n";
        for(t=0;t<n;t++)
                cin>>a[t];
}
void sort::selectionsort()
{
        int small;
        for(int i=0;i<n-1;i++)
        {
                small=i;
                for(int j=i+1;j<n;j++)
                {
                        if(a[small]>a[j])
                        small=j;
                }
                int temp=a[i];
                a[i]=a[small];
                a[small]=temp;
        }
}
void sort::display()
{
        cout<<"the sorted list is\n";
        for(int i=0;i<n;i++)
        {
                cout<<a[i]<<"\t";
        }
}
int main()
{
        sort ob;
        int op;
        while(1)
        {
                cout<<"\nenter the option\n";
                cout<<"1.get data\n2.selection sort\n3.display\n4.exit\n";
                cin>>op;
                switch(op)
                {
                        case 1:
                                ob.getdata();
                                break;
                        case 2:
                                ob.selectionsort();
                                break;
                        case 3:
                                ob.display();
                                break;
                        case 4:
                                exit(1);
                }
        }
}



OUTPUT:-

enter the option
1.get data
2.selection sort
3.display
4.exit
1
enter the number of elements in the array
5
enter the elements
1
6
5
2
3

enter the option
1.get data
2.selection sort
3.display
4.exit
2

enter the option
1.get data
2.selection sort
3.display
4.exit
3
the sorted list is
1       2       3       5       6
enter the option
1.get data
2.selection sort
3.display
4.exit
4

Program on Bubble Sort


#include<iostream>
#include<cstdlib>
using namespace std;
class sort
{
        int a[20];
        int n,t;
        public:
                void getdata();
                void bubblesort();
                void display();
};
void sort::getdata()
{
        cout<<"enter the number of elements in the array\n";
        cin>>n;
        cout<<"enter the elements\n";
        for(t=0;t<n;t++)
                cin>>a[t];
}
void sort::bubblesort()
{
        for(int i=0;i<n-1;i++)
        {
                for(int j=0;j<n-1-i;j++)
                {
                        if(a[j]>a[j+1])
                        {
                                int temp=a[j];
                                a[j]=a[j+1];
                                a[j+1]=temp;
                        }
                }
        }
}
void sort::display()
{
        cout<<"the sorted list is\n";
        for(int i=0;i<n;i++)
        {
                cout<<a[i]<<"\t";
        }
}
int main()
{
        sort ob;
        int op;
        while(1)
        {
                cout<<"\nenter the option\n";
                cout<<"1.get data\n2.bubble sort\n3.display\n4.exit\n";
                cin>>op;
                switch(op)
                {
                        case 1:
                                ob.getdata();
                                break;
                        case 2:
                                ob.bubblesort();
                                break;
                        case 3:
                                ob.display();
                                break;
                        case 4:
                                exit(1);
                }
        }
}


OUTPUT:-


enter the option
1.get data
2.bubble sort
3.display
4.exit
1
enter the number of elements in the array
3
enter the elements
2
1
3

enter the option
1.get data
2.bubble sort
3.display
4.exit
2

enter the option
1.get data
2.bubble sort
3.display
4.exit
3
the sorted list is
1       2       3
enter the option
1.get data
2.bubble sort
3.display
4.exit
4

Thursday, 13 October 2011

Program on insertion and deletion using pointers


#include<iostream>
using namespace std;
template<class T>
class linear_list
{
      T *p;
      int size;
      public:
             void set();
             T get();
             int getindex(T);
             void del(T);
             T findmax();
             T findmin();
             void empty();
             void display();
};
template<class T>
void linear_list<T>::set()
{
     cout<<"\nEnter size of array\n";
     cin>>size;
     p=new T[size];
     cout<<"\nEnter elements\n";
     for(int i=0; i<size; i++)
     cin>>*(p+i);
}
template<class T>
T linear_list<T>::get()
{
     if(size<1)
     {
               cout<<"\nList is empty";
               return -1;
     }
     else
     return *p;
}
template<class T>
int linear_list<T>::getindex(T ele)
{
     if(size<1)
     {
               cout<<"\nList is empty";
               return -1;
     }
     else
     {
         for(int i=0; i<size; i++)
         {
                 if(*(p+i)==ele)
                 return (i+1);
         }
     }
     return -1;
}
template<class T>
void linear_list<T>::del(T ele)
{
     int pos;
     pos=getindex(ele);
     for(int i=pos-1; i<size; i++)
     *(p+i)=*(p+i+1);
     size=size-1;
}
template<class T>
T linear_list<T>::findmax()
{
     int max=*p;
     for(int i=1; i<size; i++)
     {
             if(max<*(p+i))
             {
                            max=*(p+i);
             }
     }
     return max;
}
template<class T>
T linear_list<T>::findmin()
{
     int min=*p;
     for(int i=1; i<size; i++)
     {
             if(min>*(p+i))
             {
                            min=*(p+i);
             }
     }
     return min;
}
template<class T>
void linear_list<T>::empty()
{
     for(int i=0; i<size; i++)
     {
             *(p+i)=0;
     }
}
template<class T>
void linear_list<T>::display()
{
     cout<<"\nArray is\n";
     for(int i=0; i<size; i++)
     cout<<*(p+i)<<" ";
     cout<<"\n";
}
int main()
{
    int ele, pos, option, n=1;
    linear_list<int> ob;
    while(n==1)
    {
              cout<<"\nEnter option";
              cout<<"\n1 set element\n2 get element\n3 getindex of element\n4 delete an element \n5 find maximum element\n6 find minimum element\n7 empty the array\n0 exit program\n";
              cin>>option;
              switch(option)
              {
                            case 1:
                                 ob.set();
                                 ob.display();
                                 break;
                            case 2:
                                 ob.get();
                                 ob.display();
                                 break;
                            case 3:
                                 cout<<"\nEnter element to get index of\n";
                                 cin>>ele;
                                 pos=ob.getindex(ele);
                                 if(pos!=0)
                                 {
                                           cout<<"\nElement found at "<<pos;
                                 }
                                 else
                                 cout<<"\nElement not found";
                                 break;
                            case 4:
                                 cout<<"\nEnter element to be deleted";
                                 cin>>ele;
                                 ob.del(ele);
                                 ob.display();
                                 break;
                            case 5:
                                 cout<<"\nMaximum element is "<<ob.findmax()<<"\n";
                                 break;
                            case 6:
                                 cout<<"\nMinimum element is "<<ob.findmin()<<"\n";
                                 break;
                            case 7:
                                 ob.empty();
                                 ob.display();
                                 break;
                            case 0:
                                 n=0;
                                 break;
              }
    }
    system("pause");
}

Program on insertion and deletion using arrays



#include<iostream>
using namespace std;
template<class T>
class linear_list
{
      T list[20];
      int size;
      public:
             void set();
             T get();
             int getindex(T);
             void del(T);
             T findmax();
             T findmin();
             void empty();
             void display();
};
template<class T>
void linear_list<T>::set()
{
     cout<<"\nEnter size of array\n";
     cin>>size;
     cout<<"\nEnter elements\n";
     for(int i=0; i<size; i++)
     cin>>list[i];
}
template<class T>
T linear_list<T>::get()
{
     if(size<1)
     {
               cout<<"\nList is empty";
               return -1;
     }
     else
     return list[0];
}
template<class T>
int linear_list<T>::getindex(T ele)
{
     if(size<1)
     {
               cout<<"\nList is empty";
               return -1;
     }
     else
     {
         for(int i=0; i<size; i++)
         {
                 if(list[i]==ele)
                 return (i+1);
         }
     }
     return -1;
}
template<class T>
void linear_list<T>::del(T ele)
{
     int pos;
     pos=getindex(ele);
     for(int i=pos-1; i<size; i++)
     list[i]=list[i+1];
     size=size-1;
}
template<class T>
T linear_list<T>::findmax()
{
     int max=list[0];
     for(int i=1; i<size; i++)
     {
             if(max<list[i])
             {
                            max=list[i];
             }
     }
     return max;
}
template<class T>
T linear_list<T>::findmin()
{
     int min=list[0];
     for(int i=1; i<size; i++)
     {
             if(min>list[i])
             {
                            min=list[i];
             }
     }
     return min;
}
template<class T>
void linear_list<T>::empty()
{
     for(int i=0; i<size; i++)
     {
             list[i]=0;
     }
}
template<class T>
void linear_list<T>::display()
{
     cout<<"\nArray is\n";
     for(int i=0; i<size; i++)
     cout<<list[i]<<" ";
     cout<<"\n";
}
int main()
{
    int ele, pos, option, n=1;
    linear_list<int> ob;
    while(n==1)
    {
              cout<<"\nEnter option";
              cout<<"\n1 set element\n2 get element\n3 getindex of element\n4 delete an element \n5 find maximum element\n6 find minimum element\n7 empty the array\n0 exit program\n";
              cin>>option;
              switch(option)
              {
                            case 1:
                                 ob.set();
                                 ob.display();
                                 break;
                            case 2:
                                 ob.get();
                                 ob.display();
                                 break;
                            case 3:
                                 cout<<"\nEnter element to get index of\n";
                                 cin>>ele;
                                 pos=ob.getindex(ele);
                                 if(pos!=0)
                                 {
                                           cout<<"\nElement found at "<<pos;
                                 }
                                 else
                                 cout<<"\nElement not found";
                                 break;
                            case 4:
                                 cout<<"\nEnter element to be deleted";
                                 cin>>ele;
                                 ob.del(ele);
                                 ob.display();
                                 break;
                            case 5:
                                 cout<<"\nMaximum element is "<<ob.findmax()<<"\n";
                                 break;
                            case 6:
                                 cout<<"\nMinimum element is "<<ob.findmin()<<"\n";
                                 break;
                            case 7:
                                 ob.empty();
                                 ob.display();
                                 break;
                            case 0:
                                 n=0;
                                 break;
              }
    }
    system("pause");
}

Algorithm of Stacks Using Linked


"Stacks using linked list"

1)start
2)define template<class T>
3)Define a class'stacks' with private data:
structure node containing 'ele'of type T and 'link'of type node* and
and public data:node* top,constructor for initialising top=NULL,functions push,pop() and display()
4)In function push define pointer t of node* type, do t->ele=ele.Check if top=NULL,if yes do top=t,t->link=NULL,else do t->link=top,top=t
5)In function pop check if top=NULL,if Yes print"underflow",return 0,else do top=top->link,return -1.
6)In function display do t=top,while(t!=NULL) print t->ele,do t=t->ele.
7)stop

Algorithm of Stacks Using Array


"Stacks using array"

1)start
2)Define constant max=10;
3)define template<class T>
4)Define a class'stacks' with private data:
array stack[max] of template type integer top
and public data:constructor for initialising top with function push(0,pop() and display()
5)In function push check if top=max-1,If yes return 0,else do top++,stack[top]=ele,return 1.
6)In function pop check if top=-1,if so return 0.
eles do top--,return 1.
7)In function display print stack[i] where i runs from top to 0.
8)stop

Algorithm of CLL


"various operations in Cirular linked list"

1)start
2)Declare a class cll with the following data
3)Inside private:declare a  structure node,containing a pointer link of type node* and a ele of type integer.
4)Inside public:declare a constructor to initialise Head to NULL and declare functions insertbeg(),insertend(),insertmid(),delbeg,delend().delmid(),findmax(),findmin() and display.
5)Outside the class define insertbeg().Element to be inserted is stored in t.check whether head is equal to NULL,If yes do head=t;
t->link=head;
else store last node address in p and do
t->link=head;
head=t;
p->link=head;
6)Inside function insertend() read the node to be inserted int and last node address in p.If head=NULL do head=t,t->link=head.
else do p->link=t;
t->link=head.
7)In function delmid() store new node address in t and node after which new node is to be inserted in p and do
t=p->link;
p->link=t->link;
8)In function delbeg(),check if head=NULL,if Yes Print"list is empty" else check if p->link=head if yes delete p,head=NULL,
else store last node address in p.do p->link=head->link;
p=head;
head=head->link;
delete p;
9)In function delend() check if head=NULL,If Yes print"list is empty" else check if p->link=head, if yes delete p,
Head=NULL;
else t=p->link,p->link=head,delete t.
10)In function delmid(),stotre address of node before node to be deleted in p,do
t=p->link,
p->link=t->link,
delet t.
11)In function findmax(),return the maximum element in list.
12)In function findmin(),return the minimum element in list.
13)In function display,displays the list.
14)Stop



Algorithm of DLL


"various operations in Double linked list"
1)start
2)Declare3 a clasws DLL with following data.
3)Inside private: a structure node containing:
a pointer left of type node*
a ele of type integer a pointer right of type node *
Inside Public: a pointer hweadt pf type node* constructs to initialise head to NULL methods like insertbeg(),insertend(),insertmid(),delbeg().delend(),delmid(),search(),display().
4)Outside the class define insertbeg as follows read element to be inserted int and check if head=NULL,if yes do
t->left=t->right=NULL;
head=t;
else do
t->right=head;
head=t;
t->left=NULL;
5)Inside function insertend(),read element5 to be inserted iont,check if head=NULL,if Yes
t->left=t->right=NULL;
head=t;
else point p to last element,do p->right=t
t->left=p;
t->right=NULL;
6)Inside function insertmid,read elements to be inserted int,p pointing to ele where new ele has to be inserted do
t->left=p;
t->right=p->right;
p->right->left=t;
p->right=t;
7) insidefunction delbeg() do
t=head;
head=head->right;
head->left=NULL;
delete t
8)Inside Function delend() move t to the last node,then
t->left->right=NULL;
delete t;
9)In function search,traverse the linked list and displays the element found.
10)In function display,displays the whole list.
11)stop

Algorithm of SLL


"various operations in Single linked list"

1)start
2)define a class sll with a structure node having one integer ele and one self referencing structure variable link,under private section.
3)under public,define a variale head of type node *, a construction for initialising head to NULL and methods like insertbeg(),insertend(),insertmid(),delbeg(),delend(),delmid(),findmax(),findmin().display().
4)outside the class define the function insertbeg() enter element to be inserted and check whether head=NULL.if yes do
t->link=NULL;
head=t;
or else do
t->link=head;
head=t;
5)define the function insert end as follows
if head=NULL do
t->link=NULL;
headt=t;
else take a pointer of type node*,p and make it point to the last ele.Now do
p->link=t;
t->link=NULL;
6)define function insertmid() as follows read the element after which new element has to that element.Now do
t->link=p->link;
p->link=t;
7)define function delbeg() as follows
make pointer t to point to the last but one elements then do
p=t->link;
t->link=NULL;
delete p;
9)Define function delmid as follows.
if t is pointing to ele to be deleted,do
p=t->link;
t->link=p->link;
delete p;
10)In function findmax(),find maximum element and display
11)In function findmin(),finf minimum element and display
12)In function display,displays all elements
13)stop

Saturday, 24 September 2011

Single Linked List Program


#include<iostream>
using namespace std;
class structures
{
        struct std
        {
        int rno;
        char name[20];
        struct std*link;
        }*head;
        int size;
        public:
        void getdata();
        void display();
        void insertbegin();
        void insertmiddle();
        void insertend();
        void delbegin();
        void dellast();
        void delmiddle();
        void search();
        structures()
        {
        head=NULL;
        }
};
void structures::getdata()
{
        struct std*t;
   cout<<"enter size \n";
   cin>>size;
   for(int i=0;i<size;i++)
   {
        t=new std;
        cout<<"\nenter roll no ";
        cin>>t->rno;
        cout<<"\nName is ";
        cin>>t->name;
        if(head==NULL)
        {
                 t->link=NULL;
                 head=t;
        }
        else
        {
                 t->link=head;
                 head=t;
        }
    }
}
void structures::insertbegin()
{
 struct std*t;
 t=new std;
 cout<<"\n Enter rollno ";
 cin>>t->rno;
 cout<<"Name is ";
 cin>>t->name;
 if(head==NULL)
 {
  head=t;
 t->link=NULL;
 }
 else
 {
  t->link=head;
  head=t;
 }
}
void structures::insertmiddle()
{
        int no;
struct std*t;
t=new std;
cout<<"\n enter roll no";
cin>>t->rno;
cout<<"\nName ";
cin>>t->name;
cout<<"enter roll no after which the node to be inserted\n";
cin>>no;
std*p;
p=new std;
p=head;
while(p->rno!=no&p!=NULL)
p=p->link;
if(p!=NULL)
 {
 t->link=p->link;
 p->link=t;
 }
}
void structures::insertend()
{
 struct std*t;
 t=new std;
 cout<<"\n Enter Roll no ";
 cin>>t->rno;
 cout<<"\nName is ";
 cin>>t->name;
 if(head==NULL)
 {
  head=t;
  t->link=NULL;
 }
 else
 {
  std*p;
 p=new std;
 p=head;
 while(p->link!=NULL)
 p=p->link;
 p->link=t;
 t->link=NULL;
 }
}
void structures::delbegin()
{
 struct std*t;
 t=new std;
t=head;
head=head->link;
delete(t);
}
void structures::dellast()
{
 struct std*p,*t;
 p=new std;
 t=new std;
 p=head;
 while(p->link->link!=NULL)
 p=p->link;
 t=p->link;
 p->link=NULL;
 delete(t);
}
void structures::delmiddle()
{
 int no;
 cout<<"enter rno of node to be deleted";
 cin>>no;
 std*p,*t;
 p=head;
 while(p->link->link!=NULL&p->link->rno!=no)
 p=p->link;
 t=p->link;
 p->link=t->link;
 delete(t);
}
void structures::search()
{
 int r;
 std*p;
 p=new std;
 p=head;
 cout<<"enter rno of node to search ";
 cin>>r;
 while(p->rno!=r&p->link!=NULL)
 p=p->link;
if(p==NULL)
 {
 cout<<"\nElement not found \n";
 }
 else
 {
 cout<<"node is "<<p->name<<"\t"<<p->rno;
 }
}
void structures::display()
{
 std*p;
 p=new std;
 p=head;
 cout<<"\nStudent details are\n";
 while(p!=NULL)
 {
  cout<<"\nRoll No is "<<p->rno;
  cout<<"\n Name is "<<p->name;
  p=p->link;
 }
}
int main()
{
 structures ob;
 ob.getdata();
 int option,n=1;
while(n==1)
{
 cout<<"\n Enter option";

cout<<"\n1.insert begin\n2.insert middle\n3.insert end\n4.delete begin\n5.delete last\n6.delete  middle\n7.search\n8.display\n0.exit program\n";
cin>>option;
switch(option)
{
 case 1:
 ob.insertbegin();
 ob.display();
 break;
 case 2:
 ob.insertmiddle();
 ob.display();
 break;
 case 3:
 ob.insertend();
 ob.display();
 case 4:
 ob.delbegin();
 ob.display();
 case 5:
 ob.dellast();
 ob.display();
 case 6:
 ob.delmiddle();
 ob.display();
 case 7:
 ob.search();
 case 8:
 ob.display();
 case 0:
 n=0;
 break;
}
}
}