Android implements queue with LinkedList

queue

 A queue is a special kind of linear table that only allows delete operations at the front of the table and insert operations at the rear of the table. The end that performs the insert operation is called the tail of the queue, and the end that performs the delete operation is called the head of the queue. When there are no elements in the queue, it is called an empty queue. In the data structure of the queue, the first element inserted will be the first deleted element; otherwise, the last inserted element will be the last deleted element, so the queue is also called "first in, first out" (FIFO—first in first). out) of the linear table.

import java.util.LinkedList;
public class MyQueue
{
  private LinkedList list = new LinkedList();
  public void clear()//销毁队列
  {
      list.clear();
  }
  public boolean QueueEmpty()//判断队列是否为空
  {
      return list.isEmpty();
  }
  public void enQueue(Object o)//进队
  {
      list.addLast(o);
  }
  public Object deQueue()//出队
  {
      if(!list.isEmpty())
      {
          return list.removeFirst();
      }
      return "队列为空";
  }
  public int QueueLength()//获取队列长度
  {
      return list.size();
  }
  public Object QueuePeek()//查看队首元素
  {
      return list.getFirst();
  }
  public static void main(String[] args)//测试队列
  {
      MyQueue queue = new MyQueue();
      System.out.println(queue.QueueEmpty());
      queue.enQueue("a");
      queue.enQueue("b");
      queue.enQueue("c");
      queue.enQueue("d");
      queue.enQueue("e");
      queue.enQueue("f");
      System.out.println(queue.QueueLength());
      System.out.println(queue.deQueue());
      System.out.println(queue.deQueue());
      System.out.println(queue.QueuePeek());
      System.out.println(queue.deQueue());
      queue.clear();
      queue.enQueue("s");
      queue.enQueue("t");
      queue.enQueue("r");
      System.out.println(queue.deQueue());
      System.out.println(queue.QueueLength());
      System.out.println(queue.QueuePeek());
      System.out.println(queue.deQueue());
  }
}
 
 

The print result is as follows:

true
6
a
b
c
c
s
2
t
t

 

Reprinted in: Android uses LinkedList to implement queues - the power of stars - Blog Park 

Guess you like

Origin blog.csdn.net/weixin_42602900/article/details/123655610