Data structure--java Queue class

Source: Internet
Author: User

Defined

A queue is a special linear table that allows for deletion only on the front end of the table, and on the back end of the table.

The LinkedList class implements the queue interface, so we can use the LinkedList as a queue.

Legend

The queue itself is a first-in, first-out model (FIFO) that is very similar to the queuing model in our daily life. Depending on the implementation, they mainly have the array and the list of two implementation forms. Such as:

The diagram for the class associated with the queue is as follows:

Common methods

Serial number Method name Describe
1 Boolean Add (E E) Inserts the specified element into the queue.
2 Object Element () Retrieves the header of the queue.
Boolean offer (E e) Inserts the specified element into the queue (within the capacity of that queue).
3 Object Peek () Retrieves the header of the queue, and returns null if the team column is empty.
4 Object Poll () Retrieves and deletes the header of the queue, and returns null if the team column is empty.
5 Object Remove () Retrieves and deletes the header of the queue.

Source

1  PackageJava.util;2 3  Public InterfaceQueue<e>extendsCollection<e> {4     /**5 * Inserts the specified element into this queue if it's possible to doing so6 * immediately without violating capacity restrictions, returning7      * {@codetrue} upon success and throwing an {@codeIllegalStateException}8 * If no space is currently available.9      *Ten      * @parame The element to add One      * @return {@codetrue} (as specified by {@linkCollection#add}) A      * @throwsIllegalStateException If the element cannot is added at this - * Time due to capacity restrictions -      * @throwsClassCastException if the class of the specified element the * Prevents it from being added to the this queue -      * @throwsNullPointerException If the specified element is null and - * This queue does not permit null elements -      * @throwsIllegalArgumentException If some property of the This element + * Prevents it from being added to the this queue -      */ +     BooleanAdd (e e); A  at     /** - * Inserts the specified element into this queue if it's possible to do - * So immediately without violating capacity restrictions. - * When using a capacity-restricted queue, this method is generally - * Preferable to {@link#add}, which can fail to insert a element only - * by throwing an exception. in      * -      * @parame The element to add to      * @return {@codetrue} If the element is added to this queue, else +      *         {@codefalse} -      * @throwsClassCastException if the class of the specified element the * Prevents it from being added to the this queue *      * @throwsNullPointerException If the specified element is null and $ * This queue does not permit null elementsPanax Notoginseng      * @throwsIllegalArgumentException If some property of the This element - * Prevents it from being added to the this queue the      */ +     BooleanOffer (e e); A  the     /** + * Retrieves and removes the head of this queue. This method differs - * FROM {@link#poll Poll} only in so it throws an exception if this $ * Queue is empty. $      * -      * @returnThe head of this queue -      * @throwsNosuchelementexception If this queue is empty the      */ - E Remove ();Wuyi  the     /** - * Retrieves and removes the head of this queue, Wu * or returns {@codeNULL} If this queue is empty. -      * About      * @returnthe head of this queue, or {@codeNULL} If this queue is empty $      */ - E poll (); -  -     /** A * Retrieves, but does isn't remove, the head of this queue. This method + * differs from {@link#peek Peek} only in this it throws an exception the * If this queue is empty. -      * $      * @returnThe head of this queue the      * @throwsNosuchelementexception If this queue is empty the      */ the E Element (); the  -     /** in * Retrieves, but does isn't remove, the head of this queue, the * or returns {@codeNULL} If this queue is empty. the      * About      * @returnthe head of this queue, or {@codeNULL} If this queue is empty the      */ the E Peek (); the}

Reference: http://blog.csdn.net/u010617952/article/details/51726789

Data structure--java Queue class

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.