CS
[swift] 스택과 큐
스택과 큐를 구현해보자. 스택 나중에 입력된 것이 먼저 출력되는 LIFP(Last In First Out) 데이터 구조를 나타낸다. public struct Stack { private var elements = [T]() public init() {} public mutating func pop() -> T? { return self.elements.popLast() } public mutating func push(_ element: T) { self.elements.append(element) } public func peek() -> T? { return self.elements.last } public var isEmpty: Bool { return self.elements.isEmpty } p..
[swift] 링크드 리스트로 큐 구현하기
우선 기본적인 것들을 linked list로 구현해보자. class Node { var value: String var next: Node? init(value: String, next: Node? = nil) { self.value = value self.next = next } } struct LinkedList { var head: Node? var tail: Node? init() {} var isEmpty: Bool { head == nil } mutating func push(_ value: String) { head = Node(value: value, next: head) if tail == nil { tail = head } } mutating func append(_ value: Str..