이중 연결 리스트의 특징
연결 리스트의 탐색 기능을 개선한 자료구조
•
더블 링크드 리스트는 자신의 앞 Node 를 가리키는 포인터도 포함하고 있음 (앞뒤 이동가능)
class Node:
def __init__(self, _data: Any = None):
self.data = _data
self.next = None
self.prev = None
def __str__(self):
return "Data: " + str(self.data) + ", Next-> \n" + str(self.next)
class LinkedList:
def __init__(self):
self.head: Node = None
self.tail: Node = None
def insert(self, _data: Any):
head = self.head
new_node = Node(_data)
if head is None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
new_node.prev = self.tail
self.tail = self.tail.next
def headTraval(self):
head = self.head
if head is None:
return
while head is not None:
print(head.data)
head = head.next
def tailTraval(self):
tail = self.tail
if tail is None:
return
while tail is not None:
print(tail.data)
tail = tail.prev
def pop(self):
tail = self.tail
if tail is None:
return None
self.tail = self.tail.prev
self.tail.next = None
return tail.data
Python