數據結構——java Queue類

定義    前端


隊列是一種特殊的線性表,它只容許在表的前端進行刪除操做,而在表的後端進行插入操做。java

LinkedList類實現了Queue接口,所以咱們能夠把LinkedList當成Queue來用後端

 

圖例            數組


 Queue自己是一種先入先出的模型(FIFO),和咱們平常生活中的排隊模型很相似。根據不一樣的實現,他們主要有數組和鏈表兩種實現形式。以下圖:this

 

 

 

 

與隊列相關的類的關係圖以下:spa

 

經常使用方法              .net


序號 方法名 描述
1 boolean add(E e) 將指定的元素插入到隊列中。
2 Object element() 檢索該隊列的頭。
  boolean offer(E e) 將指定元素插入到該隊列中(在該隊列容量以內)。
3 Object peek() 檢索該隊列的頭,若是該隊列爲空返回null。
4 Object poll() 檢索並刪除該隊列的頭,若是該隊列爲空返回null。
5 Object remove() 檢索並刪除該隊列的頭。

 

源碼rest


 

 1 package java.util;
 2 
 3 public interface Queue<E> extends Collection<E> {
 4     /**
 5      * Inserts the specified element into this queue if it is possible to do so
 6      * immediately without violating capacity restrictions, returning
 7      * {@code true} upon success and throwing an {@code IllegalStateException}
 8      * if no space is currently available.
 9      *
10      * @param e the element to add
11      * @return {@code true} (as specified by {@link Collection#add})
12      * @throws IllegalStateException if the element cannot be added at this
13      *         time due to capacity restrictions
14      * @throws ClassCastException if the class of the specified element
15      *         prevents it from being added to this queue
16      * @throws NullPointerException if the specified element is null and
17      *         this queue does not permit null elements
18      * @throws IllegalArgumentException if some property of this element
19      *         prevents it from being added to this queue
20      */
21     boolean add(E e);
22 
23     /**
24      * Inserts the specified element into this queue if it is possible to do
25      * so immediately without violating capacity restrictions.
26      * When using a capacity-restricted queue, this method is generally
27      * preferable to {@link #add}, which can fail to insert an element only
28      * by throwing an exception.
29      *
30      * @param e the element to add
31      * @return {@code true} if the element was added to this queue, else
32      *         {@code false}
33      * @throws ClassCastException if the class of the specified element
34      *         prevents it from being added to this queue
35      * @throws NullPointerException if the specified element is null and
36      *         this queue does not permit null elements
37      * @throws IllegalArgumentException if some property of this element
38      *         prevents it from being added to this queue
39      */
40     boolean offer(E e);
41 
42     /**
43      * Retrieves and removes the head of this queue.  This method differs
44      * from {@link #poll poll} only in that it throws an exception if this
45      * queue is empty.
46      *
47      * @return the head of this queue
48      * @throws NoSuchElementException if this queue is empty
49      */
50     E remove();
51 
52     /**
53      * Retrieves and removes the head of this queue,
54      * or returns {@code null} if this queue is empty.
55      *
56      * @return the head of this queue, or {@code null} if this queue is empty
57      */
58     E poll();
59 
60     /**
61      * Retrieves, but does not remove, the head of this queue.  This method
62      * differs from {@link #peek peek} only in that it throws an exception
63      * if this queue is empty.
64      *
65      * @return the head of this queue
66      * @throws NoSuchElementException if this queue is empty
67      */
68     E element();
69 
70     /**
71      * Retrieves, but does not remove, the head of this queue,
72      * or returns {@code null} if this queue is empty.
73      *
74      * @return the head of this queue, or {@code null} if this queue is empty
75      */
76     E peek();
77 }

 

 

參考:http://blog.csdn.net/u010617952/article/details/51726789code

相關文章
相關標籤/搜索