Amazon Affiliate

Thursday 9 August 2012

PROGRAM FOR FACTORIAL USING RECURSION IN JAVA...



import java.util.Scanner;

class Factorial{
      
        public static void main(String args[]){
              
                System.out.println("Enter the number: ");
              
              
  Scanner s = new Scanner(System.in);
                int a = s.nextInt();
              
              
                int result= fact(a);
              
              
                System.out.println("Factorial of the number is: " + result);
        }
      
        public static int fact(int b)
        {
                if(b <= 1)
                      
                        return 1;
                else
                      
                        return b * fact(b-1);
        }
}

PROGRAM FOR FIBONACCI SERIES USING RECURSION IN JAVA...


import java.util.Scanner;


public class Fibonacci {
public static long fib(int n) {
        if (n <= 1) return n;
        else return fib(n-1) + fib(n-2);
    }

    public static void main(String args[]) {
Scanner s = new Scanner(System.in);
  System.out.println("Enter the number :");
       int num = s.nextInt();
        for (int i = 1; i <= num; i++)
            System.out.println(i + ": " + fib(i));
    }
}

PROGRAM FOR STRING REVERSE USING RECURSION IN JAVA..


import java.util.Scanner;

class StringReverse{

public static void main (String[] args){

Scanner s = new Scanner (System.in);

System.out.println("Enter text to reverse: ");
String st = s.nextLine();
reverse(st);

}

public static void reverse(String s){

if (s.length() == 1){
System.out.print(s);
}
else{

reverse (s.substring(1, s.length()));
System.out.print(s.substring(0,1));

}
}

}

PROGRAM FOR LINEAR SEARCH USING RECURSION IN JAVA.


import java.util.Scanner;

class LinearSearch
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int size = input.nextInt();
        System.out.print("Enter an array of numbers: ");

        int[] arr = new int[size];

        for(int i=0; i<arr.length; i++)
        {
            arr[i]=input.nextInt();
        }

        System.out.print("Enter the number you want to search: ");
        int search = input.nextInt();

      

        Linear_Search access = new Linear_Search();

        System.out.print("The position of the search item is at array index ");
        access.linSearch2(arr, 0, arr.length, search);
    }

}
class Linear_Search

{

   public void linSearch2(int[] arr, int fIndex, int lIndex, int searchNum)

   {

  if(fIndex == lIndex)

  {

  System.out.print("-1");

  }

  else

  {

  if(arr[fIndex] == searchNum)

  {

  System.out.print(fIndex);

  }

  else

  {

  linSearch2(arr, fIndex+1, lIndex, searchNum);

  }

  }

   }
}

Thursday 31 May 2012

PROGRAM FOR SELECTION SORT....

// SELECTION SORT


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

   class selection
      {
       private:    int temp, current, j;

       public:
        void selection_sort(int *, int);
        void display(int *, int );

    };


void selection :: selection_sort(int array[], int size)
   {
   for (current = 0; current < size; current++)
    for (j = current + 1; j < size; j++)
      if (array[current] > array[j])
        {
          temp = array[current];
          array[current] = array[j];
          array[j] = temp;
        }
  }

   void selection :: display( int list[], int n)
       {
      cout<<"\n Sorted list is as follows :\n";
      for(int i = 0 ; i < n ; i++)
         {
           cout<<"  "<<list[i];
         }
    }

void main(void)
 {
   selection sort;

   int list[30];
   int number;

   cout<<"\n Input the number of the elements:";
   cin>>number;
   for (int i = 0; i < number; i++)
      {
    cout<<"\n Input the values for | "<<i+1<<" | ";
    cin>>list[i];
      }
       sort.selection_sort(list, number);
       sort.display(list, number);
    }

PROGRAM FOR MERGE SORT....

 // MERGE SORT


 # include<iostream.h>

   class merge_s
     {
      public:

      public:
        void merge_sort(float *, int , int , int );
        void merge_pass(float *, int , int );
     };

  // definition of the function

  void merge_s :: merge_sort(float l[], int top, int size, int bottom)
       {
     float temp[1000];

     int f = top;
     int s = size +1 ;
     int t = top ;

      while(( f <= size)&&(s <=bottom))
        {  if(l[f] <= l[s])
          {
        temp[t] = l[f] ;
        f ++;
          }

          else
           {
        temp[t] = l[s] ;
        s ++;
           }

           t ++;
         }

         if(f <=size)
         {
         for(f = f ; f<=size; f++)
           {
         temp[t] = l[f];
         t++;
           }
         }

         else
           {
        for(s = s ; s<= bottom ; s++)
         {
          temp[t]=l[s];
          t++;
         }

           }

           for( int upper = top; upper <= bottom ; upper++)
         {
          l[upper]=temp[upper];
         }

      }

   // definition of the function

      void merge_s ::  merge_pass( float append[], int m, int n )
    {
       if( m!= n)
          {
           int mid = (m+n)/2;
        merge_pass( append, m, mid );

        merge_pass( append, mid+1, n );
        merge_sort( append, m, mid, n );
          }
      }

