Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- APNS
- 오블완
- ios
- WWDC
- modern concurrency deep dive
- 야곰 # 야곰아카데미커리어스타터캠프 #iOS개발자 # 부트캠프
- modern concurrency
- 티스토리챌린지
- SWIFT
Archives
- Today
- Total
Geon
Deque 본문
class Node {
var value: Int
var nextNode: Node?
var prevNode: Node?
init(value: Int = 0, next: Node? = nil, prev: Node? = nil) {
self.value = value
self.nextNode = next
self.prevNode = prev
}
}
class LinkedList {
var size: Int = 0
var head: Node?
var tail: Node?
func insertBack(value: Int) {
let newNode = Node(value: value)
if head == nil {
head = newNode
tail = newNode
} else {
tail?.nextNode = newNode
newNode.prevNode = tail
tail = tail?.nextNode
}
size += 1
}
func insertAt(idx: Int, value: Int) {
let newNode = Node(value: value)
if idx == 0 {
newNode.nextNode = head
head?.prevNode = newNode
head = newNode
} else {
var current = head
for _ in 0..<(idx - 1) {
current = current?.nextNode
}
newNode.nextNode = current?.nextNode
current?.nextNode?.prevNode = newNode
current?.nextNode = newNode
newNode.prevNode = current
}
size += 1
}
func removeBack() {
tail = tail?.prevNode
tail?.nextNode = nil
size -= 1
}
func removeAt(idx: Int) {
if idx == 0 {
head = head?.nextNode
head?.prevNode = nil
} else {
var current = head
for _ in 0..<(idx - 1) {
current = current?.nextNode
}
current?.nextNode = current?.nextNode?.nextNode
current?.nextNode?.prevNode = current
}
size -= 1
}
func get(idx: Int) -> Int? {
var current = head
for _ in 0..<idx {
current = current?.nextNode
}
return current?.value
}
func set(idx: Int, value: Int) {
var current = head
for _ in 0..<idx {
current = current?.nextNode
}
current?.value = value
}
func printList() {
var current = head
while let node = current {
print(node.value, terminator: "")
current = node.nextNode
if current != nil {
print("->", terminator: "")
}
}
print()
}
}
'코딩테스트' 카테고리의 다른 글
Valid Parentheses (0) | 2024.01.09 |
---|---|
Design Browser History (0) | 2024.01.07 |
Two Pointer (2) | 2024.01.06 |
제한 조건 보는 법 (0) | 2024.01.04 |
FrogRiverOne (0) | 2022.05.15 |