1
+ <?php
2
+
3
+ /**
4
+ * BidirectionalQueue
5
+ *
6
+ * @author Pu ShaoWei <[email protected] >
7
+ * @date 2017/9/13
8
+ * @license MIT
9
+ * -------------------------------------------------------------
10
+ * 思路分析: 考察PHP几个内置数组的函数
11
+ * -------------------------------------------------------------
12
+ *双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素
13
+ * @param $n
14
+ * @return int
15
+ */
16
+ class BidirectionalQueue
17
+ {
18
+ /**
19
+ * @var array
20
+ */
21
+ public $ queue = [];
22
+ public $ maxLength = 0 ; // 对列最大长度,0表示不限
23
+ public $ type = 0 ; // 对列类型
24
+ public $ frontNum = 0 ; // 前端插入的数量
25
+ public $ rearNum = 0 ; // 后端插入的数量
26
+
27
+ const C_AT_BOTH_ENDS = 1 ; // 1:两端均可输入输出
28
+ const C_FRONT_ONLY_INPUT = 2 ; // 2:前端只能输入,后端可输入输出
29
+ const C_FRONT_ONLY_OUTPUT = 3 ; // 3:前端只能输出,后端可输入输出
30
+ const C_BACK_ONLY_INPUT = 4 ; // 4:后端只能输入,前端可输入输出
31
+ const C_BACK_ONLY_OUTPUT = 5 ; // 5:后端只能输出,前端可输入输出
32
+ const C_BOTH_WAY_ONLY_INPUT = 6 ; // 6:两端均可输入输出,在哪端输入只能从哪端输出
33
+
34
+ /**
35
+ * BidirectionalQueue 初始化.
36
+ *
37
+ * @param int $type
38
+
39
+ * @param int $maxLength
40
+ */
41
+ public function __construct ($ type = self ::C_AT_BOTH_ENDS , $ maxLength = 0 )
42
+ {
43
+ var_dump ($ this ->getConfig ());
44
+ $ this ->_type = in_array ($ type , [1 , 2 , 3 , 4 , 5 , 6 ]) ? $ type : self ::C_AT_BOTH_ENDS ;
45
+ $ this ->_maxLength = intval ($ maxLength );
46
+ }
47
+
48
+ /**
49
+ * addFirst 前端入列
50
+ *
51
+ * @param $item
52
+ * @return int
53
+ */
54
+ public function addFirst ($ item )
55
+ {
56
+ return array_unshift ($ this ->queue , $ item );
57
+ }
58
+
59
+ /**
60
+ * addLast 尾部入列
61
+ *
62
+ * @param $item
63
+ * @return int
64
+ */
65
+ public function addLast ($ item )
66
+ {
67
+ return array_push ($ this ->queue , $ item );
68
+ }
69
+
70
+ /**
71
+ * removeFirst 头部出列
72
+ *
73
+ * @return mixed
74
+ */
75
+ public function removeFirst ()
76
+ {
77
+ return array_shift ($ this ->queue );
78
+ }
79
+
80
+ /**
81
+ * removeLast 尾部出列
82
+ *
83
+ * @return mixed
84
+ */
85
+ public function removeLast ()
86
+ {
87
+ return array_pop ($ this ->queue );
88
+ }
89
+
90
+ /**
91
+ * 清空队列
92
+ */
93
+ public function makeEmpty ()
94
+ {
95
+ unset($ this ->queue );
96
+ }
97
+
98
+ /**
99
+ * 获取列头
100
+ *
101
+ * @return mixed
102
+ */
103
+ public function getFirst ()
104
+ {
105
+ return reset ($ this ->queue );
106
+ }
107
+
108
+ /**
109
+ * 获取列尾
110
+ *
111
+ * @return mixed
112
+ */
113
+ public function getLast ()
114
+ {
115
+ return end ($ this ->queue );
116
+ }
117
+
118
+ /**
119
+ * 获取长度
120
+ *
121
+ * @return int
122
+ */
123
+ public function getLength ()
124
+ {
125
+ return count ($ this ->queue );
126
+ }
127
+
128
+ /**
129
+ * 获取配置常量
130
+ *
131
+ */
132
+ protected function getConfig ()
133
+ {
134
+ return [
135
+ self ::C_AT_BOTH_ENDS , // 1:两端均可输入输出
136
+ self ::C_FRONT_ONLY_INPUT , // 2:前端只能输入,后端可输入输出
137
+ self ::C_FRONT_ONLY_OUTPUT , // 3:前端只能输出,后端可输入输出
138
+ self ::C_BACK_ONLY_INPUT , // 4:后端只能输入,前端可输入输出
139
+ self ::C_BACK_ONLY_OUTPUT , // 5:后端只能输出,前端可输入输出
140
+ self ::C_BOTH_WAY_ONLY_INPUT , // 6:两端均可输入输出,在哪端输入只能从哪端输出
141
+ ];
142
+ }
143
+ }
144
+ new BidirectionalQueue ();
0 commit comments