Skip to content

Commit

Permalink
[swift][09_queue][remove] Remove "typealias Element = T".
Browse files Browse the repository at this point in the history
[swift][09_queue][fix] Fix "size" in "QueueBasedOnLinkedList".
  • Loading branch information
JiandanDream committed Oct 13, 2018
1 parent fc167fd commit e121c38
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
8 changes: 2 additions & 6 deletions swift/09_queue/ArrayQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import Foundation

/// 用数组实现的队列
struct ArrayQueue<T>: Queue {
typealias Element = T

struct ArrayQueue<Element>: Queue {
/// 数组
private var items: [Element]
/// 数组最大长度
Expand Down Expand Up @@ -78,9 +76,7 @@ struct ArrayQueue<T>: Queue {

/// 使用2个数组实现无界队列,用到 Swift 中 Array 较多的方法
/// 来源:《iOS 面试之道》(故胤道长,唐巧)
struct ArrayQueue2<T>: Queue {
typealias Element = T

struct ArrayQueue2<Element>: Queue {
/// 输入数组,主要负责入队
var inArray = [Element]()
/// 输出数组,主要负责出队
Expand Down
4 changes: 1 addition & 3 deletions swift/09_queue/CircularQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import Foundation

/// 循环队列
struct CircularQueue<T>: Queue {
typealias Element = T

struct CircularQueue<Element>: Queue {
/// 数组
private var items: [Element]
/// 数组最大长度
Expand Down
20 changes: 7 additions & 13 deletions swift/09_queue/QueueBasedOnLinkedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,7 @@

import Foundation

class Node<T> {
var value: T?
var next: Node?

init(value: T) {
self.value = value
}
}

struct QueueBasedOnLinkedList<T>: Queue {
typealias Element = T

struct QueueBasedOnLinkedList<Element>: Queue {
/// 队首
var head: Node<Element>?
/// 队尾
Expand All @@ -27,10 +16,15 @@ struct QueueBasedOnLinkedList<T>: Queue {
var isEmpty: Bool { return head == nil }

var size: Int {
var count = 0
if isEmpty {
return 0
}

var count = 1 // head 本身算一个
while head?.next != nil {
count += 1
}

return count
}

Expand Down

0 comments on commit e121c38

Please sign in to comment.