队列 双端队列_双端队列接口

队列 双端队列

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-interface-heirarchy
以下是Deque接口的方法 (Below are the methods of Deque interface)
S.No.MethodDescription
1add(element)It is used for adding elements at the tail of the Deque.
2addFirst(element)It is used for adding elements at the head of the Deque.
3addLast(element)It is used for adding elements at the tail of the Deque.
4offer(element)It is used to add an element at the tail and returns a Boolean value if added successfully.
5offerFirst(element)It is used to addan element at the head and returns a Boolean value if added successfully.
6offerLast(element)It is used to addan element at the tail and returns a Boolean value if added successfully.
7iterator()It is used to iterate the deque.
8descendingIterator()It is used to iterate for the reverse order in the deque.
9push(element)It is used for adding elements at the head of the deque.
10pop(element)It is used for removing elements from the head of the deque.
11removeFirst()It is used for removing elements from the head of the deque.
12removeLast()It is used for removing elements from the tail of the deque
13poll()It is used to get and remove the first element from the deque. If the deque is empty then it returns null.
14pollFirst()It is used to get and remove the first element from the deque. If the deque is empty then it returns null.
15pollLast()It is used to get and remove the last element from the deque. If the deque is empty then it returns null.
16peek()It is used for removing the head of the deque.
17peekFirst()It is used for removing the head of the deque.
18peekLast()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); 
    } 
}
	
    
    
   
   
deque-example

翻译自: https://www.studytonight.com/java/deque-interface.php

队列 双端队列

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值