Monday, September 23, 2013

Program of insertion/ deletion in an arrayList - Problem 21 P N 707 ISC Board

Define a class repeat which allows the user to add elements from one end (rear) and remove elements from the other end (front) only.
The following details of the class repeat are given below:
Class name                         :               repeat
Data members/instance variables:
St[]                                                         : an array to hold a maximum of 100 integer elements
cap                                                         : stores the capacity of the array
f                                                              : to point the index of the front
r                                                              : to point the index of the rear
Member functions:
Repeat(int m)                                    : constructor to initialize the data members cap=m, f=-1, r=-1 and to
                                                                  create the integer array
void pushvalue(int v)                     :to add integers from the rear index if possible else display the           
                                                                message (“OVERFOW”)
int popvalue()                                   : to remove and return element from the front. If array is empty  
                                                                then return -9999

void disp()                                           : display the elements present in the list

======================================================================

/**
 * class repeat inserts and deletes elements from the arraylist.
 * 
 * @author: Nitendra Verma
 * @version: Sept 23, 2013
 */
import java.util.Scanner;
class repeat
{
   protected int st[];      //array to hold elements
   protected int cap;       //stores the capacity of the array
   protected int f,r;       //to point to the first and last element 
   
   public repeat(int m)     //constructor
   {
       cap=m;
       f=-1;
       r=-1;
       st=new int[cap];
    }
    
    public boolean isEmpty()    //function- returns true if array is empty
    {
        if(f==-1)
          return true;
        return false;  
    }
    
    public void pushvalue(int v)    //function- insert a new element at the rear of the array
    {
        if(r==-1)
          {
              f=0;
              r=0;
              st[r]=v;
            }
            else if(r+1<st.length)
              st[++r]=v;
              else
              System.out.println("OVERFLOW OCCURES!!!");
    }
    
    public int popvalue()   //function- deletes and returns the last(rear) element in the array
    {
        int elem;
        boolean res;
        res=isEmpty();
        if(res==true)
          return -9999;
        else
        {
            elem=st[f];
            if(f==r)
              f=r=-1;
            else
              f++;
            return elem;
        }
    }
    
    public void disp()  //display the elements in the array
    {
        boolean res;
        res=isEmpty();
        if(res==true)
          System.out.println("No Element!!!");
         else
         {
             System.out.println("Elements are:");
             for(int i=f;i<=r;i++)
                 System.out.print(st[i]+"\t");
              System.out.println("Total number of elements:"+(r-f+1));   
            }
        
    }
}

public class test
{
  public static void main()
  {
      Scanner in=new Scanner(System.in);
      System.out.println("Eneter the capacity of array");
      int cap=in.nextInt();
      int ans;
      repeat ob=new repeat(cap);
      System.out.println("Eneter the value to be inserted");
      int item=in.nextInt();
      ob.pushvalue(item);
      
      for(int i=0;i<10;i++)
       {
       System.out.println("Do you want to insert more item? Press 1 for Yes and 2 for No.");
       ans=in.nextInt();
       if(ans==1 && ans<cap)
       {
           System.out.println("Enetr item to be pushed");
           int numnext=in.nextInt();
           ob.pushvalue(numnext);
        }
        else
           break;
    }
    System.out.println("Items in stack are:");
    ob.disp();       
    
    
       int anspop,res;
       System.out.println("Want to pop?Press 1 for Yes, 2 for No.");
       anspop=in.nextInt();
       if(anspop==1)
       {
       res=ob.popvalue();
       
       if(res==0)
           System.out.println("Underflow occurs");
       
           System.out.println("Item deleted successfully");    
        System.out.println("Items after deletion:");
        ob.disp();
    }   
}
}


  /*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in    

/*
OUTPUT:

Eneter the capacity of array
4
Eneter the value to be inserted
1
Do you want to insert more item? Press 1 for Yes and 2 for No.
1
Enetr item to be pushed
3
Do you want to insert more item? Press 1 for Yes and 2 for No.
1
Enetr item to be pushed
5
Do you want to insert more item? Press 1 for Yes and 2 for No.
1
Enetr item to be pushed
7
Do you want to insert more item? Press 1 for Yes and 2 for No.
2
Items in stack are:
Elements are:
1 3 5 7 Total number of elements:4
Want to pop?Press 1 for Yes, 2 for No.
1
Item deleted successfully
Items after deletion:
Elements are:
3 5 7 Total number of elements:3
*/

