Skip to content

Commit

Permalink
[Feat] Node에 prev 추가 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
meltsplit committed Apr 23, 2023
1 parent f12c682 commit 4d3a5d0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
29 changes: 26 additions & 3 deletions SOPTving/SOPTving/Presentation/MainScene/VC/MainVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ final class MainVC: UIViewController {
return label
}()

private lazy var prevButton = ButtonBuilder()
.setTitle("이전으로",
color: .white,
font: .tvingSemiBold(ofSize: 16))
.setBackgroundColor(.systemBlue)
.setCornerRadius(12)
.setAction { [weak self] _ in
self?.prevButtonDidTap()
}
.build()

private lazy var nextButton = ButtonBuilder()
.setTitle("다음으로",
color: .white,
Expand All @@ -48,6 +59,7 @@ final class MainVC: UIViewController {
}
.build()


//MARK: - Life Cycle

override func viewDidLoad() {
Expand Down Expand Up @@ -84,7 +96,7 @@ extension MainVC {

private func setData() {
dataLinkedList.append(data: data)
currentNode = dataLinkedList.getFirstNode()
currentNode = dataLinkedList.getHead()
}

}
Expand All @@ -95,6 +107,10 @@ extension MainVC {
func nextButtonDidTap() {
currentNode = currentNode?.next
}

func prevButtonDidTap() {
currentNode = currentNode?.prev
}
}


Expand All @@ -110,7 +126,7 @@ extension MainVC {
}

private func hierarchy() {
view.addSubviews(profileLabel, nextButton)
view.addSubviews(profileLabel, prevButton, nextButton)
}

private func layout() {
Expand All @@ -119,7 +135,14 @@ extension MainVC {
}

nextButton.snp.makeConstraints {
$0.centerX.equalToSuperview()
$0.centerX.equalToSuperview().offset(50)
$0.top.equalTo(profileLabel.snp.bottom).offset(100)
$0.height.equalTo(40)
$0.width.equalTo(80)
}

prevButton.snp.makeConstraints {
$0.centerX.equalToSuperview().offset(-50)
$0.top.equalTo(profileLabel.snp.bottom).offset(100)
$0.height.equalTo(40)
$0.width.equalTo(80)
Expand Down
20 changes: 10 additions & 10 deletions SOPTving/SOPTving/Utility/DataStructure/LinkedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@
import Foundation

final class LinkedList<T> {

private var head: Node<T>?
private var tail: Node<T>?

var isEmpty: Bool {
return head == nil
}

func append(data: T?) {
func append(data: T) {
let newNode = Node(data)

if isEmpty {
head = newNode
tail = newNode
tail?.next = head
} else {
tail?.next = newNode
newNode.prev = tail
tail = newNode
tail?.next = head
}

head?.prev = tail
tail?.next = head
}

// append 인자에 배열을 넣었을 경우, append(T)를 반복한다.
func append(data: [T]?) {
guard let data else { return }
for t in data {
append(data: t)
func append(data: [T]) {
for d in data {
append(data: d)
}
}

func getFirstNode() -> Node<T>? {
func getHead() -> Node<T>? {
return head
}



}
6 changes: 4 additions & 2 deletions SOPTving/SOPTving/Utility/DataStructure/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import Foundation

final class Node<T> {
var data: T?
var data: T
var prev: Node?
var next: Node?

init(_ data: T?, next: Node? = nil) {
init(_ data: T, prev: Node? = nil, next: Node? = nil) {
self.data = data
self.prev = prev
self.next = next
}
}

0 comments on commit 4d3a5d0

Please sign in to comment.