0%

structure-queue

队列操作

  • 入队:enqueue
  • 出队:dequeue
  • 查询:peek
  • 大小:size
  • 判空:isEmpty
  • 判满:isFull

数组队列

数组队列假溢出的接解决办法

  • 使用移位操作
    • rear等于capacity时,将所有元素整体移到左端
  • 使用循环数组
    • 使用循环数组并且浪费一个空间来判断满
    • 使用循环数组并且添加flag标记来辅助判断head等于rear时是空还是满
    • 使用循环数组并且添加size大小来辅助判断head等于rear时是空还是满

普通数组队列(normal queue)

简单数组队列

简单数组队列存在假溢出的问题

  • 判空:front == rear
  • 判满:rear == capacity
  • 入队指针变化:rear++;
  • 出队指针变化:front++;
MyArraySimpleQueue.javaview raw
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
package dsa.structure.ds103_queue;

import java.util.Arrays;

public class MyArraySimpleQueue {
private int capacity;
private int front = 0;
private int rear = 0;
private String[] table;

public MyArraySimpleQueue(int capacity) {
this.capacity = capacity;
this.table = new String[capacity];
}

public boolean isEmpty() {
return front == rear;
}

public boolean isFull() {
return rear == capacity;
}

public boolean enqueue(String s) {
if (isFull()) {
return false;
}

table[rear] = s;
rear++;

return true;
}

public String dequeue() {
if (isEmpty()) {
return null;
}

String v = table[front];
table[front] = null;
front++;

return v;
}

// for test print
public String toString() {
return Arrays.toString(table);
}

public static void main(String[] args) {
int capacity = 7;
MyArraySimpleQueue queue = new MyArraySimpleQueue(capacity);

// test for enqueue
System.out.println("\n---test enqueue---\n");
for (int i = 0; i < capacity + 1; i++) {
String s = "" + i;

boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}

// test for dequeue and enqueue
System.out.println("\n---test dequeue and enqueue---\n");
for (int i = 0; i < capacity + 2; i++) {
String s = "" + (i + capacity);

if (i < capacity / 2) {
s = queue.dequeue();
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}
}

// test for dequeue
System.out.println("\n---test dequeue---\n");
while (true) {
String s = queue.dequeue();
if (s != null) {
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is empty");
break;
}
}
}
}

复杂数组队列

复杂数组队列使用移位(transfer)来解决假溢出的问题,并且判满条件还需要判断front指针

  • 判空:front == rear
  • 判满:rear == capacity && front == 0
  • 入队指针变化:rear++;
  • 出队指针变化:front++;
MyArrayComplexQueue.javaview raw
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
108
109
110
111
112
package dsa.structure.ds103_queue;

import java.util.Arrays;

public class MyArrayComplexQueue {
private int capacity;
private int front = 0;
private int rear = 0;
private String[] table;

public MyArrayComplexQueue(int capacity) {
this.capacity = capacity;
this.table = new String[capacity];
}

public boolean isEmpty() {
return front == rear;
}

public boolean isFull() {
return rear == capacity && front == 0;
}

public boolean enqueue(String s) {
if (isFull()) {
return false;
}

// fake full
if (rear == capacity) {
for (int i = front; i < rear; i++) {
table[i - front] = table[i];
table[i] = null;
}

rear = rear - front;
front = 0;
}

table[rear] = s;
rear++;

return true;
}

public String dequeue() {
if (isEmpty()) {
return null;
}

String v = table[front];
table[front] = null;
front++;

return v;
}

// for test print
public String toString() {
return Arrays.toString(table);
}

public static void main(String[] args) {
int capacity = 7;
MyArrayComplexQueue queue = new MyArrayComplexQueue(capacity);

// test for enqueue
System.out.println("\n---test enqueue---\n");
for (int i = 0; i < capacity + 1; i++) {
String s = "" + i;

boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}

// test for dequeue and enqueue
System.out.println("\n---test dequeue and enqueue---\n");
for (int i = 0; i < capacity + 2; i++) {
String s = "" + (i + capacity);

if (i < capacity / 2) {
s = queue.dequeue();
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}
}

// test for dequeue
System.out.println("\n---test dequeue---\n");
while (true) {
String s = queue.dequeue();
if (s != null) {
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is empty");
break;
}
}
}
}

循环数组队列(cicular queue)

循环数组队列(冗余法)

冗余法循环数组队列存在浪费一个空间的问题

  • 判空:front == rear
  • 判满:(rear + 1) % capacity == front
  • 入队指针变化:rear = (rear + 1) % capacity;
  • 出队指针变化:front = (front + 1) % capacity;
MyArrayCircularQueueWithWaste.javaview raw
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
package dsa.structure.ds103_queue;

import java.util.Arrays;

public class MyArrayCircularQueueWithWaste {
private int capacity;
private int front = 0;
private int rear = 0;
private String[] table;

public MyArrayCircularQueueWithWaste(int capacity) {
this.capacity = capacity;
this.table = new String[capacity];
}

public boolean isEmpty() {
return front == rear;
}

public boolean isFull() {
return (rear + 1) % capacity == front;
}

public boolean enqueue(String s) {
if (isFull()) {
return false;
}

table[rear] = s;
rear = (rear + 1) % capacity;

return true;
}

public String dequeue() {
if (isEmpty()) {
return null;
}

String v = table[front];
table[front] = null;
front = (front + 1) % capacity;

return v;
}

// for test print
public String toString() {
return Arrays.toString(table);
}

public static void main(String[] args) {
int capacity = 7;
MyArrayCircularQueueWithWaste queue = new MyArrayCircularQueueWithWaste(capacity);

// test for enqueue
System.out.println("\n---test enqueue---\n");
for (int i = 0; i < capacity + 1; i++) {
String s = "" + i;

boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}

// test for dequeue and enqueue
System.out.println("\n---test dequeue and enqueue---\n");
for (int i = 0; i < capacity + 2; i++) {
String s = "" + (i + capacity);

if (i < capacity / 2) {
s = queue.dequeue();
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}
}

// test for dequeue
System.out.println("\n---test dequeue---\n");
while (true) {
String s = queue.dequeue();
if (s != null) {
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is empty");
break;
}
}
}
}

