[swift] [09_queue] [add]

This commit is contained in:
Jiandan 2018-10-11 22:56:35 +08:00
parent d1f416973e
commit fc167fd11f
4 changed files with 257 additions and 0 deletions

View File

@ -0,0 +1,112 @@
//
// Created by Jiandan on 2018/10/11.
// Copyright (c) 2018 Jiandan. All rights reserved.
//
import Foundation
///
struct ArrayQueue<T>: Queue {
typealias Element = T
///
private var items: [Element]
///
private var capacity = 0
///
private var head = 0
///
private var tail = 0
///
/// - parameter defaultElement:
/// - parameter capacity:
init(defaultElement: Element, capacity: Int) {
self.capacity = capacity
items = [Element](repeating: defaultElement, count: capacity)
}
// MARK: Protocol: Queue
var isEmpty: Bool { return head == tail }
var size: Int { return tail - head }
var peek: Element? { return isEmpty ? nil : items[head] }
//
// mutating func enqueue(newElement: Element) -> Bool {
// //
// if tail == capacity {
// return false
// }
//
// items[tail] = newElement
// tail += 1
// return true
// }
//
mutating func enqueue(newElement: Element) -> Bool {
// tail == capacity
if tail == capacity {
//
if head == 0 { return false }
//
for i in head ..< tail {
items[i - head] = items[i]
}
// head tail
tail -= head
head = 0
}
items[tail] = newElement
tail += 1
return true
}
mutating func dequeue() -> Element? {
if isEmpty {
return nil
}
let item = items[head]
head += 1
return item
}
}
/// 使2 Swift Array
/// iOS
struct ArrayQueue2<T>: Queue {
typealias Element = T
///
var inArray = [Element]()
///
var outArray = [Element]()
var isEmpty: Bool { return inArray.isEmpty && outArray.isEmpty }
var size: Int { return inArray.count + outArray.count }
// outArray inArray outArray
var peek: Element? { return outArray.isEmpty ? inArray.first : outArray.last }
mutating func enqueue(newElement: Element) -> Bool {
// inArray
inArray.append(newElement)
return true
}
mutating func dequeue() -> Element? {
if outArray.isEmpty {
// inArray outArray
outArray = inArray.reversed()
// inArray
inArray.removeAll()
}
// outArray
return outArray.popLast()
}
}

View File

@ -0,0 +1,63 @@
//
// Created by Jiandan on 2018/10/11.
// Copyright (c) 2018 Jiandan. All rights reserved.
//
import Foundation
///
struct CircularQueue<T>: Queue {
typealias Element = T
///
private var items: [Element]
///
private var capacity = 0
///
private var head = 0
///
private var tail = 0
///
/// - parameter defaultElement:
/// - parameter capacity:
init(defaultElement: Element, capacity: Int) {
self.capacity = capacity
items = [Element](repeating: defaultElement, count: capacity)
}
// MARK: Protocol: Queue
var isEmpty: Bool { return head == tail }
var size: Int {
if tail >= head {
return tail - head
} else {
return (tail + 1) + (capacity - head)
}
}
var peek: Element? { return isEmpty ? nil : items[head] }
mutating func enqueue(newElement: Element) -> Bool {
//
if (tail + 1) % capacity == head {
return false
}
items[tail] = newElement
tail = (tail + 1) % capacity
return true
}
mutating func dequeue() -> Element? {
if isEmpty {
return nil
}
let item = items[head]
head = (head + 1) % capacity
return item
}
}

View File

@ -0,0 +1,21 @@
//
// Created by Jiandan on 2018/10/11.
// Copyright (c) 2018 Jiandan. All rights reserved.
//
import Foundation
protocol Queue {
///
associatedtype Element
///
var isEmpty: Bool { get }
///
var size: Int { get }
///
var peek: Element? { get }
///
mutating func enqueue(newElement: Element) -> Bool
///
mutating func dequeue() -> Element?
}

View File

@ -0,0 +1,61 @@
//
// Created by Jiandan on 2018/10/11.
// Copyright (c) 2018 Jiandan. All rights reserved.
//
import Foundation
class Node<T> {
var value: T?
var next: Node?
init(value: T) {
self.value = value
}
}
struct QueueBasedOnLinkedList<T>: Queue {
typealias Element = T
///
var head: Node<Element>?
///
var tail: Node<Element>?
// MARK: Protocol: Queue
var isEmpty: Bool { return head == nil }
var size: Int {
var count = 0
while head?.next != nil {
count += 1
}
return count
}
var peek: Element? { return head?.value }
mutating func enqueue(newElement: Element) -> Bool {
if isEmpty {
//
let node = Node(value: newElement)
head = node
tail = node
} else {
tail!.next = Node(value: newElement)
tail = tail!.next
}
return true
}
mutating func dequeue() -> Element? {
if isEmpty {
return nil
}
let node = head
head = head!.next
return node?.value
}
}