[알고리즘] 패스트캠퍼스 챌린지 05일차

[알고리즘] 패스트캠퍼스 챌린지 05일차

계속해서 링크드리스트 강좌.

링크드리스트 장단점

장점 미리 데이터 공간을 미리 할당하지 않아도 됨 배열은 미리 데이터 공간을 할당 해야 함

단점 연결을 위한 별도 데이터 공간이 필요하므로, 저장공간 효율이 높지 않음 연결 정보를 찾는 시간이 필요하므로 접근 속도가 느림 중간 데이터 삭제시, 앞뒤 데이터의 연결을 재구성해야 하는 부가적인 작업 필요

링크드 리스트 데이터 사이에 추가하기

어제 했던 것에 이어서 링크드리스트(Node(1) ~ Node(10)) 사이에 새로운 데이터를 추가해보쟈.

Node(1) Node(2) 사이에 Node(1.5)를 넣는다고 했을 때

Node(1)이 나올 때 까지 링크드리스트 데이터를 검색한다.

Node(1)의 포인터가 Node(1.5)를 가리키도록 한다.

Node(1.5)의 포인터가 Node(2)를 가리키도록 한다.

node = head search = True while search: if node.data == 1: search = False else: node = node.next node_next = node.next node.next = node3 node3.next = node_next // 확인용 프린트 while node.next: print(node.data) node = node.next print (node.data)

위의 것을 Swift로 바꿔봤다.

오늘은 링크드리스트 클래스를 만들어서 테스트해봤다.

// insert Node(1.5) to between Node(1) and Node(2) linkedList.node = firstNode var search: Bool = true while(search) { if linkedList.node?.data == 1.0 { // find! stop searching search = false } else { // continue searching linkedList.node? = (linkedList.node?.next)! } } // stop searching // insert Node(1.5) // current node is Node(1) let insertNode = Node(1.5) let tempNode = linkedList.node?.next // it must be Node(2) linkedList.node?.next = insertNode insertNode.next = tempNode // print linked list from first to the end linkedList.node = firstNode while(linkedList.node?.next != nil) { print(linkedList.node?.data) linkedList.node = linkedList.node?.next } print(linkedList.node?.data)

링크드리스트에서 데이터 삭제

삭제하는 경우 링크드리스트의 헤드(첫번째 노드)를 삭제하는 경우 링크드리스트의 마지막 노드를 삭제하는 경우 링크드리스트의 중간 노드를 삭제하는 경우

강의 들으면서 삭제하는 것 구현 했다.

이전에 맨 뒤에 노드 추가하는 함수와 링크드 리스트 처음부터 끝까지 프린트 하는 함수, 노드 삭제하는 함수를 만들었다.

링크드리스트 클래스를 만들어서 각 기능 함수 만들고, 테스트도 해봤다!!

class Node { var data: DataType? var next: Node? init(_ data: DataType, next: Node? = nil) { self.data = data self.next = next } } class LinkedList { // first of Linked List var head: Node? // current Node of Linked List var node: Node? // add data to Linked List func add(data: DataType) { node = head while node?.next != nil { node = node?.next } node?.next = Node(data) } // print data from start to end func desc() { // print linked list from first to the end var node = head while(node != nil) { print(node?.data) node = node?.next ?? nil } } // delete node func delete(data: DataType){ guard self.head != nil else { // no head print("no head!!") return } if let head = self.head { if head.data == data { // data to delete == head data if let temp = head.data { self.head = self.head?.next return } } else { var node = head while node.next != nil { if node.next?.data == data { // data to delete == at lest second data(next to head) if let temp = node.next?.data { node.next = node.next?.next return } } else { node = node.next! } } } } } // 연습문제 추가 - 특정 데이터로 해당하는 노드 찾기. // search node with data func searchNode(data: DataType) -> Node? { var node = head while(node != nil) { if node?.data == data { return node! } else { node = node?.next } } print("No exact Node with input Data") return nil } }

링크드리스트 데이터 삭제하고 출력해서 확인해보기까지 Swift로 성공~

연습문제로 특정 데이터로 노드 찾는 문제가 있었는데 직접 구현하고 테스트도 해봤다.

링크드리스트를 전체 출력하는 것을 참고해서 구현했다.

// make linkedList 0 ~ 10 let linkedList = LinkedList() let firstNode = Node(0.0) linkedList.head = firstNode for index in 1...10 { linkedList.add(data: Double(index)) } // 노드 삭제 출력 테스트 linkedList.delete(data: 1.0) linkedList.delete(data: 4.0) linkedList.delete(data: 10.0) linkedList.desc() // 연습문제 - 특정 데이터를 가진 노드 찾기 출력 테스트 print(linkedList.searchNode(data: 2.0)) print(linkedList.searchNode(data: 44.0))

오늘의 강의 인증샷. 강의 들으면서 Playground에서 Swift로 직접 짜보면서 공부했다.

오늘도 계속해서 링크드리스트 강의 들었다.

링크드리스트 강의 아직 하나 더 남았는데..

강의를 들을 때마다 대학교 다닐때, 그리고 여태 2년동안 일하면서 공부해야지 해야지만 하고 미뤘던게 너무너무 후회된다.

https://bit.ly/37BpXiC

본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성되었습니다.

from http://tnqtnq.tistory.com/82 by ccl(A) rewrite - 2021-09-10 17:00:33