Java Program to Implement List Operation | IT2305 - Java Programming Lab

AIM:
To write a Java program for the Implementation of the List Operation in IT2305 - Java Programming Laboratory.


SOURCE CODE:
/**
 @author sourcecodesonline.blogspot.com
 @version jdk 1.6.0
 */
import java.util.*;
import java.lang.*;
class Lis
{
  /** tis is my private */
  private int a[]=new int[50];
  /** tis is my private */
  private int n=0;
  /**
    objective:tis is car method used to give the first element in the array
    @param no para for tis method
    @return the first value in the array
   */
  public int car()
  {
    return a[0];
  }
  /**
    objective:tis is cdr method used to create a copy of array
    @param no para for tis method
    @return the copied array
   */
  public int[] cdr()
  {

    int t[]=Arrays.copyOfRange(a,1,n);
    return t;
  }
  /**
    objective:tis is cons method used to insert an element in the array
    @param x the x is the only para of tis method
    @return doesn't return anything
   */
 public void cons(int x)
 {
   if(n<100)
   {
     a[n]=x;
     n++;
   }
 }
 /** objective:tis is disp method to display the list
     @param no para for tis method
     @return doesn't return anything
  */
 public void disp()
 {
  System.out.println("List is:");
 for(int i=0;i<n;i++)
  System.out.println(a[i]);
 }
}

public class Lisp1
{
  public static void main(String args[])
 {
  Lis l=new Lis();
  int n,y,i,t,ch,f;
  Scanner sin=new Scanner(System.in);
  System.out.println("Enter no. of elements");
  f=sin.nextInt();
  System.out.println("Enter the elements:");
  for(i=0;i<f;i++)
  {

    n=sin.nextInt();
     l.cons(n);
   }
     l.disp();
  do
  {
   System.out.println("1.car\n2.cdr\n3.cons\n4.exit\nEnter ur choice");
  ch=sin.nextInt();
  switch(ch)
  {
   case 3:
    System.out.println("Enter the element to insert:");
    n=sin.nextInt();
     l.cons(n);
     l.disp();
  break;

 case 1:
    System.out.println("First element:"+l.car());
    break;
 case 2:
   System.out.println("Elements:"+Arrays.toString(l.cdr()));
   break;
 case 4:
   break;
 }
}while(ch!=4);
}
}
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: