[Java] Linkedlist/queue-Source Code Learning notes

Source: Internet
Author: User

Simply draw the inheritance of the next linkedlist, such as. Just drawing the part of attention, not the complete diagram. This blog is about Queue, Deque, LinkedList's source code reading notes. For a List interface note, refer to the previous blog post list/arraylist-source code Learning notes

Queue

1. Inherit the Collection interface and provide additional methods for inserting, extracting, and viewing elements. The new method has two forms: when the operation fails, an exception is thrown or a special value is returned. The special value can be null or FALSE, depending on the method itself.

   Throws exception Returns Special Value
Insert Add (E) Offer (e)
Remove Remove () Poll ()
Examine Element () Peek ()

Use Offer (e) Insert failure returns false, which is typically used in Queue implementations with a size limit.

Remove (), poll () deletes the first element of the queue and returns the element that was deleted.

Element (), peek () returns the first element of a queue, but does not delete it.

The table lists all the methods that are defined by the Queue.

2. The typical Queue is sorted by FIFO (first-in-first-out), but this is not required. For example, the priority queue Priorityqueue is sorted by the comparer, or by the natural order of the elements. The other data structure Stack is sorted by FILO (first-in-last-out).

3. There is no way to define a blocking queue (blocking queues). These methods typically have concurrent programming and are implemented by Java.util.concurrent.BlockingQueue.

4. Insert a null element should be forbidden because the poll (), Peek () method uses NULL to indicate that the current queue is empty.

Deque

1. The Deque represents a double Ended queue, which is a dual-ended queueing. Both ends allow inserting and deleting elements.

2. Provides additional methods for inserting, extracting, and viewing elements. The new method has two forms: when the operation fails, an exception is thrown or a special value is returned. This is similar to the Queue.

First Element (Head) Last Element (Tail)
Throws exception Special value Throws exception Special value
Insert AddFirst (e) Offerfirst (e) AddLast (e) Offerlast (e)
Remove Removefirst () Pollfirst () Removelast () Polllast ()
Examine GetFirst () Peekfirst () GetLast () Peeklast ()

As you can see from the table, the Deque method name ends with first or last, providing a function similar to the Queue,

3. Because the dual-ended operation element is allowed, all Deque can be used as a Queue, FIFO (first-in-first-out), or as Statck to use FILO (first-in-last-out). Although Deque provides more features, it is not as common as Queue in practical applications.

4. Whether Deque is used as a Queue or Stack, the Peek () method is used normally because it always returns the first element of the Deque.

5. Inserting a null element should be prohibited. This is similar to the Queue.

LinkedList

1. It can be simplified to understand a collection of two interfaces for implementing the List, Queue.

2. The underlying data structure is a two-way list. the method of searching for elements by subscript node (int index), first determine which side is closer to the head and tail, and then start looking from the nearer end.

3. can contain null elements.

4. The iterator uses the Fail-fast design idea. This is similar to the ArrayList.

5. The element is in node nodes, where node is the inner class. When you look at implementing code, you can use paint to help understand that the code implemented by the method is somewhat similar to the practice on Leetcode.

6. There are many methods of LinkedList, there are 24 methods for manipulating elements, of which 6 are specific operations implemented and modified to Non-public, and 18 are external interfaces of public. All of this has so many external interfaces because it declares the interfaces of the Deque and Queue, and implements them to define all the methods. The 6 specific implementations were:

Link Method Unlinke Method
Linkfirst (E) Unlinkfirst (node<e>)
Linklast (E) Unlinklast (node<e>)
Linkbefore (E, node<e>) Unlink (node<e>)

Personally, this design makes the LinkedList function more and more miscellaneous.

7. IndexOf (object) and the Remove (object) method, when looking up an object in the list, first determine whether the object is null, and then use the appropriate comparison method. Take remove (Object) as an example, the code is as follows

     Public BooleanRemove (Object o) {if(O = =NULL) {             for(node<e> x = first; X! =NULL; x =x.next) {if(X.item = =NULL) {unlink (x); return true; }            }        } Else {             for(node<e> x = first; X! =NULL; x =x.next) {if(O.equals (X.item)) {unlink (x); return true; }            }        }        return false; }

8. AddAll (int index, COLLECTION<? extends e> c) method, the general idea is as follows

A. Obtaining an array form of the element to be inserted by C.toarray ()

B. Start with the specified position index and insert the element you want to insert sequentially

C. Point the index's original next element to the newly inserted last element.

9. Clear () method to remove all connections in the middle of the list to help the garbage collector reclaim memory.

ToArray (): Object and ToArray (t[]): The t[] method copies all the elements in the list to the array, and then returns the array. If you know the type of the List element, it is recommended to use ToArray (t[]): t[], which reduces the overhead of memory allocation.

JDK version: JDK1.8.0_31.JDK

[Java] Linkedlist/queue-Source Code Learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.