循环数组队列(flag标记法)

  • 判空:!flag && front == rear;
  • 判满:flag && front == rear;
  • 入队指针变化:rear = (rear + 1) % capacity;
  • 出队指针变化:front = (front + 1) % capacity;
MyArrayCircularQueueWithFlag.javaview raw
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
package dsa.structure.ds103_queue;

import java.util.Arrays;

public class MyArrayCircularQueueWithFlag {
private int capacity;
private int front = 0;
private int rear = 0;
private boolean flag = false; // false is empty, true is full
private String[] table;

public MyArrayCircularQueueWithFlag(int capacity) {
this.capacity = capacity;
this.table = new String[capacity];
}

public boolean isEmpty() {
return !flag && front == rear;
}

public boolean isFull() {
return flag && front == rear;
}

public boolean enqueue(String s) {
if (isFull()) {
return false;
}

table[rear] = s;
rear = (rear + 1) % capacity;

flag = true;

return true;
}

public String dequeue() {
if (isEmpty()) {
return null;
}

String v = table[front];
table[front] = null;
front = (front + 1) % capacity;

flag = false;

return v;
}

// for test print
public String toString() {
return Arrays.toString(table);
}

public static void main(String[] args) {
int capacity = 7;
MyArrayCircularQueueWithFlag queue = new MyArrayCircularQueueWithFlag(capacity);

// test for enqueue
System.out.println("\n---test enqueue---\n");
for (int i = 0; i < capacity + 1; i++) {
String s = "" + i;

boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}

// test for dequeue and enqueue
System.out.println("\n---test dequeue and enqueue---\n");
for (int i = 0; i < capacity + 2; i++) {
String s = "" + (i + capacity);

if (i < capacity / 2) {
s = queue.dequeue();
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}
}

// test for dequeue
System.out.println("\n---test dequeue---\n");
while (true) {
String s = queue.dequeue();
if (s != null) {
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is empty");
break;
}
}
}
}

循环数组队列(size辅助法)

  • 判空:size == 0 && front == rear;
  • 判满:size > 0 && front == rear;
  • 入队指针变化:rear = (rear + 1) % capacity;
  • 出队指针变化:front = (front + 1) % capacity;
MyArrayCircularQueueWithSize.javaview raw
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
package dsa.structure.ds103_queue;

import java.util.Arrays;

public class MyArrayCircularQueueWithSize {
private int capacity;
private int front = 0;
private int rear = 0;
private int size = 0;
private String[] table;

public MyArrayCircularQueueWithSize(int capacity) {
this.capacity = capacity;
this.table = new String[capacity];
}

public boolean isEmpty() {
return size == 0 && front == rear;
}

public boolean isFull() {
return size > 0 && front == rear;
}

public boolean enqueue(String s) {
if (isFull()) {
return false;
}

table[rear] = s;
rear = (rear + 1) % capacity;

size++;

return true;
}

public String dequeue() {
if (isEmpty()) {
return null;
}

String v = table[front];
table[front] = null;
front = (front + 1) % capacity;

size--;

return v;
}

// for test print
public String toString() {
return Arrays.toString(table);
}

public static void main(String[] args) {
int capacity = 7;
MyArrayCircularQueueWithSize queue = new MyArrayCircularQueueWithSize(capacity);

// test for enqueue
System.out.println("\n---test enqueue---\n");
for (int i = 0; i < capacity + 1; i++) {
String s = "" + i;

boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}

// test for dequeue and enqueue
System.out.println("\n---test dequeue and enqueue---\n");
for (int i = 0; i < capacity + 2; i++) {
String s = "" + (i + capacity);

if (i < capacity / 2) {
s = queue.dequeue();
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}
}

// test for dequeue
System.out.println("\n---test dequeue---\n");
while (true) {
String s = queue.dequeue();
if (s != null) {
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is empty");
break;
}
}
}
}

链表队列

MyLinkedQueue.javaview raw
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
package dsa.structure.ds103_queue;

import java.util.Arrays;

import dsa.link.MyDoubleLink;

public class MyLinkedQueue {

private MyDoubleLink link = new MyDoubleLink();

public MyLinkedQueue() {

}

public boolean enqueue(String s) {
return link.addLast(s);
}

public String dequeue() {
return link.removeFirst();
}

// for test print
public String toString() {
return Arrays.toString(link.toArray());
}

public static void main(String[] args) {
int capacity = 7;
MyLinkedQueue queue = new MyLinkedQueue();

// test for enqueue
System.out.println("\n---test enqueue---\n");
for (int i = 0; i < capacity + 1; i++) {
String s = "" + i;

boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}

// test for dequeue and enqueue
System.out.println("\n---test dequeue and enqueue---\n");
for (int i = 0; i < capacity + 2; i++) {
String s = "" + (i + capacity);

if (i < capacity / 2) {
s = queue.dequeue();
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
boolean ok = queue.enqueue(s);
if (ok) {
System.out.println(String.format("enqueue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is full");
break;
}
}
}

// test for dequeue
System.out.println("\n---test dequeue---\n");
while (true) {
String s = queue.dequeue();
if (s != null) {
System.out.println(String.format("dequeue value %2s , queue is %s", s, queue.toString()));
} else {
System.out.println("queue is empty");
break;
}
}
}
}
只想买包辣条