Wednesday, September 11, 2013

Program of matrix multiplictaion

/**
 * class matrixMultiple finds the mutiplication of two matirces.
 * 
 * @author: Nitendra Verma
 * @version: Sept 11, 2013
 */

import java.util.Scanner;
public class matrixMultiple
{
  public static void main()
  {
  int row,col1,col2;
  Scanner in=new Scanner(System.in);
  System.out.println("Enetr number of rows");
  row=in.nextInt();
  System.out.println("Enetr number of columns of first matrix");
  col1=in.nextInt();
  System.out.println("Enetr number of columns of second matrix");
  col2=in.nextInt();
  int[][] first=new int[row][col1];
  int[][] second=new int[row][col2];
  int[][] third=new int[row][col1];
  System.out.println("Enetr "+row*col1 +" elements of first matrix");
  for(int i=0;i<row;i++)
  {
      for(int j=0;j<col1;j++)
      {
          first[i][j]=in.nextInt();
        }
    }
    
  System.out.println("Enetr "+row*col2 +"elements of second matrix");
  for(int i=0;i<row;i++)
  {
      for(int j=0;j<col2;j++)
      {
          second[i][j]=in.nextInt();
        }
   }
   

   int sum=0;

   for(int i=0;i<row;i++)
   {
       for(int j=0;j<col2;j++)
       {
           for(int k=0;k<col1;k++)
           {
               sum+=first[i][k]*second[k][j];
            }
            third[i][j]=sum;
            sum=0;
        }
    }
    
       System.out.println("Product of above matrices");
       for(int i=0;i<row;i++)
   {
       for(int j=0;j<col1;j++)
       {
           System.out.print(third[i][j]+"\t");
        }
        System.out.println();
}
}
  }

/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in    

Monday, September 9, 2013

Single Linked List - Deletion Operations

/**
 * Deletion operations on a single linked list
 * 
 * @author: Nitendra Verma
 * @version: Sept 9,2013
 */

class listDelTest 
{
  protected static linkedList lst;
  public static void main()
  {
      int num;
      lst=new linkedList();
      lst.insert(5);
      lst.insert(3);
      lst.insert(8);
      lst.insert(9);      
      lst.insert(2);
      lst.display();
    }
}


class Node
{
 protected int data;
 protected Node link;

 public Node()
 {
     data=0;
     link=null;
    }
    
 public Node(int d,Node n)
 {
     data=d;
     link=n;
    }
    
    
 public void setdata(int d)
 {
     data=d;
    }
    
 public void setlink(Node n)
 {
     link=n;
    }
    
 public int getdata()
 {
     return data;
    }
    
 public Node getlink()
 {
    return link;
    }  
}

class linkedList
{
 protected Node start;   //start denotes the first node of the list

 public linkedList()    //default constructor
 {
     start=null;
    }
    
