Python/data_structures/linked_list/doubly_linked_list.py

121 lines
3.4 KiB
Python
Raw Normal View History

2019-10-05 05:14:13 +00:00
"""
- A linked list is similar to an array, it holds values. However, links in a linked
list do not have indexes.
2018-10-19 12:48:28 +00:00
- This is an example of a double ended, doubly linked list.
- Each link references the next link and the previous one.
- A Doubly Linked List (DLL) contains an extra pointer, typically called previous
pointer, together with next pointer and data which are there in singly linked list.
- Advantages over SLL - It can be traversed in both forward and backward direction.
Delete operation is more efficient"""
2018-10-19 12:48:28 +00:00
class LinkedList:
"""
>>> linked_list = LinkedList()
>>> linked_list.insert_at_head("a")
>>> linked_list.insert_at_tail("b")
>>> linked_list.delete_tail()
'b'
>>> linked_list.is_empty
False
>>> linked_list.delete_head()
'a'
>>> linked_list.is_empty
True
"""
2018-10-19 12:48:28 +00:00
def __init__(self):
self.head = None # First node in list
self.tail = None # Last node in list
def __str__(self):
current = self.head
nodes = []
while current is not None:
nodes.append(current)
current = current.next
return " ".join(str(node) for node in nodes)
def insert_at_head(self, data):
new_node = Node(data)
if self.is_empty:
self.tail = new_node
self.head = new_node
else:
self.head.previous = new_node
new_node.next = self.head
self.head = new_node
def delete_head(self) -> str:
if self.is_empty:
return "List is empty"
head_data = self.head.data
if self.head.next:
self.head = self.head.next
self.head.previous = None
else: # If there is no next previous node
self.head = None
self.tail = None
return head_data
def insert_at_tail(self, data):
new_node = Node(data)
if self.is_empty:
self.tail = new_node
self.head = new_node
2018-10-19 12:48:28 +00:00
else:
self.tail.next = new_node
new_node.previous = self.tail
self.tail = new_node
def delete_tail(self) -> str:
if self.is_empty:
return "List is empty"
tail_data = self.tail.data
if self.tail.previous:
self.tail = self.tail.previous
self.tail.next = None
else: # if there is no previous node
self.head = None
self.tail = None
return tail_data
def delete(self, data) -> str:
2018-10-19 12:48:28 +00:00
current = self.head
while current.data != data: # Find the position to delete
if current.next:
current = current.next
else: # We have reached the end an no value matches
return "No data matching given value"
2019-10-05 05:14:13 +00:00
if current == self.head:
self.delete_head()
2019-10-05 05:14:13 +00:00
elif current == self.tail:
self.delete_tail()
2019-10-05 05:14:13 +00:00
else: # Before: 1 <--> 2(current) <--> 3
current.previous.next = current.next # 1 --> 3
current.next.previous = current.previous # 1 <--> 3
return data
@property
def is_empty(self): # return True if the list is empty
2019-10-05 05:14:13 +00:00
return self.head is None
2019-10-05 05:14:13 +00:00
class Node:
def __init__(self, data):
self.data = data
self.previous = None
self.next = None
2019-10-05 05:14:13 +00:00
def __str__(self):
return f"{self.data}"