Thursday, June 27, 2013

Binary Tree – C++.




#include <iostream.h>
#include <stdlib.h>

       class node
      {
           private:
              int data;
              node *left;
              node *right;
              node *root;
            int Ctr;
           public:
              node()
                   {
                   Ctr=0;
                        root=NULL;
                   }

               void preorder(node *);
               void inorder(node *);
               void postorder(node *);
              node *insert(node *root,int val)
                   {
                        if(root==NULL)
                            {
                                root=new node();
                                root->left=root->right=NULL;
                                root->data=val;
                                Ctr++;
                            }
                        else if(Ctr%2==0)
                     {
                                root->left=insert(root->left,val);
                        }
                        else
                        {
                                root->right=insert(root->right,val);
                        }
                        return(root);
                   }
      };
  void node::inorder(node *root)
      {
           if(root!=NULL)
              {
                   inorder(root->left);
                   cout<<root->data;
                 inorder(root->right);
              }
       }

  void node::preorder(node *root)
      {
           if(root!=NULL)
              {
                   cout<<root->data;
                   preorder(root->left);
                   preorder(root->right);
              }
       }

  void node::postorder(node *root)
      {
           if(root!=NULL)
              {
                   postorder(root->left);
                   postorder(root->right);
                   cout<<root->data;
              }
       }

  int main()
      {
           node nD,*tree=NULL;
           int ch,val;
           do
              {
                   cout<<"\n\t\t***Binary Tree.***";
                   cout<<"\n\t1.Insert New Node.";
                   cout<<"\n\t2. PreOrder Traversal.";
                   cout<<"\n\t3. InOrder Traversal.";
                   cout<<"\n\t4. PostOder Traversal.";
                   cout<<"\n\t5. Exit.";
                   cout<<"\n\tChoice ? ";
                   cin>>ch;
                   switch(ch)
                        {
                            case 1:
                                cout<<"\n\tEnter Element : ";
                                cin>>val;
                                tree=nD.insert(tree,val);
                                break;
                            case 2:
                                 cout<<"\n\t **Pre-Order Traversal ** ";
                                 nD.preorder(tree);
                                 break;
                             case 3:
                                 cout<<"\n\t **In-Order Traversal ** ";
                                 nD.inorder(tree);
                                 break;
                             case 4:
                                 cout<<"\n\t **Post-Order Traversal ** ";
                                 nD.postorder(tree);
                                 break;
                             case 5:
                                 exit(0);
                        }
              }while(ch!=5);
           return 0;
      }

Friday, June 21, 2013

Single Linked List with Template Class – C++.




#include <iostream.h>
template <class T>
  class SList
      {
           private:
              T data;
              SList *next,*head, *Ptr;
           public:
              SList()
                   {
                        head=NULL;
                   }
              void Insert(int);
              void Delete(int);
              void disp();
              void Rev();
      };

  template <class T>
  void SList<T>::Insert(int num)
      {
           SList *temp,*pr;
           temp=new SList();
           temp->data=num;
           temp->next=NULL;
           if(head==NULL)
              {
                   Ptr=head=temp;
              }
           else
              {
                   Ptr->next=temp;
                   Ptr=temp;
              }
      }

  template <class T>
  void SList<T>::Rev()
      {
           SList *ptr1,*ptr2,*ptr3;
           ptr2=ptr3=NULL;
           ptr1 = head;
           while(ptr1!=NULL)
              {
                   ptr3=ptr2;
                   ptr2=ptr1;
                   ptr1=ptr1->next;
                   ptr2->next=ptr3;
              }
          head=ptr2;
      }

  template <class T>
  void SList<T>::disp()
      {
           SList *pr;
           cout<<"\n\tData : ";
           for(pr=head;pr!=NULL;pr=pr->next)
              {
                   cout<<pr->data<<" ";
              }
      }

  template <class T>
  void SList<T>::Delete(int num)
      {
           SList *pr,*ptr1;
           for(pr=head;pr!=NULL;pr=pr->next)
              {
                   if(pr->data==num)
                        {
                            break;
                        }
                   ptr1=pr;
              }
           if(pr==head)
              {
                   head=head->next;
              }
           else if(pr==NULL)
              {
                        cout<<"\n\tData not in the List \n";
              }
           else
               {
                   ptr1->next=pr->next;
              }
           delete pr;
      }

  int main()
      {
           int ch=0;
           int num;
           SList <int>st;
           while(ch<5)
              {
                   cout<<"\n\t1.Add. \n";
                   cout<<"\t2.Display. \n";
                   cout<<"\t3.Delete. \n";
                   cout<<"\t4.Reverse. \n";
                   cout<<"\t5.Exit. \n";
                   cout<<"\tChoice? ";
                   cin>>ch;
                   switch(ch)
                        {
                            case 1:
                                cout<<"\n\nEnter a No. ";
                                cin>>num;
                                st.Insert(num);
                                break;
                            case 2:
                                st.disp();
                                break;
                            case 3:
                                cout<<"\n\nEnter a No. ";
                                cin>>num;
                                st.Delete(num);
                                break;
                            case 4:
                                st.Rev();
                                break;
                            case 5:
                                      cout<<"\n\n\TYou Chose To Exit. \n";
                                      break;
                        }
              }

           return 0;
      }

