1. /**
    2. * @author luobo.cs@raycloud.com
    3. * @since 2021/7/25 10:47 上午
    4. */
    5. public class LinkedListQueue<E> {
    6. private int size = 0;
    7. private Node<E> head;
    8. private Node<E> tail;
    9. public LinkedListQueue() {
    10. head = new Node<>();
    11. tail = new Node<>();
    12. head.next = tail;
    13. head.prev = tail;
    14. tail.next = head;
    15. tail.prev = head;
    16. }
    17. public int size() {
    18. return size;
    19. }
    20. public void push(E e) {
    21. final Node<E> temp = new Node<>();
    22. //1、获取尾节点的前一个节点
    23. final Node<E> prev = tail.prev;
    24. //2、修改尾部指向
    25. prev.next = temp;
    26. tail.prev = temp;
    27. //3、修改当前节点指向
    28. temp.next = tail;
    29. temp.prev = prev;
    30. temp.data = e;
    31. size++;
    32. }
    33. public E poll() {
    34. final Node<E> next = head.next;
    35. if (next.equals(tail)) {
    36. return null;
    37. }
    38. final E data = next.data;
    39. head = next;
    40. size--;
    41. return data;
    42. }
    43. private static class Node<E> {
    44. public Node<E> prev;
    45. public Node<E> next;
    46. private E data;
    47. }
    48. public static void main(String[] args) {
    49. LinkedListQueue<String> queue = new LinkedListQueue<>();
    50. queue.push("111");
    51. queue.push("222");
    52. queue.push("333");
    53. queue.push("444");
    54. System.out.println(queue.size);
    55. System.out.println(queue.poll());
    56. System.out.println(queue.poll());
    57. System.out.println(queue.poll());
    58. System.out.println(queue.poll());
    59. System.out.println(queue.poll());
    60. System.out.println(queue.size);
    61. }
    62. }