Skip to content

Commit 1075b1d

Browse files
committed
Update README.markdown
1 parent 8f1d610 commit 1075b1d

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

Queue/README.markdown

+18-16
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ queue.dequeue()
4242
4343
## 实现代码
4444

45-
下面给出了一个简单粗暴的队列实现。它只是简单的包装了一下自带的数组,并暴露出入队(enqueue)、出队(dequeue)和取得队首元素(peek)三个操作:
45+
下面给出了一个简单粗暴的队列实现。它只是简单地包装了一下自带的数组,并提供了入队(enqueue)、出队(dequeue)和取得队首元素(peek)三个操作:
4646

4747
```swift
4848
public struct Queue<T> {
@@ -178,21 +178,21 @@ public struct Queue<T> {
178178

179179
现在数组存储的元素类型是 `T?`,而不是先前的 `T`,因为我们需要某种方式来将数组的元素标记为空。`head` 变量用于存储队首元素的下标值。
180180

181-
Most of the new functionality sits in `dequeue()`. When we dequeue an item, we first set `array[head]` to `nil` to remove the object from the array. Then we increment `head` because now the next item has become the front one.
181+
绝大多数的改进都是针对 `dequeue()` 函数,在将队首元素出队时,我们首先将 `array[head]` 设置为 `nil` 来将这个元素从数组中移除。然后将 `head` 的值加一,使得下一个元素变成新的队首。
182182

183-
We go from this:
183+
数组从这样:
184184

185185
[ "Ada", "Steve", "Tim", "Grace", xxx, xxx ]
186186
head
187187

188-
to this:
188+
变成这样:
189189

190190
[ xxx, "Steve", "Tim", "Grace", xxx, xxx ]
191191
head
192192

193-
It's like some weird supermarket where the people in the checkout lane don't shuffle forward towards the cash register, but the cash register moves up the queue.
193+
这就像在某个外星球的奇怪超市,在那里排队结账的人保持不动,而收银员往队尾移动来挨个结账。
194194

195-
Of course, if we never remove those empty spots at the front then the array will keep growing as we enqueue and dequeue elements. To periodically trim down the array, we do the following:
195+
当然,如果我们从不移除队首的空位,随着不断地入队和出队,队列所占空间将不断增长。为了周期性地清理无用空间,我们编写了如下代码:
196196

197197
```swift
198198
let percentage = Double(head)/Double(array.count)
@@ -202,12 +202,14 @@ Of course, if we never remove those empty spots at the front then the array will
202202
}
203203
```
204204

205-
This calculates the percentage of empty spots at the beginning as a ratio of the total array size. If more than 25% of the array is unused, we chop off that wasted space. However, if the array is small we don't want to resize it all the time, so there must be at least 50 elements in the array before we try to trim it.
205+
这段代码计算了队首空余的元素占数组总元素的百分比,如果空余元素超过 25%,我们就进行一波清理。但是,如果队列的长度过小,我们也不想频繁地清理空间,所以在清理空间之前,队列中至少要有 50 个元素。
206206

207-
> **Note:** I just pulled these numbers out of thin air -- you may need to tweak them based on the behavior of your app in a production environment.
207+
> **注意:**这个 50 只是我凭空捏造的一个数字,在实际的项目中,你应该根据项目本身来选定一个合情合理的值。
208208
209209
To test this in a playground, do:
210210

211+
如果想在 Playground 中测试,可以参考下面的代码:
212+
211213
```swift
212214
var q = Queue<String>()
213215
q.array // [] empty array
@@ -231,32 +233,32 @@ q.array // [nil, nil, {Some "Tim"}, {Some "Grace"}]
231233
q.count // 2
232234
```
233235

234-
To test the trimming behavior, replace the line,
236+
为了测试队列的自动调整特性,将下面这段代码:
235237

236238
```swift
237239
if array.count > 50 && percentage > 0.25 {
238240
```
239241

240-
with:
242+
替换为:
241243

242244
```swift
243245
if head > 2 {
244246
```
245247

246-
Now if you dequeue another object, the array will look as follows:
248+
现在,如果你再次执行出队操作,数组将看起来像下面这样:
247249

248250
```swift
249251
q.dequeue() // "Tim"
250252
q.array // [{Some "Grace"}]
251253
q.count // 1
252254
```
253255

254-
The `nil` objects at the front have been removed and the array is no longer wasting space. This new version of `Queue` isn't much more complicated than the first one but dequeuing is now also an **O(1)** operation, just because we were a bit smarter about how we used the array.
256+
在数组前面的 `nil` 已经被移除了,数组本身也没有空间浪费了新版本的队列实现并没有比初版复杂很多,但现在出队操作的复杂度已经从当初的 **O(n)** 变为了现在的 **O(1)**,只是因为我们在数组的使用策略上耍了一点小心机
255257

256-
## See also
258+
## 扩展阅读
257259

258-
There are many other ways to create a queue. Alternative implementations use a [linked list](../Linked List/), a [circular buffer](../Ring Buffer/), or a [heap](../Heap/).
260+
事实上,队列还有很多种其他的实现方式,例如可以使用[链表](../Linked List/)[环形缓冲区](../Ring Buffer/)或是[堆](../Heap/)来实现
259261

260-
Variations on this theme are [deque](../Deque/), a double-ended queue where you can enqueue and dequeue at both ends, and [priority queue](../Priority Queue/), a sorted queue where the "most important" item is always at the front.
262+
队列有很多变体,包括[双端队列](../Deque/),一个两端都可以出队和入队的队列;[优先队列](../Priority Queue/),一个有序的队列,最重要的元素排在队首
261263

262-
*Written for Swift Algorithm Club by Matthijs Hollemans*
264+
*作者:Matthijs Hollemans;译者:KSCO*

0 commit comments

Comments
 (0)