연결 리스트

추상적 자료구조 Abstract Dat Structures

: 자료의 내부 구현은 숨겨두고 밖에서 보이는 것들 두 가지를 제공하는 것

배열과 비교한 연결 리스트

배열 연결 리스트
저장 공간 연속한 위치 임의의 위치
특정 원소 지칭 매우 간편 선형탐색과 유사
시간 복잡도 $O(1)$ $O(n)$

자료 구조 정의 (2개의 클래스)

  1. Node

    class Node:
        def __init__(self, item):
            self.data = item
            self.next = None
    

생성자constructor 하나만 해봄

생성자constructor 하나만 해봄

  1. LinkedList

    class LinkedList:
        def __init__(self): # 인자 X
            self.nodeCount = 0 # 초기화
            self.head = None
            self.tail = None
    

연산 정의

1. 특정 원소 참조 (k번째)

# k번째 인자
def getAt(self, pos): # 클래스 LinkedList의 method
    if pos <= 0 or pos > self.nodeCount: # k번째 노드 없음!
        return None
    i = 1
    curr = self.head # 첫번째 노드
    while i < pos:
        curr = curr.next
        i += 1
    return curr

2. 리스트 순회