// main function

   void main()
       {
     merge_s sort;
     float list[1000];
     int n;
     cout<<"\n Size of the list :";
     cin>> n;

      for(int i = 0 ; i<n ; i++)
        {
         cout<<"\n Elements of the list : "<< i+1 <<" : ";
         cin>>list[i];
        }

        cout << "\n Entered list as follows:\n";
        for( i = 0 ; i<n ; i++)
        cout <<"  "<< list[i];

        i = 0 ;

        sort.merge_pass(list, i, n-1);

        cout<<"\n Merge sorted list is as follows:\n";
        for( i = 0 ; i<n ; i++)
        cout <<"  "<< list[i];
      }




PROGRAM FOR LINEAR SEARCH....

 // LINEAR SEARCH


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

          int search;
    class linear_s
       {
        public: int flag;
            input( int *, int , int);
             int n, key;
             int list[200];

        public: void linear_search(int * , int , int ); // prototype

            void display(int *, int);
       };


// definition of function

  void  linear_s ::  linear_search(int l[], int n, int element)
  {

   flag = 1;
    for( int k = 0; k< n; k++)
    {
     if(l[k] == element)
      {
       cout <<"\n Search is successful \n";
    cout <<"\n Element:  " <<element
    << "  " <<"Found at Location : "<< (k+1);
    flag = 0 ;
       }
      }
      if (flag)
     cout<< "\n Search is unsuccessful";
  }


   void linear_s :: display(int list[], int n)
    {
        for( int i = 0 ; i < n ; i++)
           {
             cout<<"  "<< list[i];
           }
       }
linear_s :: input(int list[], int number, int key)
   {
       cout <<"Input the number of elements in the list:";
       cin >> number;
       cout <<"\n Number  of elements in the list is :"<<number<<"\n";

        for( int i = 0 ; i < number ; i++)
        {
          cout<<" Input the elements of the list : "<<i+1<<" : ";
          cin >> list[i];
        }
        cout<<"\n Element to be searched :";
        cin>>key;
        search = key;
        return number;
    }

    void main()
           {
          linear_s sort;

          int number, key, list[200];

          number = sort.input( list, number, key);

          key = search ;
          cout<<"\n Entered list as follows:\n";
          sort.display(list,number);
          sort.linear_search(list, number, key);
          cout<<"\n In the following list\n";
          sort.display(list,number);
           }
/*
input(int list[], int number, int key)
   {
       cout <<"Input the number of elements in the list:";
       cin >> number;
       cout <<"\n Number  of elements in the list is :"<<number;

        for( int i = 0 ; i < number ; i++)
        {
          cout<<"\nInput the elements of the list : "<<i+1<<" : ";
          cin >> list[i];
        }
        cout<<"\n Element to be searched :";
        cin>>key;
        search = key;
        return number;
    }

*/

PROGRAM FOR INSERTION SORT...

 // INSERTION SORT


 # include<iostream.h>

    class insertion_s
      {
        private: int key;

        public:
            int  insertion_sort(int *);  // prototype
            void display(int *, int);
       };

// definition of the insertion sort function

 int insertion_s :: insertion_sort(int list[])
         {
        cout<<"\nElement to be inserted Break condition is -0 : ";
        cin>>key;
        cout<<"\nSelected Element is :"<<key;
        int i = 0;
        int k ;

        while( key != -0 ) // -0 is break condition
        {
         k = i - 1;
         while((key < list[k]) && (k >= 0))
         {
          list[k+1] = list[k];
          --k;
         }

         list[k+1] = key ;
         cout<<"\n List after inserting an element ";
         for(int j = 0 ; j<=i; j++)
          cout<<"  "<<list[j];

          cout<<"\nElement to be inserted Break condition is -0: " ;
          cin>>key;
          cout<<"\nSelected Element is :"<<key;
          i ++;

      }
      return i;
    }

 // end of the insertion sort function

 // definition of the function

   void insertion_s :: display(int list[], int n)
       {
      cout<<"\n Final sorted list is as follows:\n";
      for(int j = 0 ; j < n ; j++)
      cout<<"  "<<list[j];
    }

// end of the display function

    void main()
           {
         insertion_s sort;
         int list[200];
         int n ;

         n = sort.insertion_sort(list);
         sort.display(list,n);
           }

PROGRAM FOR BUBBLE SORT FOR SORTING THE STRINGS...

// BUBBLE SORT FOR SORTING THE STRINGS


 # include<iostream.h>
 # include <stdlib.h>
 # include <string.h>
 # include<alloc.h>
 # include<stdio.h>

 class bubble_string
    {
    private:   char *temp;
           int i, j;
    public:
           void bubble_sort(char *array[], int );
           void display(char *list[], int);
     };


void bubble_string :: bubble_sort(char *array[], int size)
 {

   for (i = 0; i < size; i++)
    for (j = 0; j < size; j++)
      if (strcmp(array[i], array[j]) < 0)
    {
      temp =(char *) malloc(sizeof(array[i]));
      temp = array[i];
          array[i] = array[j];
          array[j] = temp;
        }
  }
    void bubble_string :: display(char *list[], int n)
      {

    for(int i = 0 ; i < n; i++)
       {
         cout<<"\n "<<list[i];
       }
      }


void main(void)
 {
   bubble_string b_sort;
   char *list[100] = {"MONDAY", "TUESDAY", "WENSDAY", "FRIDAY", "SATARDAY"};
   cout<<"\n Input list is as follows:";
   b_sort.display(list, 5);
   b_sort.bubble_sort(list, 5);
   cout<<"\n Sorted list is as follows:\n";
   b_sort.display(list, 5);
 }

INSERTION AND DELETION IN A QUEUE....

  // INSERTION AND DELETION IN A QUEUE..

 # include<iostream.h>
 # include<conio.h>
 # include<string.h>
 # include<ctype.h>
 # include<process.h>

 # define size 10

   class Queue
       {
     public: int rear, front;
           int ch;
           int q[size];

     public:  Queue()
            {
              rear = front = 0;
            }
         void Insert_queue();
         void Delete_queue();
         void Display_queue();

    };

 // Function to create queue

 void Queue :: Insert_queue()
    {
     cout<<"\n Input the element :";
     cin>>ch;
      if(rear < size)
       {
         rear ++;
         q[rear] = ch ;
         if(front == 0)
          front = 1;
       }
       else
         cout<<"\nQueue is overflow";
    }

// Function to perform delete operation

    void Queue :: Delete_queue() //char q[], char ch)
       {
         if (front == 0)
           {
         cout<<"\nUnderflow";
         return ;
           }
           else
           {
         ch = q[front];

         cout<<"\nElement deleted :"<<ch;
           }
           if(front == rear)
        {
         front = 0;
         rear = 0;
        }
        else
        front = front + 1;
           }

 // Output function

   void Queue :: Display_queue()  //char q[])
      {
    if (front == 0)
      return;
    for( int i = front ; i <= rear; i++)
     cout<<" "<<q[i];
      }

//Function main

    void main()
      {
        Queue Q;
        int k = 0;
        char choice;

         do
           {
             cout<<"\nInsert->i Delete->d Quit->q:";
             cout<<"\nInput the choice : ";
              do
              {
            cin>>choice;
            choice = tolower(choice);
               }while(strchr("idq",choice)==NULL);
            cout<<"Your choice is ->"<<choice;

      switch(choice)
       {
     case 'i' :
        Q.Insert_queue();
        cout<<"\nQueue after inserting ";
        Q.Display_queue();
        break;

    case 'd' : Q.Delete_queue();
           cout<<"\nQueue content after deleteion is as follows:\n";
           Q.Display_queue();
           break;

    case 'q': k = 1;
      }
    } while(!k);
  }

INSERT A NODE INTO A SIMPLE LINKED LIST AT THE END OF THE LIST...

// INSERT A NODE INTO A SIMPLE LINKED LIST AT THE END OF THE LIST


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

  struct link
    {
      char info[20];
      struct link *next;
    };

     class insert
     {
    private:
        int i;
        int number;
        link start, *previous, *new1;

    public:
         void insertion(link *);
         void create_list(link *);
         void display(link *);

      };

// Function create a linked list

  void insert :: create_list( link *node)
     {

   start.next = NULL;  // Empty list

   node = &start;      // Point to the start of the list
   i = 0;
   cout<<"\n Input choice n for break: ";
   char ch = getche();
   while(ch != 'n')
     {
       node->next = (struct link* ) malloc(sizeof(struct link));
       node = node->next;
       cout<<"\n Input the node :"<<(i+1)<<" : ";
       cin>>node->info;
       node->next = NULL;
       cout<<"\n Input choice n for break: ";
       ch = getche();
       i++;
     }
   }

// Inserting a node
    void insert :: insertion(link *node)
    {
     node = start.next;
     previous = &start;


     while(node)
      {
          node = node->next;
          previous= previous->next;
      }
         if(node == NULL)
           {
         new1 = (struct link* ) malloc(sizeof(struct link));
         new1->next = node ;
         previous->next = new1;
         cout<<"\n Input the last node value: ";
         cin>>new1->info;
           }
      }

// Display the list

  void insert :: display(link *node)
    {
    node = start.next;
    cout<<"\n After Inserting a node list is as follows:\n";
    while (node)
      { cout<<"\n "<<node;
    cout<<"  "<< node->info;
    node = node->next;
      }
    }
     void main()
     {
        insert i_mode;
        link *node = (link *) malloc(sizeof(link));

        i_mode.create_list(node);
        i_mode.insertion(node);
        i_mode.display(node);
     }

