adsense


Showing posts with label JAVA EXAMPLES GET ELEMENTS OF A LINKED LIST. Show all posts
Showing posts with label JAVA EXAMPLES GET ELEMENTS OF A LINKED LIST. Show all posts

Friday, 30 October 2015

JAVA EXAMPLES GET ELEMENTS OF A LINKED LIST PAGE 152

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES GET ELEMENTS OF A LINKED LIST







Java Examples - Get elements of a Linked List



Problem Description:
How to get elements of a LinkedList?
Solution:
Following example demonstrates how to get elements of LinkedList using top() & pop() methods.
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());
   }
}
Result:
The above code sample will produce the following result.
39
39
38
37