Skip to content

Instantly share code, notes, and snippets.

@eunjin3786
Created October 8, 2022 06:31
Show Gist options
  • Save eunjin3786/cba905e6f1e07fb683cf4b56a5efb86f to your computer and use it in GitHub Desktop.
Save eunjin3786/cba905e6f1e07fb683cf4b56a5efb86f to your computer and use it in GitHub Desktop.
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