add three node at specific position

add three node at specific position

 Scanner in=new Scanner(System.in);
  //insert 3 node at a position
 public void add3Node() {
   System.out.println("enter postion:");
   int position=in.nextInt();
if(position>0&&position<=size+1)
 {

   System.out.println("enter value of first node:");
   int n1d=in.nextInt();
   System.out.println("enter value of first node:");
   int n2d=in.nextInt();
   System.out.println("enter value of first node:");
   int n3d=in.nextInt();

   Node n3=new Node(n3d,null);
   Node n2=new Node(n2d,n3);
   Node n1=new Node(n1d,n2);
   
   if(position==1)
   {
    size++;
    size++;
    size++;
    if(size==0)
    {
     start=n1;
   
    }
    else
    {
    n3.setNext(start);
    start=n1;
    }
   }
   else if (position==size+1)
   {
    size++;
    size++;
    size++;
    Node t;
    t=start;
    while(t.getNext()!=null)
    {
    t=t.getNext(); 
    }
    t.setNext(n1);
   
   }
   else
   {
    size++;
    size++;
    size++;
    Node t;
    t=start;
    for(int i=1;i<position-1;i++)
    {
     t=t.getNext();
    }
    n3.setNext(t.getNext());
    t.setNext(n1);
    
    
   }
  
 }
else 
{
System.out.println("invalid position");  
}
   }//end insert three node at a position

Comments

Popular posts from this blog

A publishing company that markets both book and audio-cassette in java. (Solution)

Create a class named Movie that can be used with your video rental business in java

informed Search and Uninformed Search