链表LinkedList实现队列(Java)

矫情吗;* 2023-07-01 08:42 200阅读 0赞

链表LinkedList实现队列

Queue.java(队列接口)

  1. public interface Queue<E> {
  2. void enqueue(E e);//入队
  3. E dequeue();//出队
  4. E getFront();//获取队首元素
  5. int getSize();//获取队列中元素个数
  6. boolean isEmpty();//判断队列是否为空
  7. }

LinkedListQueue.java(链表队列)
带尾节点的链表实现队列,没有设置虚拟头节点

  1. //链表头是队列头,出队O(1);链表尾是队列尾,入队O(1)
  2. public class LinkedListQueue<E> implements Queue<E> {
  3. private class Node {
  4. // 内部类将节点设为私有 隐藏实现细节
  5. public E e;// 元素
  6. public Node next;// next指针
  7. public Node(E e, Node next) {
  8. this.e = e;
  9. this.next = next;
  10. }
  11. public Node(E e) {
  12. this(e, null);
  13. }
  14. public Node() {
  15. this(null, null);
  16. }
  17. @Override
  18. public String toString() {
  19. return e.toString();
  20. }
  21. }
  22. private Node head, tail;// 定义头节点,尾节点
  23. private int size;
  24. public LinkedListQueue() {
  25. // TODO Auto-generated constructor stub
  26. head = null;
  27. tail = null;
  28. size = 0;
  29. }
  30. @Override
  31. public void enqueue(E e) {
  32. // TODO Auto-generated method stub
  33. if (tail == null) {
  34. // tail == null是尾节点为空,也就是队列为空的时候,否则tail应该指向尾节点
  35. tail = new Node(e);// 传入要插入的节点
  36. head = tail;
  37. } else {
  38. tail.next = new Node(e);// 如果tail不为空,在tail.next处插入节点
  39. tail = tail.next;// tail后移
  40. }
  41. size++;
  42. }
  43. @Override
  44. public E dequeue() {
  45. // TODO Auto-generated method stub
  46. if (isEmpty())
  47. try {
  48. throw new Exception("队列为空");
  49. } catch (Exception e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. Node retNode = head;// 出队的节点就是head所指向的节点
  54. head = head.next;// head后移
  55. retNode.next = null;
  56. if (head == null)// 如果队列中只有一个元素
  57. tail = null;
  58. size--;
  59. return retNode.e;
  60. }
  61. @Override
  62. public E getFront() {
  63. // TODO Auto-generated method stub
  64. if (isEmpty())
  65. try {
  66. throw new Exception("队列为空");
  67. } catch (Exception e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. }
  71. return head.e;
  72. }
  73. @Override
  74. public int getSize() {
  75. // TODO Auto-generated method stub
  76. return size;
  77. }
  78. @Override
  79. public boolean isEmpty() {
  80. // TODO Auto-generated method stub
  81. return size == 0;
  82. }
  83. public String toString() {
  84. StringBuilder res = new StringBuilder();
  85. res.append("Queue:front ");
  86. for (Node cur = head; cur != null; cur = cur.next)
  87. res.append(cur + "->");
  88. res.append("NULL tail");
  89. return res.toString();
  90. }
  91. }

测试(main)

  1. public class LinkedListQueueTest {
  2. public static void main(String[] args) {
  3. LinkedListQueue<Integer> loopqueue = new LinkedListQueue<>();
  4. for (int i = 0; i < 5; i++) {
  5. loopqueue.enqueue(i);
  6. System.out.println(loopqueue);
  7. }
  8. loopqueue.dequeue();
  9. System.out.println(loopqueue);
  10. }
  11. }

测试结果在这里插入图片描述

链表实现的队列的时间复杂度

入队:O(1)
出队:O(1)
查询队首元素:O(1)

实现队列的链表创建了尾节点tail,使得在链表尾部添加元素变得容易,复杂度为O(1);但是在尾部删除元素需要遍历链表找到尾部元素的前一个节点,还是需要O(n)的复杂度,是不推荐的;所以使用链表头做队首,使用链表尾做队尾。

发表评论

表情:
评论列表 (有 0 条评论,200人围观)

还没有评论,来说两句吧...

相关阅读

    相关 LinkedList(Java)

    链表LinkedList 定义 链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元