-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathArrayQueue.swift
63 lines (55 loc) · 1.4 KB
/
ArrayQueue.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// ArrayQueue.swift
// Queue
//
// Created by ggl on 2019/4/10.
// Copyright © 2019年 ggl. All rights reserved.
// 顺序队列
import Foundation
class ArrayQueue<Element> {
/// 队列底层存储数组,这里做了优化,让其大小固定
var array: [Element?]
/// 队列首指针
var head: Int
/// 队列尾指针
var tail: Int
init(capcity: Int) {
array = [Element?](repeating: nil, count: capcity);
head = 0
tail = 0
}
/// 数据加入队列
///
/// - Parameter item: 需要加入队列的数据
/// - Returns: 是否加入成功
@discardableResult
func enQueue(item: Element) -> Bool {
if tail == array.count {
if head == 0 {
//表示队列满了,无法再插入新的元素
return false
}
//数据搬移
for index in head..<tail {
array[index - head] = array[index]
}
tail -= head
head = 0
}
array[tail] = item
tail += 1
return true
}
/// 队列中取出数据
///
/// - Returns: 队列中获取到的数据
func deQueue() -> Element? {
if tail == head {
return nil
}
let item = array[head]
array[head] = nil
head += 1
return item
}
}