Sunday, 11 May 2014

LinkedList : addLast : Insert Element at the End of Linked List

public void addFirst(E e)
Inserts the specified element at the end of this Linked List.

import java.util.*;

class LinkedListAddLast{
 public static void main(String args[]){
  LinkedList<Integer> myList = new LinkedList<Integer> ();
  
  System.out.println("Add 10 to the myList");
  myList.addLast(10);
  System.out.println("Elements in myList are\n" + myList);
  
  System.out.println("\nAdd 20 to the myList");
  myList.addLast(20);
  System.out.println("Elements in myList are\n" + myList);
  
  System.out.println("\nAdd 30 to the myList");
  myList.addLast(30);
  System.out.println("Elements in myList are\n" + myList);
  
  System.out.println("\nAdd 40 to the myList");
  myList.addLast(40);
  System.out.println("Elements in myList are\n" + myList);
 }
}

Output
Add 10 to the myList
Elements in myList are
[10]

Add 20 to the myList
Elements in myList are
[10, 20]

Add 30 to the myList
Elements in myList are
[10, 20, 30]

Add 40 to the myList
Elements in myList are
[10, 20, 30, 40]




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment