集册 Java实例教程 通过LinkedList创建双端队列

通过LinkedList创建双端队列

欢马劈雪     最近更新时间:2020-01-02 10:19:05

646
通过LinkedList创建Deque

import java.util.Iterator;

import java.util.NoSuchElementException;

import java.util.LinkedList;
/**
 from
* N o  w  J a v a . c o m - 时  代  Java 
**/



public class Deque<Item> implements Iterable<Item>

{

  

  private int sizeN;

  private LinkedList<Item> linkedList;

  

  // construct an empty deque

  public Deque()

  {   

    linkedList = new LinkedList<Item>();      

  }/*时   代     Java  公  众  号 - nowjava.com 提 供*/

  

  // is the deque empty?

  public boolean isEmpty()

  {

    return linkedList.isEmpty();

  }

  

  // return the number of items on the deque

  public int size()

  {

    return linkedList.size();

  }

  

  // insert the item at the front

  public void addFirst(Item item)

  {

    linkedList.addFirst(item);

  }

  

  // insert the item at the end

  public void addLast(Item item)

  {

    linkedList.addLast(item);

  }

  

  // delete and return the item at the front

  public Item removeFirst()

  {   

    return linkedList.pollFirst();

  }

  

  // delete and return the item at the end

  public Item removeLast()   

  {

    return linkedList.pollLast();

  }

  

  
展开阅读全文