Showing posts with label Template class. Show all posts
Showing posts with label Template class. Show all posts

C++ program code to implement the circular linked list, utilizing the concept of template class

Leave a Comment
/* The following C++ Program Code will be used to implement the circular linked list. The user is asked first to create the circular linked list, & when he is done entering the element in the list , he will enter 0. After creation to the initial circular list, he is given the option of insertion, deletion & display of the circular linked list thus created. Also provided with the program exit option. */

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

template<class T>
class node
{
     public:
              T data;
              node<T> *next;
};

template<class T>
class list
{
      private:
              node<T> *first;
      public:
      list();
      ~list();
      void create();
      void del();
      void ins();
      void disp();
};

template<class T>
list<T>::list()
{
               first=NULL;
}

template<class T>
list<T>::~list()
{
                node<T> *next;
                while(first)
                {
                            next=first->next;
                            delete first;
                            first=next;
                }
}

template<class T>
void list<T>::create()
{
                     T a;
                     node<T> *cur,*ptr;
                     first=NULL;
                     cout<<"\nEnter data : ";
                     cin>>a;
                     while(a)
                     {
                             cur=new node<T>;
                             cur->data=a;
                             cur->next=NULL;
                             if(first==NULL)
                             first=cur;
                             else
                             ptr->next=cur;
                             ptr=cur;
                             cout<<"\nEnter data : ";
                             cin>>a;
                     }
              ptr->next=first;  
}

template<class T>
void list<T>::ins()
{
                 node<T> *cur,*ptr,*exmp;
                 T ele;
                 char ch;
                 ptr=exmp=first;
                 cur=new node<T>;
                 cout<<"\nEnter data : ";
                 cin>>cur->data;
                 cur->next=NULL;
                 cout<<"\nDo you want to insert at first [y/n] ? : ";
                 cin>>ch;
                 if(ch=='Y'||ch=='y')
                 {
                                     while(ptr->next!=first)
                                     {
                                                          ptr=ptr->next;
                                     }
                                     cur->next=first;
                                     first=cur;
                                     ptr->next=first;
                 }
                 else 
                 {
                     cout<<"\nAfter which element you want to insert ? : ";
                     cin>>ele;
                     do
                     {
                              if(exmp->data==ele)
                              {
                                                 cur->next=exmp->next;
                                                 exmp->next=cur;
                              }
                              exmp=exmp->next;
                     }while(exmp!=first);
                 }
}

template<class T>
void list<T>::del()
{
                     T ele;
                     char ch;
                     node<T> *ptr,*ptr1;
                     ptr=ptr1=first;
                     cout<<"\nDo you want to delete the first element [y/n] ? : ";
                     cin>>ch;
                     if(ch=='y'||ch=='Y')
                     {
                            while(ptr->next!=first)
                            {
                                                   ptr=ptr->next;                 
                            }
                            first=ptr1->next;
                            ptr->next=first;
                      }
                     else
                     {
                         cout<<"\nWhich element to delete? : ";
                         cin>>ele;
                         do
                         {
                                  if(ptr1->next->data==ele)
                                  {
                                                           ptr1->next=ptr1->next->next;
                                                           break;
                                  }
                                  ptr1=ptr1->next;
                         }while(ptr1!=first);
                      }
}

template<class T>
void list<T>::disp()
{
     node<T> *ptr;
     ptr=first;
     do
     {
             cout<<ptr->data<<"---->";
             ptr=ptr->next; 
     }while(ptr!=first);
}

int main()
{
    int n;
    list <int> x;
    cout<<"\nEnter the elements to create initial list (Enter 0 when done)\n";
    x.create();
    do
    {
                   cout<<"\n 1.Insert Element \n2.Delete Element \n3.Display Element \n4.Exit \n";
                   cout<<"\nEnter option : ";
                   cin>>n;
                   switch(n)
                   {
                            case 1: x.ins();
                            break;
                            case 2: x.del();
                            break;
                            case 3: x.disp();
                            break;
                            case 4: exit(0);
                            break;
                            default: cout<<"\nWrong Choice Entered!";
                   }
    }while(n<=4);
    system("pause");
    return 0;
}


Screenshot:
C++ program code to implement the circular linked list, utilizing the concept of template class

Read More...

C++ Program Code is for implementing the insertion sort algorithm on an array, utilizing template class concept

Leave a Comment
/* The following C++ Program Code is for implementing the insertion sort algorithm on an array. To attain the compatibility for various data types, template class concept is used. The user is asked about the number of element he want to put in the array. Then he also enters the elements & the insertion sort algorithm is applied, & thus result is displayed */

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

template<class T>
void isort(T a[],int n)
{
     int i;
     for(i=1;i<n;i++)
     {
             T t=a[i];
             int j;
             for(j=i-1;j>=0&&t<a[j];j--)
             {
                                           a[j+1]=a[j];
             }
             a[j+1]=t;
     }
}


