SWiFT(48)
-
[Swift] (자료구조) 큐 / Queue
import Foundationclass Queue { var enQueue: [T] = [] var deQueue: [T] = [] var count: Int { return enQueue.count + deQueue.count } var isEmpty: Bool { return enQueue.isEmpty && deQueue.isEmpty } init() { } func push(_ element: T) { enQueue.append(element) } func pop() -> T { if deQueue.isEmpty { deQueue = enQueue.reversed() enQueue.removeAll() } return deQueue.popLast()! }} import Foun..
2022.08.06 -
[Swift] (자료구조) 힙 / Heap
import Foundation struct Heap { private var elements: [T] = [] private let sortFunction: (T, T) -> Bool var isEmpty: Bool { return self.elements.count == 1 } var peek: T? { if self.isEmpty { return nil } return self.elements.last! } var count: Int { return self.elements.count - 1 } init(elements: [T] = [], sortFunction: @escaping (T, T) -> Bool) { if !elements.isEmpty { self.elements = [elements..
2022.08.01 -
[Swift] (자료구조) 더블 링크드 리스트 / Doubly Linked List
Node import Foundation class Node { let id: Int let data: T var next: Node? = nil var prev: Node? = nil init(id: Int, data: T) { self.id = id self.data = data } } DoublyLinkdedList struct DoublyLinkedList { var head: Node? var tail: Node? var front: Node? { return head } var back: Node? { return tail } func showAll() { var now = head print("===== Linked List ======") while now != nil { now?.next..
2022.08.01 -
[Swift] (자료구조) 링크드 리스트 / Linked List
Node import Foundation class Node { let id: Int let data: T var next: Node? init(id: Int, data: T, next: Node? = nil) { self.id = id self.data = data self.next = next } } LinkedList struct LinkedList { var head: Node? var tail: Node? func showList() { var now = head print("===== Linked List ======") while now != nil { now?.next == nil ? print("id: \(now?.id) | data: \(now?.data)") : print("id: \..
2022.08.01 -
[Swift] (자료구조) 스택 / Stack
import Foundation struct Stack { var elements: [T] = [] var count : Int { return elements.count } var isEmpty : Bool { return elements.isEmpty } mutating func pop() -> T? { return elements.popLast() } mutating func push(_ element: T) { elements.append(element) } func top() -> T? { return elements.last } }
2022.08.01