INTRODUCTION OF LINKEDLIST

Introduction Of LinkedList


LinkedList:

  • LinkedList is one of the most important class in List interface.

  • If we have requirement for inserting elements frequently in list. Then it’s better to go to LinkedList instead of ArrayList.

  • LinkedList will give good performance incase of frequent insertion. Means for each insertion only two elements will get disturbed, remaining elements won’t disturb.

  • Except this feature LinkedList is same as “ArrayList”.

  • “LinkedList” contain same features like ”ArrayList”.

  • All methods inside “ArrayList” is same as “linkedList” also.

  • Tape drivers are example for LinkedList.

  • Drawback of LinkedList:

  • LinkedList will give less performance incase of Random access for retrieving data.

LinkedList Structure:

    it contains two parts in each objects. One is original element value and second is next element address. Like bellow

    Naming Convention
    LinkedList Structure

Example 1:

 
            
              package com.sdj.pack1;
              public class LinkList
              {
                public static void main(String[] args)
                {
                  List l1=new LinkedList();
                  l1.add(10);
                  l1.add(30);
                  l1.add(80);
                  l1.add(60);
                  l1.add(40);
                  l1.add(null);
                  l1.add("abc");
                  System.out.println(l1);
                  List l2=new LinkedList();
                  l2.add(45);
                  l2.add(33);
                  l2.add("ram");
                  l2.add(40);
                  l2.add("pqr");
                  l2.add(10);
                  System.out.println(l2);
                  System.out.println(l1.remove(l1));
                  System.out.println(l2.removeAll(l1));
                  System.out.println(l2);
                  l2.add(l1);
                  l2.addAll(l1);
                  System.out.println(l2);
                  System.out.println(l2.retainAll(l1));
                  System.out.println(l2);
                  System.out.println(l2.toArray());
                }
              } 
                  

Output:

     [10, 30, 80, 60, 40, null, abc]
     [45, 33, ram, 40, pqr, 10]
     false
     true
     [45, 33, ram, pqr]
     [45, 33, ram, pqr, [10, 30, 80, 60, 40, null, abc], 10, 30, 80, 60, 40, null, abc]
     true
     [10, 30, 80, 60, 40, null, abc]
     [Ljava.lang.Object;@10b62c9

Example 2:

 
            
              package com.sdj.pack1;
              public class LinkList
              {
                public static void main(String[] args)
                {
                  LinkedList list=new LinkedList();
                  list.add("abc");
                  list.add("def");
                  list.add("ghi");
                  list.add("klm");
                  list.add("nop");
                  list.add(null);
                  list.add("sdj");
                  System.out.println(list);
                  Object str[]=list.toArray(new String[5]);
                  System.out.println(str);
                }
              } 
                  

Output:

     [abc, def, ghi, klm, nop, null,sdj ]
     [Ljava.lang.String;@10b62c9]