队列 双端队列
In Java, the Deque interface is under java.util.Deque and it is a subtype of java.util.Queue interface. A Deque is a double-ended queue that means addition and deletion of the elements can be done from both the ends. The Deque can be used as a Queue or as Stack i.e First-in-first-out or Last-in-last-out.
在Java中,Deque接口位于java.util.Deque下,它是java.util.Queue接口的子类型。 Deque是一个双端队列,这意味着可以从两端进行元素的添加和删除。 双端队列可以用作队列或堆栈,即先进先出或后进 先 出。
以下是Deque接口的层次结构 (Below is the hierarchy of Deque interface)
以下是Deque接口的方法 (Below are the methods of Deque interface)
S.No. | Method | Description |
---|---|---|
1 | add(element) | It is used for adding elements at the tail of the Deque. |
2 | addFirst(element) | It is used for adding elements at the head of the Deque. |
3 | addLast(element) | It is used for adding elements at the tail of the Deque. |
4 | offer(element) | It is used to add an element at the tail and returns a Boolean value if added successfully. |
5 | offerFirst(element) | It is used to addan element at the head and returns a Boolean value if added successfully. |
6 | offerLast(element) | It is used to addan element at the tail and returns a Boolean value if added successfully. |
7 | iterator() | It is used to iterate the deque. |
8 | descendingIterator() | It is used to iterate for the reverse order in the deque. |
9 | push(element) | It is used for adding elements at the head of the deque. |
10 | pop(element) | It is used for removing elements from the head of the deque. |
11 | removeFirst() | It is used for removing elements from the head of the deque. |
12 | removeLast() | It is used for removing elements from the tail of the deque |
13 | poll() | It is used to get and remove the first element from the deque. If the deque is empty then it returns null. |
14 | pollFirst() | It is used to get and remove the first element from the deque. If the deque is empty then it returns null. |
15 | pollLast() | It is used to get and remove the last element from the deque. If the deque is empty then it returns null. |
16 | peek() | It is used for removing the head of the deque. |
17 | peekFirst() | It is used for removing the head of the deque. |
18 | peekLast() | It is used for removing the tail of the deque. |
序号 | 方法 | 描述 |
---|---|---|
1个 | 加(元素) | 它用于在双端队列的末尾添加元素。 |
2 | addFirst(element) | 它用于在双端队列的顶部添加元素。 |
3 | addLast(element) | 它用于在双端队列的末尾添加元素。 |
4 | 报价(元素) | 它用于在尾部添加元素,如果添加成功,则返回布尔值。 |
5 | offerFirst(element) | 它用于在头部添加元素,如果添加成功,则返回布尔值。 |
6 | offerLast(element) | 它用于在尾部添加元素,如果添加成功,则返回布尔值。 |
7 | iterator() | 它用于迭代双端队列。 |
8 | DescendIterator() | 它用于迭代双端队列中的相反顺序。 |
9 | 推(元素) | 它用于在双端队列的顶部添加元素。 |
10 | 流行(元素) | 它用于从双端队列的头部删除元素。 |
11 | removeFirst() | 它用于从双端队列的头部删除元素。 |
12 | removeLast() | 用于从双端队列的尾部删除元素 |
13 | 轮询() | 它用于获取和删除双端队列的第一个元素。 如果双端队列为空,则返回null。 |
14 | pollFirst() | 它用于获取和删除双端队列的第一个元素。 如果双端队列为空,则返回null。 |
15 | pollLast() | 它用于获取和删除双端队列的最后一个元素。 如果双端队列为空,则返回null。 |
16 | 窥视() | 用于去除双端队列的头部。 |
17 | peekFirst() | 用于去除双端队列的头部。 |
18 | peekLast() | 用于去除双端队列的尾巴。 |
Example:
例:
import java.util.*;
public class DequeDemo1
{
public static void main(String[] args)
{
Deque
obj = new LinkedList
();
obj.add("A 1 (Tail)");
obj.addFirst("B 2 (Head)");
obj.addLast("C 3 (Tail)");
obj.push("D 4 (Head)");
obj.offer("E 5 (Tail)");
obj.offerFirst("F 6 (Head)");
obj.offerLast("G 7 (Tail)");
System.out.println("*****************************************************************************");
System.out.println(obj + "\n");
System.out.println("*****************************************************************************");
System.out.println("Standard Iterator");
Iterator iterator = obj.iterator();
while (iterator.hasNext())
System.out.println("\t" + iterator.next());
Iterator reverse = obj.descendingIterator();
System.out.println("*****************************************************************************");
System.out.println("Reverse Iterator");
while (reverse.hasNext())
System.out.println("\t" + reverse.next());
System.out.println("*****************************************************************************");
System.out.println("Peek " + obj.peek());
System.out.println("*****************************************************************************");
System.out.println("After peek: " + obj);
System.out.println("*****************************************************************************");
System.out.println("Pop " + obj.pop());
System.out.println("*****************************************************************************");
System.out.println("After pop: " + obj);
System.out.println("*****************************************************************************");
System.out.println("Contains element 3: " + obj.contains("Element 3 (Tail)"));
obj.removeFirst();
obj.removeLast();
System.out.println("*****************************************************************************");
System.out.println("Deque after removing " + "first and last: " + obj);
}
}
队列 双端队列