Wednesday, March 11, 2015

Java use in Elements of Linked List program



Get elements of a LinkedList
 
import java.util.*;
public class Main 
{
   private LinkedList list = new LinkedList();
   public void push(Object v) 
  {
      list.addFirst(v);
   }
   public Object top() 
   {
      return list.getFirst();
   }
   public Object pop() 
   {
      return list.removeFirst();
   }
   public static void main(String[] args) 
   {
      Main stack = new Main();
      for (int i = 30; i < 40; i++)
         stack.push(new Integer(i));
      System.out.println(stack.top());
      System.out.println(stack.pop());
      System.out.println(stack.pop());
      System.out.println(stack.pop());
   }
}
 
Clear elements of Linked List
 
import java.util.*;
public class Main 
{
   public static void main(String[] args) 
     {
      LinkedList lList = new LinkedList();
      lList.add("one");
      lList.add("two");
      lList.add("three");
      lList.add("four");
      lList.add("five");
      System.out.println(lList);
      lList.subList(2, 4).clear();
      System.out.println(lList);
   }
}

No comments:

Post a Comment