티스토리 뷰
import Foundation
class Stack {
private var list = [String]()
func push(item: String) {
list.append(item)
}
func pop() -> String? {
return list.popLast()
}
var isEmpty: Bool {
return list.isEmpty
}
}
class Queue {
private var inBox = Stack()
private var outBox = Stack()
private let lock = NSLock()
func enQueue(item: String) {
lock.lock()
inBox.push(item: item)
lock.unlock()
}
func deQueue() -> String? {
lock.lock()
if outBox.isEmpty {
while !inBox.isEmpty, let pop = inBox.pop() {
outBox.push(item: pop)
}
}
lock.unlock()
return outBox.pop()
}
var isEmpty: Bool {
return outBox.isEmpty
}
}
// Testing
var queue = Queue()
(0...10)
.map { String($0) }
.forEach { item in
queue.enQueue(item: item)
}
print("item = \(queue.deQueue()!)")
queue.enQueue(item: "dh")
while let deQueue = queue.deQueue() {
print("item = \(deQueue)")
}
'Coding' 카테고리의 다른 글
| 배열에서 가장 큰 정사각형 찾기 (0) | 2019.12.02 |
|---|---|
| fibonacci. index에 해당하는 값은? (0) | 2019.11.27 |
| c0dility. 1. BinaryGap (0) | 2019.11.23 |
| ROT13 (0) | 2019.08.14 |
| Algorithm. swift. 진수 변환 (0) | 2018.07.16 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- CentOS
- github
- Java
- MySQL
- git
- php
- Codable
- go
- nodejs
- cocoapods
- rxswift
- Xcode
- Windows
- windows10
- Linux
- ios
- intellij
- tomcat
- centos8
- Kotlin
- war
- android
- docker
- ubuntu
- SWIFT
- golang
- Gradle
- enum
- Spring
- Python
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함