File tree Expand file tree Collapse file tree 2 files changed +75
-0
lines changed Expand file tree Collapse file tree 2 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class LinkedQueue {
2
+ //定义一个节点类
3
+ private class Node {
4
+ String value ;
5
+ Node next ;
6
+ }
7
+
8
+ private int size = 0 ;
9
+ private Node head ;
10
+ private Node tail ;
11
+
12
+ public LinkedQueue (){};
13
+
14
+ public boolean enQueue (String item ){
15
+ Node newNode = new Node ();
16
+ newNode .value = item ;
17
+
18
+ if (size == 0 ) head = newNode ;
19
+ else tail .next = newNode ;
20
+
21
+ tail = newNode ;
22
+ size ++;
23
+ return true ;
24
+ }
25
+
26
+ public String deQueue (){
27
+ String res = null ;
28
+ if (size == 0 ) return res ;
29
+ if (size == 1 ) tail = null ;
30
+ res = head .value ;
31
+ head = head .next ;
32
+ size --;
33
+ return res ;
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 循环队列
3
+ * 数组实现
4
+ * 队列空:head == tail
5
+ * 队列满:(tail + 1) % n == head
6
+ * tail所指向的位置实际没有存储数据
7
+ */
8
+
9
+ public class LoopArrayQueue {
10
+ private String items [];//存储数据的数组
11
+
12
+ private int n ;
13
+ private int size = 0 ;//记录数组容量
14
+
15
+ private int head = 0 ;
16
+ private int tail = 0 ;
17
+
18
+ public LoopArrayQueue (int capacity ){
19
+ items = new String [capacity ];
20
+ n = capacity ;
21
+ }
22
+
23
+ public boolean enQueue (String item ){
24
+ if ((tail + 1 ) % n == head ) return false ;
25
+ items [tail ] = item ;
26
+ tail = (tail + 1 ) % n ;
27
+ size ++;
28
+ return true ;
29
+ }
30
+
31
+ public String deQueue (){
32
+ String res = null ;
33
+ if (head == tail ) return res ;
34
+ res = items [head ];
35
+ head = (head + 1 ) % n ;
36
+ size --;
37
+ return res ;
38
+ }
39
+
40
+ }
You can’t perform that action at this time.
0 commit comments