  public void insert(int val)
  {
      Node nptr,ptr,save=null;  
      nptr=new Node(val,null);
    boolean ins=false;
      
      //Case1: list is empty
      if(start==null)
        start=nptr;
        
      //Case2: Insertion in the beginning
      else if(val<start.getdata())     //checking whether ITEM is to be inserted in the beginning 
      {
          save=start;                  //save START's value(save is also a reference)
          start=nptr;                  //assign nptr to START
          nptr.link=save;              //makes nptr points to previous START
        }
        
        
      //Case3:INsertion in middle
      else
      {
          save=start;
          ptr=start.getlink();
          while(ptr!=null)
          {
              if(nptr.data>ptr.data)
              {
                  save=ptr;
                  ptr=ptr.link;
                }
                else            //insert in between to nodes
                {
                    save.link=nptr;
                    nptr.link=ptr;
                    break;      //jump out of loop
                }
            }
            
     //Case4:Insertion at the end      
     if(ptr==null)     
       {
           save.link=nptr;
           nptr.link=null;
           //save.setlink(nptr);
        }   
    }
}


public boolean delete(int val)
{
 boolean res=false;
 if(start.getdata()==val)   //when first node is to be deleted
 {
     start=start.getlink();
     res=true;       //node deleted
    }
    else
    {
        Node ptr,save;
        save=start;
        ptr=start.getlink();
        
        while(ptr!=null)   
        {
            if(ptr.getdata()==val)
            {
                Node next=ptr.getlink();
                save.link=next;
                res=true;     //node deleted
                break;
            }
            else
            {
                save=ptr;
                ptr=ptr.getlink();
            }
        }
    }
    return res;
}

    
    public void display()
    {
        Node ptr=start;
        System.out.print(start.getdata()+"- ->");
        ptr=start.getlink();
        
        while(ptr.getlink()!=null)
        {
            System.out.print(ptr.getdata()+"- ->");
            ptr=ptr.getlink();
        }
       System.out.print(ptr.getdata()+"!!!");    //last node info printed
       System.out.println();
    }
}

/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in    

Single Linked List- Insertion operations

/**
 * Insertion operations on a single linked list
 * 
 * @author: Nitendra Verma
 * @version: Sept 8,2013
 */

class listTest 
{
  protected static linkedList lst;
  public static void main()
  {
      int num;
      lst=new linkedList();
      lst.insert(5);
      lst.insert(3);
      lst.insert(8);
      lst.insert(9);      
      lst.insert(2);
      lst.display();
    }
}


class Node
{
 protected int data;
 protected Node link;

 public Node()
 {
     data=0;
     link=null;
    }
    
 public Node(int d,Node n)
 {
     data=d;
     link=n;
    }
    
    
 public void setdata(int d)
 {
     data=d;
    }
    
 public void setlink(Node n)
 {
     link=n;
    }
    
 public int getdata()
 {
     return data;
    }
    
 public Node getlink()
 {
    return link;
    }  
}

class linkedList
{
 protected Node start;   //start denotes the first node of the list

 public linkedList()    //default constructor
 {
     start=null;
    }
    
  public void insert(int val)
  {
      Node nptr,ptr,save=null;  
      nptr=new Node(val,null);
    boolean ins=false;
      
      //Case1: list is empty
      if(start==null)
        start=nptr;
        
      //Case2: Insertion in the beginning
      else if(val<start.getdata())     //checking whether ITEM is to be inserted in the beginning 
      {
          save=start;                  //save START's value(save is also a reference)
          start=nptr;                  //assign nptr to START
          nptr.link=save;              //makes nptr points to previous START
        }
        
        
      //Case3:INsertion in middle
      else
      {
          save=start;
          ptr=start.getlink();
          while(ptr!=null)
          {
              if(nptr.data>ptr.data)
              {
                  save=ptr;
                  ptr=ptr.link;
                }
                else            //insert in between to nodes
                {
                    save.link=nptr;
                    nptr.link=ptr;
                    break;      //jump out of loop
                }
            }
            
     //Case4:Insertion at the end      
     if(ptr==null)     
       {
           save.link=nptr;
           nptr.link=null;
           //save.setlink(nptr);
        }   
    }
}

    
    public void display()
    {
        Node ptr=start;
        System.out.print(start.getdata()+"- ->");
        ptr=start.getlink();
        
        while(ptr.getlink()!=null)
        {
            System.out.print(ptr.getdata()+"- ->");
            ptr=ptr.getlink();
        }
       System.out.print(ptr.getdata()+"!!!");    //last node info printed
       System.out.println();
    }
}

/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in