当前位置:   article > 正文

队列的底层设计以及时间复杂度的优化_队列搜索的复杂度

队列搜索的复杂度

1.基于数组的实现

队列的基本概念就是先进先出,但是如果采用数组来实现就会出现每次执行一次dequeue会把数组里面的每个元素都会移动一次,移动n次的时间复杂度就是O(n^2),我们想要优化它的dequeue操作,可以定义两个变量来记录它的栈顶和栈尾来进行入队和出队操作

  1. /**
  2. * Created by upupgogogo on 2018/4/26.下午6:02
  3. */
  4. public class LoopQueue<E> implements Queue<E> {
  5. private E[] data; //容器
  6. private int size; //队列长度
  7. private int front; //栈顶位置
  8. private int tail; //栈尾位置
  9. public LoopQueue(int capacity){
  10. data = (E[]) new Object[capacity];
  11. size = 0;
  12. front = 0;
  13. tail = 0;
  14. }
  15. public LoopQueue(){
  16. this(10);
  17. }
  18. @Override
  19. public int getSize(){
  20. return this.size;
  21. }
  22. @Override
  23. public boolean isEmpty(){
  24. return size == 0;
  25. }
  26. @Override
  27. public void enqueue(E e){
  28. if (size == getCapacity() ){
  29. resize(getCapacity() * 2); // 扩容
  30. }
  31. data[tail] = e;
  32. tail = (tail + 1) % data.length; //这个是一个循坏的队列
  33. size++;
  34. }
  35. @Override
  36. public E dequeue(){
  37. if(isEmpty())
  38. throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
  39. E ret = data[front];
  40. data[front] = null;
  41. front = (front + 1) % data.length;
  42. size --;
  43. if(size == getCapacity() / 4 && getCapacity() / 2 != 0)
  44. resize(getCapacity() / 2);
  45. return ret;
  46. }
  47. @Override
  48. public E getFront(){
  49. if(isEmpty())
  50. throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
  51. E ret = data[front];
  52. return ret;
  53. }
  54. public int getCapacity(){
  55. return data.length ;
  56. }
  57. private void resize(int capacity) {
  58. E[] newData = (E[]) new Object[capacity];
  59. for (int i = 0; i < size; i++)
  60. newData[i] = data[(i+front) % data.length];
  61. data = newData;
  62. front = 0;
  63. tail = size;
  64. }
  65. @Override
  66. public String toString(){
  67. StringBuilder res = new StringBuilder();
  68. res.append(String.format("Queue: size = %d , capacity = %d\n", size, getCapacity()));
  69. res.append("front [");
  70. for (int i = 0; i < size; i++){
  71. res.append(data[(i+front) % data.length]);
  72. if (i != size - 1)
  73. res.append(", ");
  74. }
  75. res.append("] tail");
  76. return res.toString();
  77. }

2.基于链表的实现

链表本身会有一个头指针,它进行dequeue时时间复杂度是O(1),但是进行enqueue的操作却是O(n)级别的,我们可以设置一个tail变量来记录尾结点,这样enqueue操作也是O(1)级别的

  1. /**
  2. * Created by upupgogogo on 2018/5/9.下午7:43
  3. */
  4. public class LinkedListQueue<E> {
  5. private class Node{
  6. public E e;
  7. public Node next;
  8. public Node(E e, Node next){
  9. this.e = e;
  10. this.next = next;
  11. }
  12. public Node(E e){
  13. this(e, null);
  14. }
  15. public Node(){
  16. this(null, null);
  17. }
  18. @Override
  19. public String toString(){
  20. return e.toString();
  21. }
  22. }
  23. private Node head,tail;
  24. private int size;
  25. public LinkedListQueue() {
  26. this.head = null;
  27. this.size = 0;
  28. this.tail = null;
  29. }
  30. public int getSize(){
  31. return this.size;
  32. }
  33. boolean isEmpty(){
  34. return size == 0;
  35. }
  36. public void enqueue(E e){
  37. if (isEmpty()){ //如果队列是空的,那么设置tail和head都指向new Node(e)
  38. tail = new Node(e);
  39. head = tail;
  40. }
  41. else {
  42. tail.next = new Node(e);
  43. tail = tail.next;
  44. }
  45. size ++;
  46. }
  47. public E dequeue(){
  48. if (isEmpty()){
  49. throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
  50. }
  51. Node resNode = head;
  52. head = head.next;
  53. resNode.next = null;
  54. size --;
  55. return resNode.e;
  56. }
  57. public E getFront(){
  58. if (isEmpty()){
  59. throw new IllegalArgumentException("Cannot getFront from an empty queue.");
  60. }
  61. return head.e;
  62. }
  63. public String toString(){
  64. StringBuilder res = new StringBuilder();
  65. res.append("Queue: front ");
  66. Node cur = head;
  67. while(cur != null) {
  68. res.append(cur + "->");
  69. cur = cur.next;
  70. }
  71. res.append("NULL tail");
  72. return res.toString();
  73. }
  74. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/960625
推荐阅读
相关标签
  

闽ICP备14008679号