Monday, January 12, 2015

Java elements from Linked List Program



How to add element at last position in Linked List

package com.java2novice.linkedlist;
import java.util.LinkedList;
public class MyAddLast
{
public static void main(String a[])
{
        LinkedList arrl = new LinkedList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println(arrl);
        System.out.println("Adding element at last position...");
        arrl.addLast("I am last");
        System.out.println(arrl);
        System.out.println("Adding element at last position...");
        arrl.offerLast("I am last - 1");
        System.out.println(arrl);
        System.out.println("Adding element at last position...");
        arrl.offer("I am last - 2");
        System.out.println(arrl);
    }
}

How to read first element from Linked List

package com.java2novice.linkedlist;
import java.util.LinkedList;
public class MyFirstElement
{
public static void main(String a[])
{
        LinkedList arrl = new LinkedList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println("First Element: "+arrl.element());
        System.out.println("First Element: "+arrl.getFirst());
        System.out.println("First Element: "+arrl.peek());
        System.out.println("First Element: "+arrl.peekFirst());
    }
}

How to read last element from Linked List

package com.java2novice.linkedlist;
import java.util.LinkedList;
public class MyLastElement
{
public static void main(String a[])
{
        LinkedList arrl = new LinkedList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println("Last Element: "+arrl.getLast());
        System.out.println("Last Element: "+arrl.peekLast());
    }
}

How to iterate through Linked List in reverse order

package com.java2novice.linkedlist;
import java.util.Iterator;
import java.util.LinkedList;
public class MyReverseIter
{
public static void main(String a[])
{
        LinkedList arrl = new LinkedList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        Iterator itr = arrl.descendingIterator();
        while(itr.hasNext())
       {
            System.out.println(itr.next());
        }
    }
}

Linked List push(), pop() operations

package com.java2novice.linkedlist;
import java.util.LinkedList;
public class MyPushPopOpr
{
 public static void main(String a[])
{
        LinkedList arrl = new LinkedList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println(arrl);
        arrl.push("push element");
        System.out.println("After push operation:");
        System.out.println(arrl);
        arrl.pop();
        System.out.println("After pop operation:");
        System.out.println(arrl);
    }
}

How to remove elements from Linked List

package com.java2novice.linkedlist;
import java.util.LinkedList;
public class MyAllRemoveOprs
{
public static void main(String a[])
{
        LinkedList arrl = new LinkedList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        arrl.add("four");
        arrl.add("five");
        arrl.add("six");
        arrl.add("seven");
        arrl.add("eight");
        arrl.add("nine");
        System.out.println(arrl);
        System.out.println("Remov() method:"+arrl.remove());
        System.out.println("After remove() method call:");
        System.out.println(arrl);
        System.out.println("remove(index) method:"+arrl.remove(2));
        System.out.println("After remove(index) method call:");
        System.out.println(arrl);
        System.out.println("Remov(object) method:"+arrl.remove("six"));
        System.out.println("After remove(object) method call:");
        System.out.println(arrl);
        System.out.println("removeFirst() method:"+arrl.removeFirst());
        System.out.println("After removeFirst() method call:");
        System.out.println(arrl);
        System.out.println("removeFirstOccurrence() method:"
          +arrl.removeFirstOccurrence("eight"));
        System.out.println("After removeFirstOccurrence() method call:");
        System.out.println(arrl);
        System.out.println("removeLast() method:"+arrl.removeLast());
        System.out.println("After removeLast() method call:");
        System.out.println(arrl);
        System.out.println("removeLastOccurrence() method:"
          +arrl.removeLastOccurrence("five"));
        System.out.println("After removeLastOccurrence() method call:");
        System.out.println(arrl);
    }
}

No comments:

Post a Comment