Created
October 8, 2022 06:31
-
-
Save eunjin3786/cba905e6f1e07fb683cf4b56a5efb86f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Stack<T> { | |
private var array: [T] = [] | |
var top: T? { array.last } | |
mutating func push(_ element: T) { | |
array.append(element) | |
} | |
mutating func pop() -> T? { | |
return array.popLast() | |
} | |
} | |
struct Queue<T> { | |
private var array: [T] = [] | |
var head: T? { array.first } | |
var tail: T? { array.last } | |
mutating func enqueue(_ element: T) { | |
array.append(element) | |
} | |
mutating func dequeue() -> T? { | |
return array[array.indices].popFirst() | |
} | |
} | |
var stack = Stack<Int>() | |
var queue = Queue<Int>() | |
stack.push(1) | |
stack.push(2) | |
stack.pop() // 2 | |
queue.enqueue(1) | |
queue.enqueue(2) | |
queue.dequeue() // 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment