Push and Pop in LinkedList

—MyLinkedPushPop.java—

 
OUTPUT
LinkedList push pop

 
NOTE:
A list (which implements Deque) can be treated as either FILO (e.g. stack) or FIFO (e.g. queue), with separate sets of methods for each.
In either case, you remove from the front.

When treating it as a stack you use push, to add to the front. When treating it as a queue you use add to add to the end

you can see the discussion on http://stackoverflow.com/questions/6287840/why-linkedlist-removefirst-linkedlist-pop

LinkedList in Java

—MyLinkedList.java—

 
OUTPUT
LinkedList sample program

LinkedList in Java

LinkedList is not an array rather it is a doubly-linkedlist. Inserting and deleting the element is easy than ArrayList , so LinkedList has advantage over ArrayList. but for other operation ArrayList is faster than LinkedList , as LinkedList is a doubly-linkedlist so it have move overheads for updating pointers.

example1:

package collection.list.linkedlist;
import java.util.Iterator;
import java.util.LinkedList;
/**
 *
 * @author Abhishek
 */
public class linkedlist {
    public static void main(String args[])
    {
        LinkedList ll=new LinkedList();
        ll.add(“one”);
        ll.add(“two”);
        ll.add(“three”);
        ll.add(“four”);
        Iterator itr=ll.descendingIterator();
        /*
         * public Iterator descendingIterator()
Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head).
Returns:
an iterator over the elements in this deque in reverse sequence
Since:
1.6
         */
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}
example2:
package collection.list.linkedlist;
import java.util.Iterator;
import java.util.LinkedList;
/**
 *
 * @author Abhishek
 */
public class linkedlispushpop {
    public static void main(String args[])
    {
        LinkedList ll=new LinkedList();
        ll.add(“one”);
        ll.add(“two”);
        ll.add(“three”);
        ll.add(“four”);
        ll.pop();
        ll.push(“odd”);
        ll.push(“odd1”);
        Iterator itr=ll.iterator();
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}
example3:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package collection.list.linkedlist;
import java.util.Iterator;
import java.util.LinkedList;
/**
 *
 * @author Abhishek
 */
public class linkedlistpeek {
        public static void main(String args[])
    {
        LinkedList ll=new LinkedList();
        ll.add(“one”);
        ll.add(“two”);
        ll.add(“three”);
        ll.add(“four”);
        System.out.println(ll.peek());
        /*
         * public E peek()
Retrieves, but does not remove, the head (first element) of this list.
Returns:
the head of this list, or null if this list is empty
Since:
1.5
         */
        Iterator itr=ll.iterator();
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}
example4:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package collection.list.linkedlist;
import java.util.Iterator;
import java.util.LinkedList;
/**
 *
 * @author Abhishek
 */
public class linkedlistremove {
     public static void main(String args[])
    {
        LinkedList ll=new LinkedList();
        ll.add(“one”);
        ll.add(“two”);
        ll.add(“three”);
        ll.add(“four”);
        System.out.println(ll.removeFirstOccurrence(“one”));
        /*
         * public boolean removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element in this list (when traversing the list from head to tail). If the list does not contain the element, it is unchanged.
Parameters:
o – element to be removed from this list, if present
Returns:
true if the list contained the specified element
Since:
1.6
         */
        Iterator itr=ll.iterator();
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}

 


What to hire some one for software projects?
MVC, MySQL, Struts2 & JSP expert ready to complete your work on Freelancer.com
Click on the button

or
Hire Me at https://www.freelancer.com/users/3341136.html?ext=1&action=hireme