Unique methods of LinkedList:
(a) How to add
ADDFISRT (e E): Inserts the specified element at the beginning of this list. The argument e can be interpreted as an object, because the list can receive objects of any type, so E is the object (the transfer process is transformed upward).
AddLast (e E): Inserts the specified element at the end of this list.
After JDK1.6:
Offerfirst ();
Offerlast ();//In fact, the use of the same before and after changing a name.
(ii): Gets the element method (the fetch procedure does not delete the linked list element):
GetFirst (); Returns the first element of this list. If the linked list is empty, the nosuchelementexception exception is thrown.
GetLast (); Returns the last element of this list.
After JDK1.6:
Peekfirst () Gets the first element of the list and returns null if the list is empty.
Peeklast ();
(iii): Gets the element method (gets the procedure to delete the linked list element):
Removefirst (); Gets the first element of the list and deletes the element in the linked list, and throws a Nosuchelementexception exception if the list is empty.
Removelast ();
After JDK1.6:
Pollfirst (); Gets the first element of the list and deletes the element in the linked list, and returns null if the list is empty.
Polllast ();
Specific code implementation:
1 package collection; 2/* 3 * Use LinkedList to simulate the queue this data structure 4 */5 import java.util.LinkedList; 6 7 class queue{//creates a container that provides external objects that can use the container ( Call Container encapsulation method) 8 private LinkedList link; 9 public Queue () { link=new linkedlist (); one }12 public void Myadd (Object obj) { link.offerlast ((obj));}15 public Object MyGet () { Link.pollfirst (); }18 public boolean isNull () { return link.isempty (); }21}22 public class linkedlisttest { /**25 * @param args26 */27 public static void Main (string[] args) { Queue Dl=new Queue (), dl.myadd ("abc0"), Dl.myadd ("ABC1"), dl.myadd ("ABC2") , Dl.myadd ("ABC3"); !dl.isnull ()) { System.out.println (Dl.myget ()); }37}38 39}
Summarize the above code: provide the Container object, the container encapsulation method can realize the queue FIFO (FIFO) demand, adjust the method of encapsulation, also can implement the stack advanced after this kind of data structure (FILO).
Packagecollection;/** Using LinkedList to simulate the data structure of a queue*/Importjava.util.LinkedList;classqueue{//Create a container to provide external objects that can use the container (the method encapsulated in the call container) PrivateLinkedList link; PublicQueue () {link=NewLinkedList (); } Public voidmyadd (Object obj) {link.offerlast ((obj)); } PublicObject MyGet () {returnLink.pollfirst (); } Public BooleanIsNull () {returnLink.isempty (); }} Public classLinkedlisttest {/** * @paramargs*/ Public Static voidMain (string[] args) {Queue DL=NewQueue (); Dl.myadd ("Abc0"); Dl.myadd ("ABC1"); Dl.myadd ("ABC2"); Dl.myadd ("ABC3"); while(!Dl.isnull ()) {System.out.println (Dl.myget ()); } }}
LinkedList of the Java Collection framework-----simulating queues and stacks with LinkedList