implementation of link list

implementation of link list

import java.util.Scanner;

class Node{
public int data;//data 
public Node next;//next
Node()//constructor
{
data=0;
next=null;
}

Node(int data,Node next)//constructor
{
 this.data=data;
 this.next=next;
}
//set data
public void setData(int val)
{
 data=val;
}
public Node setNext(Node n) {return next=n;}

public int getData() {int val=data;return val;}
public Node getNext() { return next;}
public String toString()
{
     return "data="+data;
}
}//end of node

//class link list
class LinkList{
 public Node start; //starting node for reference
 public int size;   //size of link list.
 LinkList(){start=null;size=0;}
 //empty
 public boolean isEmpty()
 {
  return start==null;
 }
 //size
 public int getSize() 
 {
  return size;
 }
 //3 view list
 public void viewList() {
 Node temp;
  if(isEmpty())
  {
   System.out.println("List is empty.");
  }
  else 
  {
   temp=start;
   for(int i=1;i<=size;i++) 
   {
    System.out.println(temp.getData());
   temp=temp.getNext();
       }
        }
                 }//end method view list
 
 //insert at first position.
 public void insertAtFirst(int val) 
 { 
  Node tt=new Node(val,start);
  start=tt;
  size++;
    }
 //insert at last
 public void insertAtLast(int val) {
  Node n,t;
  t=start;
  n=new Node(val,null);
  if(isEmpty())
  {
   start=n;
  }
  else
  {
   
    while(t.getNext()!=null)
    { 
     t=t.getNext();
    }
    t.setNext(n);
  }
  size++; 
 }
 
 //insert at position
   public void insertAtPosition(int val,int pos)
  {
  
   if(pos==1)
   {
    insertAtFirst(val); 
   }
   else if(pos==size+1)
   {
    insertAtLast(val);
   }
   else if(pos>1&&pos<=size)
   {
    Node t,n;
    n=new Node(val,null);
    t=start;
    for(int i=1;i<pos-1;i++)
    {
     t=t.getNext();
    }
    n.setNext(t.getNext());
    t.setNext(n);
    size++; 
   }
    else
   {
    System.out.println("Invalid position.");
   }
  }
  //delete first  
  public void deleteFirst()
  {
   if(isEmpty())
   {
    System.out.println("List is empty");
   }
   else
   {
    start=start.getNext();
    size--;
   }
  }
 //delete last
  public void deleteLast()
  {
   Node t;
   t=start;   
   for(int i=1;i<size-1;i++)
   {
    t=t.getNext();
   }
   t.setNext(null);
   size--;   
  }
 //delete at position. 
  public void deleteAtPosition(int pos)
  {
    if(pos==1)
      deleteFirst();
    else if(pos==size-1)
      deleteLast();
    else if(pos<1||pos>size)
    {
     Node t;
     t=start;
     for(int i=1;i<=pos;i++)
     {
      t=t.getNext();
     }
    }
    else
     System.out.println("Invalid input.");
  }
  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
  }
public class LinkListExample {

public static void main(String args[]) 
{
Scanner input = new Scanner(System.in);
LinkList ll=new LinkList();
boolean flag=true;
while(flag)
{
 System.out.println("1.enter to at specific position.");
 System.out.println("2.enter to add number at first position.");
 System.out.println("3.enter to add number at last position");
 System.out.println("4.enter to delete number at position.");
 System.out.println("5.enter to delete at first position.");
 System.out.println("6.enter to delete at last position.");
 System.out.println("7.view list.");
 System.out.println("8.Adding three node at specific position:");
 System.out.println("9.Exit.");
       int choice=input.nextInt();
 switch(choice)
 {
 case 1:
  System.out.println("enter value and  postion.");
  int value=input.nextInt();
  int position=input.nextInt();
  ll.insertAtPosition(value,position);
  break;
 case 2:
  System.out.println("Enter value to insert:");
  ll.insertAtFirst(input.nextInt());
  break;
 case 3:
  System.out.println("Enter value to insert:");
  ll.insertAtLast(input.nextInt());
  break;
 case 4:
  System.out.println("enter postion to delete.");
  int p=input.nextInt();
  ll.deleteAtPosition(p);
  break;
 case 5:
  ll.deleteFirst();
  break;
 case 6:
  ll.deleteLast();
  break;
 case 7:
  ll.viewList();
  break;
 case 9:
  flag=true;
  break;
 case 8:
  ll.add3Node();
  break;
 case 10:
  System.out.println(ll.getSize());
 }
}
}
}
output

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