INSERT A NODE INTO A SIMPLE LINKED LIST AT THE BEGINNING...

// INSERT A NODE INTO A SIMPLE LINKED LIST AT THE BEGINNING


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

  struct link
    {
      int info;
      struct link *next;
    };

     class insert
     {
    private:
        int i;
        int number;
        link start, *previous, *new1;

    public:
         void insertion(link *);
         void create_list(link *);
         void display(link *);

      };

// Function create a linked list

  void insert :: create_list( link *node)
     {

   start.next = NULL;  // Empty list

   node = &start;      // Point to the start of the list
   i = 0;
   cout<<"\n Input choice n for break: ";
   char ch = getche();
   while(ch != 'n')
     {
       node->next = (struct link* ) malloc(sizeof(struct link));
       node = node->next;
       cout<<"\n Input the node :"<<(i+1)<<" : ";
       cin>>node->info;
       node->next = NULL;
       cout<<"\n Input choice n for break: ";
       ch = getche();
       i++;
     }
   }

// Inserting a node

    void insert :: insertion(link *node)
    {
     node = start.next;

     previous = &start;

         new1 = (struct link* ) malloc(sizeof(struct link));
         new1->next = node ;
         previous->next = new1;
         cout<<"\n Input the fisrt node value: ";
         cin>>new1->info;
    }

// Display the list

  void insert :: display(link *node)
    {
    node = start.next;
    cout<<"\n After Inserting a node list is as follows:\n";
    while (node)
      {
    cout<<"\n"<< node;
    cout<<"  "<< node->info;
    node = node->next;
      }
    }
     void main()
     {
        insert i_mode;
        link *node = (link *) malloc(sizeof(link));

        i_mode.create_list(node);
        i_mode.insertion(node);
        i_mode.display(node);

     }

DELETING FIRST NODE FROM A SIMPLE LINKED LIST...

// DELETING FIRST NODE FROM A SIMPLE LINKED LIST


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

  struct link
    {
      int data;
      struct link *next;
    } ;

    class D_node
      {
    private:
       int i;
       int number ;
       link start, *node, *previous;
   public:

   void  Delete_node();
   void  creat_node();
   };

   void D_node :: creat_node()
   {
     char ch;
     i = 0;

     start.next = NULL;  // Empty list
     node = &start;      // Point to the start of the list

     cout<<"\n Input choice n for break:";
     ch = getche();

   while(ch != 'n')
     {
       node->next = (struct link* ) malloc(sizeof(struct link));
       node = node->next;
       cout<<"\n Input the node :"<<(i+1)<<" : ";
       cin>>node->data;
       node->next = NULL;

       cout<<"\n Input choice n for break:";
       ch = getche();
       i++;
     }

     node = start.next;
     previous = &start;
     cout<<"\n Created list is as follows\n";

     while (node)
      {
    cout<<"\n "<<node;
    cout<<"  "<< node->data;
    node = node->next;
      }
    }


 // Removing a node
  void D_node:: Delete_node()
   {
     node = start.next;
     previous = &start;

     if (node == NULL)
       {
     cout<<"\n Under flow";
       }
       else
      {
         previous->next = node->next;
         free(node);
      }

  // Display the list

    node = start.next;
    cout<<"\n After deleting first node list is as follows\n";
    while (node)
      {
    cout<<"\n "<<node;
    cout<<"  "<< node->data;
    node = node->next;
      }
 }


    void main()
    {
       D_node del_node;
       del_node.creat_node();
       del_node.Delete_node();
    }

DELETING LAST NODE FROM A SIMPLE LINKED LIST...

// DELETING LAST NODE FROM A SIMPLE LINKED LIST


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

  struct link
    {
      char info;
      struct link *next;
    };

    class D_node
      {
    private:
           int i;
           int number ;
           link start, *previous;
    public:

           void  Delete_node(link *);
           void  creat_node(link *);
   };

   void D_node :: creat_node(link *node)
   {
     char ch;
     i = 0;

     start.next = NULL;  // Empty list
     node = &start;      // Point to the start of the list

     cout<<"\n Input choice n for break:";
     ch = getche();

   while(ch != 'n')
     {
       node->next = (struct link* ) malloc(sizeof(struct link));
       node = node->next;
       cout<<"\n Input the node :"<<(i+1)<<" : ";
       cin>>node->info;
       node->next = NULL;

       cout<<"\n Input choice n for break:";
       ch = getche();
       i++;
     }

     node = start.next;
     previous = &start;
     cout<<"\n Created list is as follows\n";

     while (node)
      {
    cout<<"\n "<<node;
    cout<<"  "<< node->info;
    node = node->next;
      }
    }

 // Removing a node

  void D_node:: Delete_node(link *node)
    {
     int node_number = 0;

     node = start.next;
     previous = &start;

     if (node == NULL)
       {
     cout<<"\n Underflow";
       }

      else

       while(node)
    {
      node = node->next;
      previous = previous->next;
      node_number ++;
    }
    cout<<"\n Total Nodes = "<<node_number;

     node = start.next;
     previous = &start;

     while(node_number != 1)
    {
      node = node->next;
      previous = previous->next;
      node_number --;
    }
      if(node_number == 1)
          {
         previous->next = node->next;
         free(node);
          }
  // Display the list

    node = start.next;
    cout<<"\n After deleting last node list is as follows\n";
    while (node)
      {
    cout<<"\n "<<node;
    cout<<"  "<< node->info;
    node = node->next;
      }
 }