void main()
{
    int a[100],i,n;
    cout<<"Enter number of elements : ";
    cin>>n;
    cout<<"Enter elements (Use Spacebar as Separator)\n";
    for(i=0;i<n;i++)
    {
                    cin>>a[i];
    }
    isort(a,n);
    cout<<"After sorting the elements are\n";
    for(i=0;i<n;i++)
    {
                    cout<<a[i]<<"\n";
    }
    system("pause");
}


Screenshot:
C++ Program Code is for implementing the insertion sort algorithm on an array, utilizing template class concept

Read More...

C++ Program Code To Implement Singly Linked List Using Template Class

4 comments
/* The Following C++ code is tested in MS Visual Studio 2010. The Given code simulates the concept of Singly linked list utilizing the concept of Template Class, First it asks the user to create a list, then after the user enters a '0', it displays other options of the list to process it further */

#include<iostream>

using namespace std;

#include<conio.h>

template<class T>

class node

{

     public:

              T data;

              node<T> *link;

};

template<class T>

class list

{

      private:

              node<T> *first;

      public:

      list();

      ~list();

      void createlist();

      void deletion();

      void insertion();

      void display();
     
      };

template<class T>

list<T>::list()

{

               first=NULL;

}

template<class T>

list<T>::~list()

{

                node<T> *next;

                while(first)

                {

                            next=first->link;

                            delete first;

                            first=next;

                }

}

template<class T>

void list<T>::createlist()

{

                     T a;
                    
                     node<T> *cur,*ptr;

                     cout<<"\nEnter data Of New Node (Enter 0 To Have other options):";

                     cin>>a;

                     while(a)

                     {

                             cur=new node<T>;

                             cur->data=a;

                             cur->link=NULL;

                             if(first==NULL)

                             first=cur;

                             else

                             ptr->link=cur;

                             ptr=cur;

                             cout<<"\nEnter data Of New Node (Enter 0 To Have other options):";

                             cin>>a;

                     }

}

template<class T>

void list<T>::insertion()

{
                node<T> *cur,*ptr;

                 T ele;
                  
                 char ch;
         
                 ptr=first;

                 cur=new node<T>;

                 cout<<"\nEnter data Of New Node:";

                 cin>>cur->data;

                 cur->link=NULL;

                 cout<<"\n Do u wish to insert at the start [y/n]:";

                 cin>>ch;

                 if(ch=='Y'||ch=='y')

                 {

                                     cur->link=first;

                                     first=cur;

                 }

                 else

                 {

                     cout<<"\n Specify after which element do u want to insert :";

                     cin>>ele;

                     while(ptr!=NULL)

                     {

                                     if(ptr->data==ele)

                                     {

                                                      cur->link=ptr->link;

                                                      ptr->link=cur;

                                                      break;

                                     }

                                     else

                                     {

                                         ptr=ptr->link;

                                     }

                     }

                 }

}

template<class T>

void list<T>::deletion()

{

                 T ele;

                 char ch;

                 node<T> *ptr,*ptr1;

                 if(first==NULL)

                 {

                                cout<<"\nSorry list is empty.";

                 }

                 else
                
                 {

                     ptr=first;

                     cout<<"\nDo u want to delete first element? [y/n]:";

                     cin>>ch;

                     if(ch=='y'||ch=='Y')

                     {

                                         first=first->link;

                                         delete ptr;

                     }

                     else

                     {

                         cout<<"\nSpecify which element do u want to delete :";

                         cin>>ele;

                         while(ptr!=NULL)

                         {

                                         if(ptr->link->data==ele)

                                         {

                                                                 ptr1=ptr->link;

                                                                 ptr->link=ptr1->link;

                                                                 delete ptr1;
                                                                
                                                                 return;

                                         }
                                       
                                        else

                                         {

                                             ptr=ptr->link;

                                         }

                         }

                     }

                 }

}

template<class T>

void list<T>::display()

{

     node<T> *ptr;

     if(first==NULL)

     {

                    cout<<"\n Sorry list is empty..";

     }

     else

     {

         ptr=first;

         while(ptr!=NULL)

         {
                        
                         cout<<ptr->data<<"   ";

                         ptr=ptr->link;

         }

     }

}

int main()

{

    int n;

    list <int> l;

    l.createlist();

    do

    {

                   cout<<"\n 1.Insertion \n2.Deletion \n3.Print List \n4.Exit \n";

                   cout<<"\n Enter your option : ";

                   cin>>n;

                   switch(n)

                   {

                            case 1: l.insertion();

                            break;

                            case 2: l.deletion();

                            break;

                            case 3: l.display();

                            break;

                            case 4:
                                 exit(0);

                            break;

                   }

    }while(n<=4);
   
    _getch();

    return 0;

}



Screenshot:
C++ Program Code To Implement Singly Linked List Using Template Class

Read More...