LinkedList- (2) DoublyLinkedList with python
연결리스트
단일 연결리스트는 앞서 (1) SingleLinkedList 에서 다뤄봤고 이번에는 전방 탐색도 가능한 DoublyLinkedList 이중연결리스트를 정리했다.
(2) DoublyLinkedList
from typing import Any, Type
class Node:
def __init__(self, data: Any, prev = None, next = None) -> None:
self.data = data
self.prev = prev
self.next = next
class DoublyLinkedList:
def __init__(self) -> None:
self.no = 0
self.head = self.tail = None
self.cur = None
def __len__(self):
return self.no
def add_first(self, data: Any):
self.no += 1
if self.head is not None:
ptr = self.head
self.head = self.cur = Node(data)
self.head.next = ptr
ptr.prev = self.head
else:
self.head = Node(data)
self.tail = self.head
def add_last(self, data: Any):
self.no += 1
if self.tail is not None:
ptr = self.tail
self.tail = self.cur = Node(data)
self.tail.prev = ptr
ptr.next = self.tail
else:
self.tail = Node(data)
self.head = self.tail
def remove_first(self):
if self.head is None:
return False
else:
self.head = self.cur = self.head.next
self.head.prev = None
self.no -= 1
return True
def remove_last(self):
if self.tail is None:
return False
else:
self.tail = self.cur = self.tail.prev
self.tail.next = None
self.no -= 1
return True
def print(self):
ptr = self.head
while ptr is not None:
print(f'{ptr.data} ->')
ptr = ptr.next
def reverse_print(self):
ptr = self.tail
while ptr is not None:
print(f'{ptr.data} ->')
ptr = ptr.prev
dlst = DoublyLinkedList()
dlst.add_first(2)
dlst.add_first(3)
dlst.add_first(4)
dlst.add_first(5)
dlst.add_last(6)
dlst.add_last(7)
dlst.add_last(8)
dlst.add_last(9)
dlst.remove_first()
dlst.remove_last()
dlst.reverse_print()
print()
dlst.print()
print()
print(len(dlst))
8 ->
7 ->
6 ->
2 ->
3 ->
4 ->
4 ->
3 ->
2 ->
6 ->
7 ->
8 ->
6
Leave a comment