Thursday, June 13, 2013

Doubly Linked List – C++.




Program for Doubly Linked List 



#include <iostream.h>

  class Dlist
      {
           private:
              int n;
              Dlist  *next, *prev,*head,*ptr,*pr;
           public:
              Dlist()
                   {
                        head=NULL;
                   }
              void getData();
              void disp();
              void del();
              void insert();
      };

  void Dlist::getData()
      {
           Dlist *tmp;
           tmp=new Dlist();
           tmp->prev=tmp->next=NULL;
           cout<<"\n\tEnter No :";
           cin>>tmp->n;
           if(head==NULL)
              {
                   ptr=head=tmp;
              }
           else
              {
                   tmp->prev=ptr;
                   ptr->next=tmp;
                   ptr=tmp;
              }
           pr=tmp;
      }
  void Dlist::disp()
      {
           Dlist *tmp;
           cout<<"\n\t\tList\t\t";
           for(tmp=head;tmp!=NULL;tmp=tmp->next)
              {
                   cout<<tmp->n<<" - ";
              }
           tmp=pr;
           cout<<"\n\t\tReverse \t";
           for(;tmp!=NULL;tmp=tmp->prev)
              {
                   cout<<tmp->n<<" - ";
              }
        cout<<"\n";
      }

  void Dlist::del()
      {
           int num;
           Dlist *tmp,*ptr1;
           cout<<"\n\t\t\tNumber to Delete? ";
           cin>>num;
           for(tmp=head;tmp!=NULL;tmp=tmp->next)
              {
                   if(tmp->n==num)
                        {
                            break;
                        }
                   else
                        {
                            ptr1=tmp;
                        }
              }
           if(tmp==head)
              {
                   head=head->next;
                   head->prev=NULL;
              }
           else if(tmp==NULL)
              {
                   cout<<"\n\t\tNo. is not Found!...";
              }
           else
              {
                   ptr1->next=tmp->next;
                   tmp->next->prev=ptr1;
              }
        delete(tmp);
      }

  void Dlist::insert()
      {
           int pos,i=0;
           Dlist *tmp,*ptr1,*ptr2;
           if(head==NULL)
              {
                   cout<<"\n List not created !!!";
              }
           else
              {
                   tmp=new Dlist();
                   tmp->next=tmp->prev=NULL;
                   cout<<"\n\tPostion ? ";
                   cin>>pos;
                   cout<<"\n\tNew Data : ";
                   cin>>tmp->n;
                   ptr1=head;
                   while(ptr1->next!=NULL)
                        {
                            i++;
                            if(i==pos)
                                {
                                      break;
                                }
                            else
                                {
                                      ptr2=ptr1;
                                }
                            ptr1=ptr1->next;
                        }
           if(ptr1==head)
              {
                   tmp->next=head;
                   head->prev=tmp;
                   head=tmp;
              }
           else
              {
                   cout<<ptr1->n<<"  "<<ptr2->n;;
                   ptr2->next=tmp;
                   tmp->prev=ptr2;
                   tmp->next=ptr1;
                   ptr1->prev=tmp;

              }
           }

      }

  int menu(int ch)
      {
           cout<<"\n\t\tMain Menu\n";
           cout<<"\n\t[1.]\t Add";
           cout<<"\n\t[2.] \t Delete";
           cout<<"\n\t[3.] \t Display";
           cout<<"\n\t[4.] \t Insert";
           cout<<"\n\t[5.] \t Exit";
           cout<<"\n\tEnter a Choice : ";
           cin>>ch;
           return ch;
      }

void main()
  {
    int ch=0;
    Dlist DL;
    do
       {
    ch=menu(ch);
    switch(ch)
       {
           case 1:DL.getData(); break;
           case 2:DL.del();break;
           case 3:DL.disp();break;
           case 4:DL.insert();break;
           case 5:cout<<ch<<"Quit . . . .";
       }
  }while(ch!=5);
   }