ํฐ์คํ ๋ฆฌ ๋ทฐ
๐๏ธโ๏ธ ๊ธฐ๋ฐ ๋ค์ง๊ธฐ/์๋ฃ๊ตฌ์กฐ์ ์๊ณ ๋ฆฌ์ฆ
์๋ฃ๊ตฌ์กฐ: Linked List
dirmathfl 2020. 6. 15. 23:41728x90
๋ฐ์ํ
Linked List?
๋จ์ํ ์ ํ ๋ฐฐ์ด(๋ฆฌ์คํธ)์ ์ ์ธํ๋ฉด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐ์์ ์ผ๋ก ํ ๋นํ๋ค. ๋ง์ฝ, ๋ฐฐ์ด ๋ด์ ๊ฐ ์ญ์ ๊ฐ ํ์ํ๊ฑฐ๋ ์ถ๊ฐ๊ฐ ํ์ํ ๊ฒฝ์ฐ ๊ฐ๋ค์ ์๋ฆฌ ๊ต์ฒด๊ฐ ํ์ํ๋ค. ์ด์ ๋ฌ๋ฆฌ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ ์ฐ์์ ์ธ ๊ณต๊ฐ์ ์ฌ์ฉํ์ง ์๊ณ , ๊ฐ๊ฐ์ ๋ ธ๋๋ค์ ์ฐ๊ฒฐํ ๊ตฌ์กฐ๋ก ์ฐ๊ฒฐ๋์ด ์ค๊ฐ์ ๊ฐ ์ญ์ ๊ฐ ๋น๋ฒํ๊ฒ ์ผ์ด๋๋ ์๋ฃ๋ฅผ ๊ด๋ฆฌํ๊ธฐ์ ๋ณด๋ค ํจ์จ์ ์ด๋ค.
Single Linked List
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SingleLinkedList:
def __init__(self, data):
self.head = Node(data)
def add(self, data):
node = self.head
while node.next:
node = node.next
node.next = Node(data)
def delete(self, data):
if self.head is None:
return -1
if self.head.data == data:
temp, self.head = self.head, self.head.next
del temp
else:
node = self.head
while node.next:
if node.next.data == data:
temp, node.next = node.next, node.next.next
del temp
break
else:
node = node.next
def display(self):
node = self.head
while node:
print(node.data)
node = node.next
- add, delete, display ๋ฉ์๋๋ฅผ ๊ตฌํํ์ฌ ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ ํด๋์ค๋ฅผ ๊ตฌํํ์๋ค.
- ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ next(tail)์ ํตํด ๊ฐ ๋ฆฌ์คํธ์ ์ ๊ทผํ ์ ์๋ ๊ตฌ์กฐ์ ํ๊ณ๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
- ์ด์ธ์ ๊ฐ์ ํ์ํ๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ฌ ๋ค์ํ ๋ฐฉ๋ฉด์ผ๋ก ํ์ฉํ ์ ์๋ค.
Double Linked List
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoubleLinkedList:
def __init__(self, data):
self.head = Node(data)
self.tail = self.head
self.num_of_node = 1
def add_first(self, data):
new_node = Node(data)
new_node.next = self.head
self.head.prev = new_node
self.head = new_node
self.num_of_node += 1
def add_last(self, data):
new_node = Node(data)
new_node.prev = self.tail
self.tail.next = new_node
self.tail = new_node
self.num_of_node += 1
def delete(self, data):
if self.head is None:
return False
if self.head.data == data:
temp, self.head = self.head, self.head.next
del temp
node = self.head
while node.next:
if node.next.data == data:
temp, node.next = node.next, node.next.next
node.next.prev = node
del temp
return True
node = node.next
return False
def display(self):
node = self.head
while node:
print(node.data)
node = node.next
def display_rev(self):
node = self.tail
while node:
print(node.data)
node = node.prev
def ins_front(self, n, data):
if not n:
self.add_first(data)
return True
elif n == self.num_of_node - 1:
self.add_last(data)
return True
else:
if self.num_of_node < n:
return False
node = self.head.next
while node.next:
node = node.next
new_node = Node(data)
new_node.next = node.next
node.next.prev = new_node
node.next = new_node
new_node.prev = node
self.num_of_node += 1
return True
def ins_back(self, n, data):
if not n:
self.add_last(data)
return True
elif n == self.num_of_node - 1:
self.add_first(data)
return True
else:
if self.num_of_node < n:
return False
node = self.tail.prev
while node.next:
node = node.prev
new_node = Node(data)
new_node.prev = node.prev
node.next.next = new_node
node.prev = new_node
new_node.next = node
self.num_of_node += 1
return True
- ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋ฌ๋ฆฌ ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ prev, next๋ฅผ ํตํด ์ ๋ฐฉํฅ ์ญ๋ฐฉํฅ์ผ๋ก ์ถ๊ฐ๊ฐ ๊ฐ๋ฅํ๋ค.
- ์ด๋ฅผ ์ํด Node ํด๋์ค์ prev ๋ฉค๋ฒ๋ฅผ ์ถ๊ฐํ๊ณ , head, prev๋ฅผ ํตํด ์ฝ์ ์ญ์ ๊ฐ ๊ฐ๋ฅํ ๋ฉ์๋๋ค์ ์ถ๊ฐํ์ฌ ๊ตฌํํ์๋ค.
728x90
๋ฐ์ํ
'๐๏ธโโ๏ธ ๊ธฐ๋ฐ ๋ค์ง๊ธฐ > ์๋ฃ๊ตฌ์กฐ์ ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๊ณ ๋ฆฌ์ฆ: ์ ๋ ฌ (0) | 2020.06.17 |
---|---|
์๊ณ ๋ฆฌ์ฆ: DFS์ BFS (0) | 2020.06.16 |
์๊ณ ๋ฆฌ์ฆ: Recursion (0) | 2020.06.16 |
์๋ฃ๊ตฌ์กฐ: Queue (0) | 2020.06.15 |
์๋ฃ๊ตฌ์กฐ: Stack (0) | 2020.06.15 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