• 使用链表实现队列


    使用链表实现队列


      1 public class LinkedListQueue2<E> implements Queue<E> {
      2 
      3     private class Node {
      4         public E e;
      5         public Node next;
      6         
      7         public Node(E e, Node next) {
      8             this.e = e;
      9             this.next = next;
     10         }
     11         
     12         public Node(E e) {
     13             this(e, null);
     14         }
     15         
     16         public Node() {
     17             this(null, null);
     18         }
     19         
     20         @Override
     21         public String toString() {
     22             return e.toString();
     23         }
     24     }
     25 
     26     private Node dummyHead, tail;
     27     private int size;
     28     
     29     public LinkedListQueue2() {
     30         dummyHead = new Node();
     31         tail = dummyHead;
     32         size = 0;
     33     }
     34 
     35     @Override
     36     public int getSize() {
     37         return size;
     38     }
     39 
     40     @Override
     41     public boolean isEmpty() {
     42         return size == 0;
     43     }
     44 
     45     @Override
     46     public void enqueue(E e) {
     47 
     48         tail.next = new Node(e);
     49         tail = tail.next;
     50     
     51         size++;
     52     }
     53 
     54     @Override
     55     public E dequeue() {
     56         if(this.isEmpty()) {
     57             throw new IllegalArgumentException("Dequeue failed. Queue is empty.");
     58         }
     59         
     60         Node retNode = dummyHead.next;
     61         dummyHead.next = retNode.next;
     62         retNode.next = null;
     63         if(dummyHead.next == null) {
     64             tail = dummyHead;
     65         }
     66         size--;
     67         return retNode.e;
     68     }
     69 
     70     @Override
     71     public E getFront() {
     72         if(isEmpty()) {
     73             throw new IllegalArgumentException("Queue is empty.");
     74         }
     75         return dummyHead.next.e;
     76     }
     77 
     78     @Override
     79     public String toString() {
     80         StringBuilder res = new StringBuilder();
     81         res.append("Queue: front ");
     82         Node cur = dummyHead.next;
     83         while(cur != null) {
     84             res.append(cur + "->");
     85             cur = cur.next;
     86         }
     87         res.append("NULL tail");
     88         
     89         return res.toString();        
     90     }
     91     
     92     public static void main(String[] args) {
     93         LinkedListQueue2<Integer> queue = new LinkedListQueue2<>();
     94         for(int i=0; i<10; i++) {
     95             queue.enqueue(i);
     96             System.out.println(queue);
     97         
     98             if(i % 3 == 2) {
     99                 queue.dequeue();
    100                 System.out.println(queue);
    101             }
    102         }
    103         System.out.println(queue.getSize());
    104         for(int i=0; i<7; i++) {
    105             queue.dequeue();
    106             System.out.println(queue);
    107         }
    108     }
    109 }
  • 相关阅读:
    手把手教你使用markdown
    spring WebSocket详解
    springmvc请求参数异常处理
    蓝桥杯java 算法提高 摆花
    蓝桥杯java 算法提高 扶老奶奶过街
    蓝桥杯java 算法训练 未名湖边的烦恼
    蓝桥杯java 算法训练 Torry的困惑(基本型)
    java算法 硬币
    java算法 牌型种数
    java算法 方格填数
  • 原文地址:https://www.cnblogs.com/mingmingmomo/p/9288965.html
Copyright © 2020-2023  润新知