Skip to content

Commit 34b568f

Browse files
author
wangzheng
committed
09_queue
1 parent 619caad commit 34b568f

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed

java/09_queue/ArrayQueue.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package queue;
2+
3+
/**
4+
* Created by wangzheng on 2018/10/9.
5+
*/
6+
// 用数组实现的队列
7+
public class ArrayQueue {
8+
// 数组:items,数组大小:n
9+
private String[] items;
10+
private int n = 0;
11+
// head表示队头下标,tail表示队尾下标
12+
private int head = 0;
13+
private int tail = 0;
14+
15+
// 申请一个大小为capacity的数组
16+
public ArrayQueue(int capacity) {
17+
items = new String[capacity];
18+
n = capacity;
19+
}
20+
21+
// 入队
22+
public boolean enqueue(String item) {
23+
// 如果tail == n 表示队列已经满了
24+
if (tail == n) return false;
25+
items[tail] = item;
26+
++tail;
27+
return true;
28+
}
29+
30+
// 出队
31+
public String dequeue() {
32+
// 如果head == tail 表示队列为空
33+
if (head == tail) return null;
34+
// 为了让其他语言的同学看的更加明确,把--操作放到单独一行来写了
35+
String ret = items[head];
36+
++head;
37+
return ret;
38+
}
39+
40+
public void printAll() {
41+
for (int i = head; i < tail; ++i) {
42+
System.out.print(items[i] + " ");
43+
}
44+
System.out.println();
45+
}
46+
}

java/09_queue/CircularQueue.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package queue;
2+
3+
/**
4+
* Created by wangzheng on 2018/10/9.
5+
*/
6+
public class CircularQueue {
7+
// 数组:items,数组大小:n
8+
private String[] items;
9+
private int n = 0;
10+
// head表示队头下标,tail表示队尾下标
11+
private int head = 0;
12+
private int tail = 0;
13+
14+
// 申请一个大小为capacity的数组
15+
public CircularQueue(int capacity) {
16+
items = new String[capacity];
17+
n = capacity;
18+
}
19+
20+
// 入队
21+
public boolean enqueue(String item) {
22+
// 队列满了
23+
if ((tail + 1) % n == head) return false;
24+
items[tail] = item;
25+
tail = (tail + 1) % n;
26+
return true;
27+
}
28+
29+
// 出队
30+
public String dequeue() {
31+
// 如果head == tail 表示队列为空
32+
if (head == tail) return null;
33+
String ret = items[head];
34+
head = (head + 1) % n;
35+
return ret;
36+
}
37+
38+
public void printAll() {
39+
for (int i = head; i < tail; ++i) {
40+
System.out.print(items[i] + " ");
41+
}
42+
System.out.println();
43+
}
44+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package queue;
2+
3+
/**
4+
* Created by wangzheng on 2018/10/9.
5+
*/
6+
public class DynimacArrayQueue {
7+
// 数组:items,数组大小:n
8+
private String[] items;
9+
private int n = 0;
10+
// head表示队头下标,tail表示队尾下标
11+
private int head = 0;
12+
private int tail = 0;
13+
14+
// 申请一个大小为capacity的数组
15+
public DynimacArrayQueue(int capacity) {
16+
items = new String[capacity];
17+
n = capacity;
18+
}
19+
20+
// 入队操作,将item放入队尾
21+
public boolean enqueue(String item) {
22+
// tail == n表示队列末尾没有空间了
23+
if (tail == n) {
24+
// tail ==n && head==0,表示整个队列都占满了
25+
if (head == 0) return false;
26+
// 数据搬移
27+
for (int i = head; i < tail; ++i) {
28+
items[i-head] = items[i];
29+
}
30+
// 搬移完之后重新更新head和tail
31+
tail -= head;
32+
head = 0;
33+
}
34+
35+
items[tail] = item;
36+
tail++;
37+
return true;
38+
}
39+
40+
// 出队
41+
public String dequeue() {
42+
// 如果head == tail 表示队列为空
43+
if (head == tail) return null;
44+
// 为了让其他语言的同学看的更加明确,把--操作放到单独一行来写了
45+
String ret = items[head];
46+
++head;
47+
return ret;
48+
}
49+
50+
public void printAll() {
51+
for (int i = head; i < tail; ++i) {
52+
System.out.print(items[i] + " ");
53+
}
54+
System.out.println();
55+
}
56+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package queue;
2+
3+
/**
4+
* 基于链表实现的队列
5+
*
6+
* Author: Zheng
7+
*/
8+
public class QueueBasedOnLinkedList {
9+
10+
// 队列的队首和队尾
11+
private Node head = null;
12+
private Node tail = null;
13+
14+
// 入队
15+
public void enqueue(String value) {
16+
if (tail == null) {
17+
Node newNode = new Node(value, null);
18+
head = newNode;
19+
tail = newNode;
20+
} else {
21+
tail.next = new Node(value, null);
22+
tail = tail.next;
23+
}
24+
}
25+
26+
// 出队
27+
public String dequeue() {
28+
if (head == null) return null;
29+
30+
String value = head.data;
31+
head = head.next;
32+
if (head == null) {
33+
tail = null;
34+
}
35+
return value;
36+
}
37+
38+
public void printAll() {
39+
Node p = head;
40+
while (p != null) {
41+
System.out.print(p.data + " ");
42+
p = p.next;
43+
}
44+
System.out.println();
45+
}
46+
47+
private static class Node {
48+
private String data;
49+
private Node next;
50+
51+
public Node(String data, Node next) {
52+
this.data = data;
53+
this.next = next;
54+
}
55+
56+
public String getData() {
57+
return data;
58+
}
59+
}
60+
61+
}

0 commit comments

Comments
 (0)