Created
February 11, 2021 04:40
-
-
Save crissilvaeng/efc1ebc36d08127887d4ae5b95bc52bf to your computer and use it in GitHub Desktop.
DoublyLinkedList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import islice | |
class Node: | |
def __init__(self, data, previous=None, next=None): | |
self.data = data | |
self.previous = None | |
self.next = None | |
def __eq__(self, other): | |
if isinstance(other, self.data.__class__): | |
return self.data == other | |
else: | |
return False | |
def __str__(self): | |
return str(self.data) | |
class DoublyLinkedList: | |
def __init__(self): | |
self.head = None | |
def __iter__(self): | |
current = self.head | |
while current: | |
yield current | |
current = current.next | |
if current is self.head or not current: | |
return | |
def __len__(self): | |
count = 0 | |
for node in self: | |
count += 1 | |
return count | |
def __getitem__(self, index): | |
generator = (node for node in self) | |
return next(islice(generator, index, None)) | |
def add_head(self, data): | |
self.add_tail(data) | |
if self.head.previous: | |
self.head = self.head.previous | |
def add_tail(self, data): | |
current = Node(data) | |
if not self.head: | |
self.head = Node(data) | |
self.arrange(current, current, current) | |
elif not self.head.next: | |
self.arrange(self.head, current, self.head) | |
else: | |
self.arrange(self.head.previous, current, self.head) | |
def arrange(self, previous, current, next): | |
previous.next = current | |
current.previous = previous | |
current.next = next | |
next.previous = current | |
def remove_head(self): | |
self.head = self.head.next | |
self.remove_tail() | |
def remove_tail(self): | |
self.remove(self.head.previous) | |
def remove(self, current): | |
previous = current.previous | |
next = current.next | |
previous.next = next | |
next.previous = previous | |
def search(self, data): | |
for node in self: | |
if node == data: | |
return node | |
def reverse(self): | |
current = self.head.previous | |
while current: | |
yield current | |
current = current.previous | |
if current is self.head.previous or not current: | |
return | |
def left(self): | |
return str([str(node) for node in self.reverse()]) | |
def __str__(self): | |
return str([str(node) for node in self]) | |
if __name__ == '__main__': | |
numbers = DoublyLinkedList() | |
numbers.add_tail(20) | |
print(numbers) # ['20'] | |
numbers.add_tail(30) | |
print(numbers) # ['20', '30'] | |
numbers.add_head(10) | |
print(numbers) # ['10', '20', '30'] | |
numbers.remove_head() | |
print(numbers) # ['20', '30'] | |
numbers.add_tail(40) # ['20', '30', '40'] | |
print(numbers) | |
numbers.remove_tail() | |
print(numbers) # ['20', '30'] | |
print(numbers.left()) # ['30', '20'] | |
print(numbers.search(10)) # None | |
print(numbers.search(20)) # 20 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment