Bibi's DevLog ๐ค๐
[Swift] Queue ํ ๊ตฌํํ๊ธฐ (๋ฐฐ์ด / ์ฐ๊ฒฐ๋ฆฌ์คํธ / Two Stack / Head ๋ฐฉ์) ๋ณธ๋ฌธ
๐ฑ๐ iOS/Code Templates
[Swift] Queue ํ ๊ตฌํํ๊ธฐ (๋ฐฐ์ด / ์ฐ๊ฒฐ๋ฆฌ์คํธ / Two Stack / Head ๋ฐฉ์)
๋น๋น bibi 2022. 12. 21. 00:50Queue๋ฅผ ๊ตฌํํ๋ ๋ค์ํ ๋ฐฉ์๋ค.
Queue ๊ตฌํํ๊ธฐ - (1) ๋ฐฐ์ด
๊ฐ์ฅ ์ฝ์ง๋ง ๊ฐ์ฅ ์ถ์ฒํ์ง ์๋ ๋ฐฉ๋ฒ
struct Queue<T>: CustomStringConvertible {
private var elements: [T] = []
public init() {}
var isEmpty: Bool {
elements.isEmpty
}
var peek: T? {
elements.first
}
var description: String {
if isEmpty { return "Queue is empty..." }
return "---- Queue start ----\n"
+ elements.map({"\($0)"}).joined(separator: " -> ")
+ "\n---- Queue End ----"
}
mutating func enqueue(_ value: T) {
elements.append(value)
}
mutating func dequeue() -> T? {
isEmpty ? nil : elements.removeFirst()
}
}
Queue ๊ตฌํํ๊ธฐ - (2) ์ฐ๊ฒฐ๋ฆฌ์คํธ(Singly Linked List)
class Node<T>: CustomStringConvertible {
var value: T
var next: Node?
var description: String {
guard let next = next else { return "\(value)" }
return "\(value) -> " + String(describing: next)
}
init(value: T, next: Node? = nil) {
self.value = value
self.next = next
}
}
struct Queue<T>: CustomStringConvertible {
var front: Node<T>?
var rear: Node<T>?
init() { }
var isEmpty: Bool {
return front == nil
}
var description: String {
guard let front = front else { return "Empty Queue" }
return String(describing: front)
}
var peek: T? {
return front?.value
}
}
extension Queue {
mutating private func push(_ value: T) {
front = Node(value: value, next: front)
if rear == nil {
rear = front
}
}
mutating func enqueue(_ value: T) {
if isEmpty {
self.push(value)
return
}
rear?.next = Node(value: value)
rear = rear?.next
}
mutating func dequeue() -> T? {
defer {
front = front?.next
if isEmpty {
rear = nil
}
}
return front?.value
}
}
Queue ๊ตฌํํ๊ธฐ - (3) Stack ๋ ๊ฐ๋ฅผ ์ฌ์ฉํด ๊ตฌํ (two stacks)
struct TwoStackQueue<T>: CustomStringConvertible {
private var leftStack: [T] = []
private var rightStack: [T] = []
var isEmpty: Bool {
leftStack.isEmpty && rightStack.isEmpty
}
var peek: T? {
leftStack.isEmpty ? rightStack.first : leftStack.last
}
var description: String {
if isEmpty {
return "Queue is empty..."
}
var allElements: [T] = []
if leftStack.isEmpty == false {
allElements.append(contentsOf: leftStack.reversed())
}
allElements.append(contentsOf: rightStack)
return "---- Queue start ----\n"
+ allElements.map({"\($0)"}).joined(separator: " -> ")
+ "\n---- Queue End ----"
}
}
extension TwoStackQueue {
mutating func enqueue(_ element: T) {
rightStack.append(element)
}
}
extension TwoStackQueue {
mutating func dequeue() -> T? {
if isEmpty {
return nil
}
if leftStack.isEmpty {
leftStack = rightStack.reversed()
rightStack.removeAll()
}
return leftStack.removeLast()
}
}
Queue ๊ตฌํํ๊ธฐ - (4) head๋ฅผ ์ฌ์ฉํด ๊ตฌํ
public struct Queue<T> {
fileprivate var array = [T?]()
fileprivate var head = 0
public var isEmpty: Bool {
return count == 0
}
public var count: Int {
return array.count - head
}
public mutating func enqueue(_ element: T) {
array.append(element)
}
public mutating func dequeue() -> T? {
guard head < array.count, let element = array[head] else { return nil }
array[head] = nil
head += 1
let percentage = Double(head)/Double(array.count)
if array.count > 50 && percentage > 0.25 {
array.removeFirst(head)
head = 0
}
return element
}
public var front: T? {
if isEmpty {
return nil
} else {
return array[head]
}
}
}
'๐ฑ๐ iOS > Code Templates' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] Stack ์คํ ๊ตฌํ (๋ฐฐ์ด / ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ก ๊ตฌํํ๊ธฐ) (0) | 2022.12.20 |
---|---|
[Swift] Linked List - Singly Linked List ๊ตฌํ (0) | 2022.12.15 |
[Swift] URL์์ ๋น๋๊ธฐ๋ก ์ด๋ฏธ์ง ๋ค์ด๋ก๋ ๋ฐ ์บ์ฑํ๊ธฐ - NSCache (0) | 2022.05.18 |
์ฝ๋๋ก UIView ๋ง๋ค๊ธฐ ๊ธฐ๋ณธ ํ ํ๋ฆฟ (0) | 2022.05.11 |
UIFont Extension - custom font (0) | 2022.05.11 |