// Function main

    void main()
    {
       D_node del_node;
       link *node = (link *) malloc(sizeof(link));
       del_node.creat_node(node);
       del_node.Delete_node(node);
    }

CREATING HEADER LINKED LIST....

// CREATING HEADER LINKED LIST....


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

  struct link
    {
      int info;
      struct link *next;
    };

     class Header
     {
    private:
        int i;
        int number;
        link *start, *new1;

    public:
         void insertion(link *);
         void create_header_list(link *);
         void Traverse (link *);
      };

// Function to create a header linked list

  void Header :: create_header_list( link *node)
     {

   start->next = NULL;  // Empty list

   node = start;      // Point to the header node of the list
   node->next = (link* ) malloc(sizeof(link)); // Create header node
   i = 0;
   cout<<"\n Input choice n for break: ";
   char ch = getche();
   while(ch != 'n')
     {
       node->next = (link* ) malloc(sizeof(link));
       node = node->next;
       cout<<"\n Input the node :"<<(i+1)<<" : ";
       cin>>node->info;
       node->next = NULL;
       cout<<"\n Input choice n for break: ";
       ch = getche();
       i++;
     }
     cout<<"\n Total nodes = "<< i;
     node = start;
     node->info = i; // Assign total number of nodes to the header node
   }

// Inserting a node
    void Header :: insertion(link *node)
    {
     node = start;
     int count = node->info;
     node = node->next;

     int node_number = 1;
     int insert_node;

     cout<<"\n Input node number you want to insert:";
     cout<<"\n Value should be less are equal to the";
     cout<<"\n number of nodes in the list: ";
     cin >> insert_node;

     while(count)
      {
        if((node_number) == insert_node)
           {
         new1 = (link* ) malloc(sizeof(link));
         new1->next = node->next ;
         node->next = new1;
         cout<<"\n Input the node value: ";
         cin>>new1->info;
         node = node->next;
           }
        else
        {
          node = node->next;
          count--;
        }
           node_number ++;
         }
         node = start;
         node->info = node->info+1;
    }

// Traversing the header list

  void Header :: Traverse(link *node)
    {
    node = start;
    int count = node->info;

    node = node->next;
    cout<<"\n After Inserting a node list is as follows:\n";
    while (count)
      {
    cout<<"\n "<<node;
    cout<<"  "<< node->info;
    node = node->next;
    count --;
      }
    }

// Function main

     void main()
     {
        Header Header_mode;
        link *node = (link *) malloc(sizeof(link));

        Header_mode.create_header_list (node);
        Header_mode.Traverse (node);
        Header_mode.insertion (node);
        Header_mode.Traverse (node);

     }

MERGING TWO DOUBLY LINKED LISTS (ASCENDING ORDER)....

//  MERGING TWO DOUBLY LINKED LISTS (ASCENDING ORDER)


 # include<iostream.h>
 # include <stdio.h>
 # include <alloc.h>

    struct Double
    {
     int info;
     struct Double *next;
     struct Double *previous;
   };

  class D_insert
    {
       public:  int i;
            Double  start, start1, *new1, *local;
       public:
            void Doubly_insertion (Double * );
            void Doubly_Create (Double * );
            void Display (Double *);
    };

