Java-队列实现

队列有多种实现的方式,有数组队列还有循环队列

  • 数组队列出列的时间复杂度是O(n)

数组队列

1
2
3
4
5
6
7
8
9
10
public interface Queue<E> {
int getSize();
int getCapacity();
boolean isEmpty();
// 队列增加一个值
void enqueue(E e);
// 队列出列
E dequeue();
E getFront();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package dataStructure;

public class ArrayQueue<E> implements Queue<E> {
private Array<E> array;

public ArrayQueue(int capacity){
array = new Array<>(capacity);
}

public ArrayQueue(){
array = new Array<>();
}

@Override
public int getSize() {
return array.getSize();
}

@Override
public int getCapacity() {
return array.getCapacity();
}

@Override
public boolean isEmpty() {
return array.isEmpty();
}

@Override
public void enqueue(E e) {
array.addLast(e);
}

@Override
public E dequeue() {
return array.removeFirst();
}

@Override
public E getFront() {
return array.getFirst();
}

@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("ArrayQueue:");
res.append("front [");
for (int i = 0; i < array.getSize() ; i++) {
res.append(array.get(i));
if (i != array.getSize() -1){
res.append(", ");
}
}
res.append("] tail");
return res.toString();
}

}

循环队列

  • 接口也是继承的Queue接口
  • 若创建一个容量为10的队列,需要创建容量为11的数组,
    • 因为这是为了保证 front == tail时,队列为空
    • front == tail + 1时,队列为满
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package dataStructure;

public class LoopQueue<E> implements Queue<E> {

private E data[];
private int front;
private int tail;
private int size;

public LoopQueue(int capacity) {
data = (E[]) new Object[capacity + 1];
front = 0;
tail = 0;
size = 0;
}

public LoopQueue() {
this(10);
}

@Override
public int getCapacity() {
return data.length - 1;
}

@Override
public boolean isEmpty() {
return front == tail;
}

@Override
public int getSize() {
return size;
}

@Override
public void enqueue(E e) {
if ((tail + 1) % data.length == front) {
resize(getCapacity() * 2);
}
data[tail] = e;
tail = (tail + 1) % data.length;
size++;
}

@Override
public E dequeue() {
if (isEmpty())
throw new IllegalArgumentException("cannot dequeue from an empty LoopQueue");
E ret = data[front];
data[front] = null;
front = (front + 1) % data.length;
size--;
if (size == getCapacity() / 4 && getCapacity() != 0) {
resize(getCapacity() / 2);
}
return ret;
}

@Override
public E getFront() {
if (isEmpty())
throw new IllegalArgumentException("cannot getFront from an empty LoopQueue");
return data[front];
}

private void resize(int newCapacity) {
E[] newData = (E[]) new Object[newCapacity + 1];
for (int i = 0; i < size; i++) {
newData[i] = data[(i + front) % data.length];
}
data = newData;
front = 0;
tail = size;
}

@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Queue: size = %d ,capacity = %d\n", size, getCapacity()));
res.append("front [");
for (int i = front; i != tail; i = (i + 1) % data.length) {
res.append(data[i]);
if ((i + 1) % data.length != tail) {
res.append(", ");
}
}
res.append("] tail");
return res.toString();
}

public static void main(String[] args) {
LoopQueue<Integer> queue = new LoopQueue<>();
queue.enqueue(10);
System.out.println(queue);
}
}

两种队列的比较

  • 下面是两种队列时间复杂度的对比

链表队列

  • 我们需要在链表中加个尾指针,这样的话 操作的时间复杂度会降低
  • 尾端的 删除元素 时间复杂度是O(n),添加元素 时间复杂度是O(1)
  • 首端 删除、添加 的时间复杂度都是O(1)
  • 所以我们选择链表的尾端添加元素首端删除元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package dataStructure;

public class LinkedListQueue<E> implements Queue<E> {

public class Node {
public E e;
public Node next;

public Node(E e, Node next) {
this.e = e;
this.next = next;
}

public Node(E e) {
this(e, null);
}

public Node() {
this(null, null);
}

public String toString() {
return e.toString();
}
}

private Node head, tail;
private int size;

public LinkedListQueue() {
head = null;
tail = null;
size = 0;
}

@Override
public int getSize() {
return size;
}

@Override
public int getCapacity() {
return size;
}


@Override
public boolean isEmpty() {
return size == 0;
}

@Override
public void enqueue(E e) {
if (tail == null) {
tail = new Node(e);
head = tail;
} else {
tail.next = new Node(e);
tail = tail.next;
}
size++;
}

@Override
public E dequeue() {
if (isEmpty()) {
throw new IllegalArgumentException("Cannot dequeue from an empty queue");
}
Node retNode = head;
head = head.next;
retNode.next = null;
if (head == null) {
tail = null;
}
size--;
return retNode.e;
}

@Override
public E getFront() {
if (isEmpty())
throw new IllegalArgumentException("Cannot getFront from an empty queue");
return head.e;
}


@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Queue : front ");
Node curr = head;
while (curr != null) {
res.append(curr + "->");
curr = curr.next;
}
res.append("Null tail");
return res.toString();
}

public static void main(String[] args) {
LinkedListQueue<Integer> queue = new LinkedListQueue();
for (int i = 0; i < 10; i++) {
queue.enqueue(i);
}
System.out.println(queue);
}
}