Skip to content

Commit 0d87347

Browse files
committed
队列的三种实现 :基于数组的实现、基于链表的实现、基于栈的实现
1 parent 3441163 commit 0d87347

10 files changed

Lines changed: 418 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package cn.edu.tju.rico.queue;
2+
3+
4+
/**
5+
* Title: 基于链表的队列实现
6+
* Description: 含头结点(头结点不存储值,添加操作O(1)),尾指针(删除操作O(1))
7+
* @author rico
8+
* @created 2017年5月19日 下午8:49:34
9+
*/
10+
11+
public class LinkedQueue<E> {
12+
13+
private Node<E> head; // 头结点
14+
private Node<E> rear; // 尾指针
15+
private int size; // 队列大小
16+
17+
public LinkedQueue(){
18+
head = rear = new Node<E>(null);
19+
}
20+
21+
/**
22+
* @description 添加元素到队尾
23+
* @author rico
24+
* @created 2017年5月19日 下午8:52:20
25+
* @param data
26+
*/
27+
public void put(E data){
28+
Node<E> node = new Node<E>(data);
29+
rear.next = node;
30+
rear = node;
31+
size ++;
32+
}
33+
34+
/**
35+
* @description 删除队头并返回队头元素的值
36+
* @author rico
37+
* @created 2017年5月19日 下午8:52:24
38+
* @return
39+
*/
40+
public E pop(){
41+
if(!isEmpty()){
42+
E e = null;
43+
Node<E> temp = head.next;
44+
head.next = temp.next;
45+
e = temp.data;
46+
47+
temp.data = null;
48+
temp.next = null;
49+
size--;
50+
return e;
51+
}
52+
return null;
53+
}
54+
55+
/**
56+
* @description 返回队头元素的值
57+
* @author rico
58+
* @created 2017年5月19日 下午8:52:28
59+
* @return
60+
*/
61+
public E peek() {
62+
if (!isEmpty()) {
63+
return head.next.data;
64+
}
65+
return null;
66+
}
67+
68+
69+
/**
70+
* @description 队列是否为空
71+
* @author rico
72+
* @created 2017年5月19日 下午8:52:33
73+
* @return
74+
*/
75+
public boolean isEmpty(){
76+
return size == 0;
77+
}
78+
79+
80+
/**
81+
* @description 队列大小
82+
* @author rico
83+
* @created 2017年5月19日 下午8:52:35
84+
* @return
85+
*/
86+
public int size() {
87+
return size;
88+
}
89+
90+
@Override
91+
public String toString() {
92+
// TODO Auto-generated method stub
93+
Node<E> cur = head.next;
94+
StringBuilder sb = new StringBuilder();
95+
while(cur != null){
96+
sb.append(cur.data).append(" ");
97+
cur = cur.next;
98+
}
99+
return sb.toString();
100+
}
101+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.edu.tju.rico.queue;
2+
3+
/**
4+
* Title: 结点类
5+
* Description: 线性表的基本元素
6+
*
7+
* @author rico
8+
* @created 2017年4月6日 下午9:55:58
9+
*/
10+
public class Node<T> {
11+
//包可见性
12+
Node<T> next;
13+
T data;
14+
15+
/**
16+
* 构造函数
17+
*
18+
* @description 构造一个新节点
19+
* @author rico
20+
* @created 2017年4月6日 下午9:56:56
21+
* @param data
22+
* 新元素数据
23+
* @param next
24+
* 新元素与链表结合节点
25+
*/
26+
public Node(T data) {
27+
this.data = data;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return data.toString();
33+
}
34+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package cn.edu.tju.rico.queue;
2+
3+
import java.util.Arrays;
4+
5+
6+
/**
7+
* Title: 基于数组的队列实现
8+
* Description:
9+
* @author rico
10+
* @created 2017年5月19日 下午8:23:55
11+
*/
12+
public class SeqQueue<E> {
13+
14+
15+
/** 队列的存储结构 (@author: rico) */
16+
private Object[] queue;
17+
private int size;
18+
private int maxSize; // 最大容量
19+
20+
public SeqQueue(int maxSize){
21+
this.maxSize = maxSize;
22+
queue = new Object[maxSize];
23+
}
24+
25+
26+
/**
27+
* @description 添加元素到队尾
28+
* @author rico
29+
* @created 2017年5月19日 下午8:25:32
30+
* @param data
31+
*/
32+
public void put(E data){
33+
if(!isFull()){
34+
queue[size] = data;
35+
size ++;
36+
}
37+
}
38+
39+
40+
/**
41+
* @description 删除队头并返回队头元素的值
42+
* @author rico
43+
* @created 2017年5月19日 下午8:25:47
44+
* @return
45+
*/
46+
public E pop(){
47+
if (!isEmpty()) {
48+
E temp = (E) queue[0];
49+
for (int i = 0; i < size - 1; i++) {
50+
queue[i] = queue[i+1];
51+
}
52+
queue[size-1] = null;
53+
size--;
54+
return temp;
55+
}
56+
return null;
57+
}
58+
59+
60+
/**
61+
* @description 返回队头元素
62+
* @author rico
63+
* @created 2017年5月19日 下午8:26:01
64+
* @return
65+
*/
66+
public E peek(){
67+
if (!isEmpty()) {
68+
return (E) queue[0];
69+
}
70+
return null;
71+
}
72+
73+
74+
/**
75+
* @description 队列是否已满
76+
* @author rico
77+
* @created 2017年5月19日 下午8:26:14
78+
* @return
79+
*/
80+
public boolean isFull(){
81+
return size == maxSize;
82+
}
83+
84+
85+
/**
86+
* @description 队列是否为空
87+
* @author rico
88+
* @created 2017年5月19日 下午8:26:25
89+
* @return
90+
*/
91+
public boolean isEmpty(){
92+
return size == 0;
93+
}
94+
95+
96+
/**
97+
* @description 队列的大小
98+
* @author rico
99+
* @created 2017年5月19日 下午8:26:34
100+
* @return
101+
*/
102+
public int size(){
103+
return size;
104+
}
105+
106+
@Override
107+
public String toString() {
108+
// TODO Auto-generated method stub
109+
return Arrays.toString(queue);
110+
}
111+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cn.edu.tju.rico.queue;
2+
3+
import cn.edu.tju.rico.stack.LinkedStack;
4+
5+
6+
/**
7+
* Title: 使用两个栈模拟一个队列
8+
* Description: 其中一个栈作存储空间,另一个栈作临时缓冲区
9+
* @author rico
10+
* @created 2017年5月19日 下午10:45:11
11+
*/
12+
public class StackQueue<E> {
13+
14+
private LinkedStack<E> stack1; // 存储空间
15+
private LinkedStack<E> stack2; //临时缓冲区
16+
17+
public StackQueue() {
18+
stack1 = new LinkedStack<E>();
19+
stack2 = new LinkedStack<E>();
20+
}
21+
22+
23+
/**
24+
* @description 添加元素到队尾。先检查stack2是否为空:
25+
* 若为空,则直接对stack1执行压栈操作
26+
* 否则,先将stack2中的元素倒回stack1,再对stack1执行压栈操作
27+
* @author rico
28+
* @created 2017年5月19日 下午10:47:59
29+
* @param e
30+
*/
31+
public void put(E e) {
32+
if (!stack2.isEmpty()) {
33+
while (stack2.size() > 0) {
34+
stack1.push(stack2.pop().getData());
35+
}
36+
}
37+
stack1.push(e);
38+
}
39+
40+
41+
/**
42+
* @description 删除队头并返回队头元素的值。先检查stack2是否为空:
43+
* 若为空,先将stack1中的size-1个元素倒回stack2,再对stack1中栈底元素执行弹栈操作
44+
* 否则,则直接对stack2执行弹栈操作
45+
* @author rico
46+
* @created 2017年5月19日 下午10:48:32
47+
* @return
48+
*/
49+
public E pop() {
50+
if (stack2.isEmpty()) {
51+
if (!stack1.isEmpty()) {
52+
while (stack1.size() > 1) {
53+
stack2.push(stack1.pop().getData());
54+
}
55+
return stack1.pop().getData();
56+
}
57+
return null;
58+
} else {
59+
return stack2.pop().getData();
60+
}
61+
}
62+
63+
@Override
64+
public String toString() {
65+
if (!stack1.isEmpty()) {
66+
return new StringBuilder(stack1.toString()).reverse().toString();
67+
}
68+
return stack2.toString();
69+
}
70+
}

src/cn/edu/tju/rico/stack/LinkedStack.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ public void print() {
134134
System.out.println();
135135
}
136136

137+
138+
137139
/**
138140
* @description 返回栈的大小
139141
* @author rico
@@ -150,4 +152,16 @@ public LinkedStack<E> getMin() {
150152
public void setMin(LinkedStack<E> min) {
151153
this.min = min;
152154
}
155+
156+
@Override
157+
public String toString() {
158+
// TODO Auto-generated method stub
159+
Node<E> index = top;
160+
StringBuilder sb = new StringBuilder();
161+
while (index != null) {
162+
sb.append(index.data).append(" ");
163+
index = index.next;
164+
}
165+
return sb.toString();
166+
}
153167
}

src/cn/edu/tju/rico/stack/Node.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ public Node(T data) {
2727
this.data = data;
2828
}
2929

30+
public T getData() {
31+
return data;
32+
}
33+
34+
public void setData(T data) {
35+
this.data = data;
36+
}
37+
3038
@Override
3139
public String toString() {
3240
return data.toString();
3341
}
42+
3443
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.edu.tju.rico.test;
2+
3+
import cn.edu.tju.rico.queue.LinkedQueue;
4+
5+
public class LinkedQueueTest {
6+
public static void main(String[] args) {
7+
LinkedQueue<Integer> queue = new LinkedQueue<Integer>();
8+
queue.put(1);
9+
queue.put(2);
10+
queue.put(4);
11+
queue.put(3);
12+
queue.put(0);
13+
System.out.println(queue);
14+
System.out.println("\n ------------------- \n");
15+
16+
17+
queue.pop();
18+
System.out.println(queue);
19+
System.out.println("\n ------------------- \n");
20+
21+
22+
System.out.println(queue.peek());
23+
queue.put(121);
24+
System.out.println(queue);
25+
26+
}
27+
}

src/cn/edu/tju/rico/test/LinkedStackTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ else if(o1 < o2)
2020
return 0;
2121
}
2222
};
23+
2324
stack.push(7,c);
2425
stack.push(6,c);
2526
stack.push(8,c);

0 commit comments

Comments
 (0)