// Function create a list of five nodes

  void D_insert :: Doubly_Create (Double *node)
      {
       start.next = NULL;  // Empty list
       start.previous = NULL;
       node = &start;      // Point to the start of the list


    for (i = 1; i < 10; i += 2)
         {
           node->next = (Double *) malloc(sizeof(Double ));
           node->next->previous = node;
           node = node->next;
           node->info = i;
           node->next = NULL;
         }
    }


 void D_insert :: Doubly_insertion (Double *node)
   {
       Double *new1;
       start1.next = NULL;  // Empty list
       start1.previous = NULL;
       new1 = &start1;      // Point to the start of the list

    for (i = 2; i <= 10; i += 2)
         {
           new1->next = (Double *) malloc(sizeof(Double ));
           new1->next->previous = new1;
           new1= new1->next;
           new1->info = i;
           new1->next = NULL;
         }

    new1 = start1.next;
    cout<<"\n Second list is as follows";
    while(new1)
    {
      cout<<"\n "<<new1;
      cout<<"  "<<new1->info;
      new1 = new1->next;
    }
    new1 = start1.next;

     while(new1)
     {
       int found = 0;
       local = (Double *) malloc(sizeof(Double));
       local = new1;
       new1 = new1->next;

       node = start.next;

       do
    {
     if ( node->info > local->info )
       {
         local->next = node;
         local->previous = node->previous;
         node->previous->next = local;
         node->previous = local;
         found = 1;
         break;
         }
     else
       node = node->next;
       } while ((node->next) && (! found));

       if (! found)
    if (node->info> local->info)
      {
         local->next = node;
         local->previous = node->previous;
         node->previous->next = local;
         node->previous = local;
      }
       else
      {
         local->next = NULL;
         local->previous = node;
         node->next = local;

      }
     }
   }

 // Display the list

   void D_insert :: Display (Double *node)
      {
        node = start.next;

       while (node)
    {
      cout<<"\n "<<node;
      cout<<"  " <<node->info;
      node = node->next;
    }
      }


  void main()
    {
      D_insert D_ins;
      Double *node = (Double *) malloc(sizeof(Double));
      D_ins.Doubly_Create (node);
      cout<<"\n First list is as follows\n";
      D_ins.Display (node);
      D_ins.Doubly_insertion (node);
      cout<<"\n List after merging above two lists (Ascending order)\n";
      D_ins.Display (node);

    }

DELETE A NODE FROM A SIMPLE DOUBLY LINKED LIST...

// DELETE A NODE FROM A SIMPLE DOUBLY LINKED LIST


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

     struct Double
     {
       char info;
       struct Double *next;
       struct Double *previous;
      };

      class D_link
     {
       private: int num ;
            Double start;

       public:
           void Doubly_Link_Del (Double *);
           void Doubly_Link_Creat (Double *);
           void display (Double *);
    };

// Function creates a doubly linked list

  void D_link :: Doubly_Link_Creat (Double *node)
    {
       start.next = NULL;  // Empty list
       start.previous = NULL;
       node = &start;      // Point to the start of the list

       num = 0;

      cout<<"\n Input choice n for break:";
     char ch = getche();

     while(ch != 'n')
      {
       node->next = (Double *) malloc(sizeof(Double));
    node->next->previous = node;
     node = node->next;
      cout<<"\n Input the values of the node :"<<(num+1)<< " : ";
       cin >> node->info;
        node->next = NULL;

         cout<<"\n Input choice n for break:";
          ch = getche();
           num ++;
       }
       cout<<"\n Total nodes = "<<num;
     }

////////////////////////////////////////////////////
// Function delete

  void D_link :: Doubly_Link_Del (Double *node)
      {
       int delete_node;
       int search_counter = 0;

       cout<<"\n Input the node number to which you want delete: ";
       cin >> delete_node;

       node = start.next;
       if ( node == NULL)
        {
          cout<<"\n Underflow\n";
          cout<<"\n List is empty\n";
        }
       else

     while(node)
       {
     if((search_counter + 1) == delete_node)
        {
          node->previous->next = node->next ;
          node->next->previous = node->previous ;
          free(node);
         }
       else
        {
         node = node->next;

        }
         search_counter++;
       }
     }

////////////////////////////////////////////////////
    // Display the list

   void D_link :: display(Double *node)
     {
      node = start.next;

     while (node)
      {
    cout<<"\n "<<node;
    cout<<"  "<< node->info;
    node = node->next;
      }
    }

// Function main

    void main()
     {
       D_link Doubly_del;
        Double *node = (Double *) malloc(sizeof(Double));
         Doubly_del.Doubly_Link_Creat(node);
          cout<<"\n Created linked list is as follows\n";
           Doubly_del.display(node);
          Doubly_del.Doubly_Link_Del(node);
        cout<<"\n After deletion of a node linked list is as follows\n";
      Doubly_del.display(node);
     }

INSERTING SOME NODES IN THE DOUBLY LINKED LIST...

//  INSERTING SOME NODES IN THE DOUBLY LINKED LIST


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

    struct Double
    {
     char info[20];
     struct Double *next;
     struct Double *previous;
   };

  class D_insert
    {
       public:  int i;
            Double  start, *new1;
       public:
            void Doubly_insertion_First (Double *);
            void Doubly_Create_First (Double *);
            void Display (Double *);
    };

