Linked List는 Array List와는 다르게 엘리먼트와 엘리먼트 간의 연결을 이용해서 리스트를 구현한 것을 의미합니다.
Array와 trade off의 관계를 갖는다.
즉, 어떤 특성이 좋아지면 다른 특성이 나뻐지는 상황을 의미
생활코딩 참조
Python으로 linked list(단방향 연결) 클래스로 메소드를 구현해보았다.
class Node:
def __init__(self, item):
self.val = item
self.next = None
class LinkedList:
def __init__(self, item):
self.head = Node(item)
# 추가 메소드
def add(self, item):
cur = self.head
while cur.next is not None:
cur = cur.next
cur.next = Node(item)
# 삭제 메소드
def remove(self, item):
if self.head.val == item:
self.head = self.head.next
else:
cur = self.head
while cur.next is not None:
if cur.val == item:
self.removeItem(item)
return
cur = cur.next
print("item does not exist in linked list")
def removeItem(self, item):
cur = self.head
while cur.next is not None:
if cur.next.val == item:
nextnode = cur.next.next
cur.next = nextnode
break
# 출력 메소드
def printlist(self):
cur = self.head
print("HEAD:: ", end='')
while cur is not None:
print(cur.val, '->', end=' ')
cur = cur.next
if cur is None:
print('empty')
# linked list size 출력 메소드
def size(self):
cur = self.head
count = 0
while cur:
count += 1
cur = cur.next
return count
# 탐색 메소드
def search(self, data):
check = self.head
for i in range(self.size()):
if check.val == data:
print(data, "The data is at the {} index.".format(i+1))
return None
check = check.next
print(data, "Data does not exist in linked list")
return None
linked = LinkedList(2)
linked.add(3)
linked.add(4)
linked.add(5)
linked.printlist()
linked.remove(3)
linked.printlist()
linked.search(5)
print(linked.size())
# HEAD:: 2 -> 3 -> 4 -> 5 -> empty
# HEAD:: 2 -> 4 -> 5 -> empty
# 5 The data is at the 3 index.
# 3