// Function create a list of five nodes

  void D_insert :: Doubly_Create_First (Double *node)
      {
       int i = 0;
    start.next = NULL;  // Empty list
     start.previous = NULL;
      node = &start;      // Point to the start of the list

    cout<<"\n Input choice n for break: ";
     char ch = getche();

       while (ch != 'n')
         {
          node->next = (Double *) malloc(sizeof(Double ));
           node->next->previous = node;
        node = node->next;
         cout<<"\n Input the value for :"<<i+1<<" : ";
           cin>>node->info;
         node->next = NULL;
       i++;
     cout<<"\n Input choice n for break: ";
    ch = getche();
     }
  }


 void D_insert :: Doubly_insertion_First (Double *node)
   {
       node = start.next;

       new1 = (Double *) malloc(sizeof(Double ));
       cout<<"\n Input the first node  value; ";
       cin>>new1->info;
         cout<<"\n new1 address:"<<new1;
         cout<<"\n Node address:"<<node;
         new1->next = node;
         cout<<"\n New1 next address:"<<new1->next;
         cout<<"\n Node previous address:"<<node->previous;
         new1->previous = node->previous;
         node->previous->next = new1;
         cout<<"\n node->previous.next:"<<node->previous->next;
          cout<<"\n Node previous address:"<<node->previous;
          cout<<"\n Node next address:"<<node->next;
         cout<<"\n new1 address:"<<new1;
         node->previous = new1;
         cout<<"\n node->previous:"<<node->previous;
   }

 // Display the list

   void D_insert :: Display (Double *node)
      {
        node = start.next;

       while (node)
    {
      cout<<"\n "<<node;
      cout<<"  " <<node->info;
      node = node->next;
    }
      }


  void main()
    {
      D_insert D_ins;
      Double *node = (Double *) malloc(sizeof(Double));
      D_ins.Doubly_Create_First (node);
      cout<<"\n Created list is as follows\n";
      D_ins.Display(node);
      D_ins.Doubly_insertion_First (node);
      cout<<"\n List after insertion of first node \n";
      D_ins.Display (node);

    }

CREATING CIRCULAR HEADER LINKED LIST....

// CREATING CIRCULAR HEADER LINKED LIST..


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

  struct link
    {
      int info;
      struct link *next;
    };

     class Circular
     {
    private:
        int i; // Represents number of nodes in the list
        int number;
        link *start, *new1;

    public:
         void insertion(link *);
         void create_circular_list(link *);
         void display(link *);

      };

// Function create a circular header linked list

void Circular :: create_circular_list( link *node)
 {

   node = start;      // Point to the header node in the list
   node->next = (link* ) malloc(sizeof(link));
   i = 0;
   cout<<"\n Input choice n for break: ";
   char ch = getche();
   while(ch != 'n')
     {
       node->next = (link* ) malloc(sizeof(link));
       node = node->next;
       cout<<"\n Input the node :"<<(i+1)<<" : ";
       cin>>node->info;
       cout<<"\n Input choice n for break: ";
       ch = getche();
       i++;
     }
     cout<<"\n Total nodes = "<< i;
     node = start;
     node->info = i; // Assign total number of nodes to the header node
   }

// Inserting a node in circular header linked
    void Circular :: insertion(link *node)
    {
     node = start;
     int count = node->info;
     node = node->next;

     int node_number = 0;
     int insert_node;

     cout<<"\n Input node number you want to insert:";
     cout<<"\n Value should be less are equal to the";
     cout<<"\n number of nodes in the list: ";
     cin >> insert_node;

     while(count)
      {
        if((node_number+1) == insert_node)
           {
         new1 = (link* ) malloc(sizeof(link));
         new1->next = node->next ;
         node->next = new1;
         cout<<"\n Input the node value: ";
         cin>>new1->info;
          node = node->next;
          count--;
           }
        else
        {
          node = node->next;
          count--;
        }
           node_number ++;
         }
         if (count == 0)
          {
        node = start;  // Points to header node
        node->info = node->info+1;
          }
    }

// Display the list

  void Circular :: display(link *node)
    {
    node = start;
    int count = node->info;

    node = node->next;

    while (count)
      {
    cout<<"\n "<<node;
    cout<<"  "<< node->info;
    node = node->next;
    count --;
      }
    }

// Function main

     void main()
     {
        Circular Circular_List;
        link *node = (link *) malloc(sizeof(link));
        Circular_List.create_circular_list(node);
        cout<<"\n Before inserting a node list is as follows:\n";
        Circular_List.display(node);
        Circular_List.insertion(node);
        cout<<"\n After inserting a node list is as follows:\n";
        Circular_List.display(node);

     }

UPDATING A STACK IMPLEMENTED WITH THE HELP OF ARRAYS..

// UPDATING A STACK IMPLEMENTED WITH THE HELP OF ARRAYS.


 # include<iostream.h>
 # include<stdio.h>
 # include<string.h>
 # include<ctype.h>

 # define size  100

    int top = -1;
    int flag = 0;

    class stacks
     {
       private: int stack[100];
            int data;

       public:
           void push (int *, int);
           int update (int *);
           void display (int *);
      };

// definition of the push function

    void stacks :: push(int s[], int d)
         {
          if(top ==(size-1))
         flag = 0;
         else
         {
         flag = 1;
         ++top;
         s[top] = d;

         }
         }

// definition of the pop function

    int stacks :: update(int s[])
         {
           int i;
           cout<<"\n Input the information number to which you want to update:";
           cin>> i;
           int update_element;
          if(top - i + 1 < 0)
             {
            update_element = 0;
            flag = 0;
             }
          else
           {
             flag = 1;
             update_element = s[top-i +1];
             cout<<"\n Input the new value:";
             cin>> s[top-i+1];
           }
           return (update_element);
        }

// definition of the display function

    void stacks :: display(int s[])
        {
           if(top == -1)
          {
            cout<<"Stack is empty";
          }
           else
         {
           for(int i = top; i>=0; --i)
           cout<<"  "<<s[i];
         }
          }

          void main()
           {
         stacks s;
         int stack[size];
         int  data;
         char choice;
         int q = 0;
         int top = -1;

         do
           {
             cout<<" \nPush->i Update->u Quit->q:";
             cout<<"\nInput the choice : ";
              do
              {
            cin>>choice;
            choice =tolower(choice);
               }while(strchr("iuq",choice)==NULL);
            cout<<"Your choice is ->"<<choice;

      switch(choice)
       {
     case 'i' :
           cout<<"\n Input the element to pushed:";
           cin>>data;
           s.push(stack,data);
           if(flag)
           {
           cout<<"\n After inserting ";
           s.display(stack);
           if(top == (size-1))
            cout<<"\n Stack is full";
           }
           else
           cout<<"\n Stack overflow after pushing";
           break;

    case 'u' : data = s.update(stack);
          if(flag)
            {
              cout<<"\n Data is peepped:"<<data;
              cout<<"\n Stack is as follows:\n";

              s.display(stack);
            }
            else
            cout<<"\n Stack underflow";
            break;
    case 'q': q = 1;
      }
    } while(!q);
   }

PROGRAM USING INHERITANCE IN JAVA....


Q3) Derive sub-classes of  ContractEmployee namely HourlyEmployee & WeeklyEmployee with information number of hours & wages per hour, number of weeks & wages per week respectively  & method calculateWages() to calculate their monthly salary. Also override getDesig () method depending on the type of contract employee.
ANSWER:

class Employee
{
            private String firstName;
            private String lastName;
            public String getFirstName()
            {
                        return firstName;
            }
            public String getLastName()
            {
                        return lastName;
            }
            public void setFirstName(String firstName)
            {
                        this.firstName=firstName;
            }
            public void setLastName(String lastName)
            {
                        this.lastName=lastName;
            }
}
class ContractEmployee extends Employee
{
            String depart;
            String desig;
            double salary;
            public void setDepartment(String depart)
            {
                        this.depart=depart;
            }
            public void setDesignation(String desig)
            {
                        this.desig=desig;
            }
            public void setSalary(double salary)
            {
                        this.salary=salary;
            }
            public String getDepartment()
            {
                        return depart;
            }
            public String getDesignation()
            {
                        return desig;
            }
            public double getSalary()
            {
                        return salary;
            }
            public void dispFullName()
            {
                        System.out.println(getFirstName()+getLastName());
            }
}
class RegularEmployee extends Employee
{
            String depart;
            String desig;
            double salary;
            public void setDepartment(String depart)
            {
                        this.depart=depart;
            }
            public void setDesignation(String desig)
            {
                        this.desig=desig;
            }
            public void setSalary(double salary)
            {
                        this.salary=salary;
            }
            public String getDepartment()
{
                        return depart;
            }
            public String getDesignation()
            {
                        return desig;
            }
            public double getSalary()
            {
                        return salary;
            }
            public void dispFullName()
            {
                        System.out.print(getFirstName() + " " + getLastName());
            }
}
class HourlyEmployee extends ContractEmployee
{
            int noOfHrs;
            double wagesPerHrs;
            public void setHours(int noOfHrs)
            {
                        this.noOfHrs=noOfHrs;
            }
            public void setWages(double wagesPerHrs)
            {
                        this.wagesPerHrs=wagesPerHrs;
            }
            public double calculateSalary()
            {
                        return noOfHrs*wagesPerHrs;
            }
            public String getDesignation()
            {
                        return "HourlyEmployee";
            }
               }
class WeeklyEmployee extends ContractEmployee{
            int noOfWeeks;
            double wagesPerWeek;
            public void setWeeks(int noOfWeeks)
            {
                        this.noOfWeeks=noOfWeeks;
            }
            public void setWages(double wagesPerWeek)
            {
                        this.wagesPerWeek=wagesPerWeek;
            }
            public double calculateSalary()
            {
                        return noOfWeeks*wagesPerWeek;
            }
            public String getDesignation()
            {
                        return "WeeklyEmployee";
            }
}