-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Update doubly_linked_list.py #2573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
2e54707
44e105f
efc1a11
8929b5b
e934b9a
69180f7
ad17f04
e5d25e4
0a12a82
2d947c1
f7b2e99
57934ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,24 +6,21 @@ | |
- 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""" | ||
Delete operation is more efficient | ||
""" | ||
|
||
|
||
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 | ||
""" | ||
class Node: | ||
def __init__(self, data, previous=None, next=None): | ||
self.data = data | ||
self.previous = previous | ||
self.next = next | ||
|
||
def __str__(self): | ||
return f"{self.data}" | ||
|
||
|
||
class LinkedList: | ||
def __init__(self): | ||
self.head = None # First node in list | ||
self.tail = None # Last node in list | ||
|
@@ -32,89 +29,129 @@ def __str__(self): | |
current = self.head | ||
nodes = [] | ||
while current is not None: | ||
nodes.append(current) | ||
nodes.append(current.data) | ||
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 | ||
else: | ||
self.tail.next = new_node | ||
new_node.previous = self.tail | ||
self.tail = new_node | ||
return "<-->".join(str(node) for node in nodes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find the "<—>" notation to be a lot of visual clutter. Why not just add an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi sir, However, I have left the str(self) same and just removed the <--> from the string as I had previously used the <--> representation as I thought it would make the links between nodes more clear @cclauss I have made the changes |
||
|
||
def delete_tail(self) -> str: | ||
if self.is_empty: | ||
return "List is empty" | ||
def set_head(self, node: Node) -> None: | ||
|
||
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 | ||
if self.head is None: | ||
self.head = node | ||
self.tail = node | ||
else: | ||
self.insert_before_node(self.head, node) | ||
|
||
return tail_data | ||
def set_tail(self, node: Node) -> None: | ||
if self.head is None: | ||
self.set_head(node) | ||
else: | ||
self.insert_after_node(self.tail, node) | ||
|
||
def delete(self, data) -> str: | ||
current = self.head | ||
def insert(self, value): | ||
node = Node(value) | ||
if self.head is None: | ||
self.set_head(node) | ||
else: | ||
self.set_tail(node) | ||
|
||
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" | ||
def insert_before_node(self, node: Node, node_to_insert: Node) -> None: | ||
node_to_insert.next = node | ||
node_to_insert.previous = node.previous | ||
|
||
if current == self.head: | ||
self.delete_head() | ||
if node.previous is None: | ||
self.head = node_to_insert | ||
else: | ||
node.previous.next = node_to_insert | ||
|
||
elif current == self.tail: | ||
self.delete_tail() | ||
node.previous = node_to_insert | ||
|
||
else: # Before: 1 <--> 2(current) <--> 3 | ||
current.previous.next = current.next # 1 --> 3 | ||
current.next.previous = current.previous # 1 <--> 3 | ||
return data | ||
def insert_after_node(self, node: Node, node_to_insert: Node) -> None: | ||
node_to_insert.previous = node | ||
node_to_insert.next = node.next | ||
|
||
@property | ||
def is_empty(self): # return True if the list is empty | ||
if node.next is None: | ||
self.tail = node_to_insert | ||
else: | ||
node.next.previous = node_to_insert | ||
|
||
node.next = node_to_insert | ||
|
||
def insert_at_position(self, position: int, value: int) -> None: | ||
current_position = 1 | ||
new_node = Node(value) | ||
node = self.head | ||
while node: | ||
if current_position == position: | ||
self.insert_before_node(node, new_node) | ||
return None | ||
current_position += 1 | ||
node = node.next | ||
self.insert_after_node(self.tail, new_node) | ||
|
||
def get_node(self, item): | ||
node = self.head | ||
while node: | ||
if node.data == item: | ||
return node | ||
node = node.next | ||
return None | ||
|
||
def delete_value(self, value): | ||
node = self.get_node(value) | ||
|
||
if node is not None: | ||
if node == self.head: | ||
self.head = self.head.next | ||
|
||
if node == self.tail: | ||
self.tail = self.tail.previous | ||
|
||
self.remove_node_pointers(node) | ||
|
||
@staticmethod | ||
def remove_node_pointers(node: Node) -> None: | ||
if node.next: | ||
node.next.previous = node.previous | ||
|
||
if node.previous: | ||
node.previous.next = node.next | ||
|
||
node.next = None | ||
node.previous = None | ||
|
||
def is_empty(self): | ||
return self.head is None | ||
|
||
|
||
class Node: | ||
def __init__(self, data): | ||
self.data = data | ||
self.previous = None | ||
self.next = None | ||
|
||
def __str__(self): | ||
return f"{self.data}" | ||
linked_list = LinkedList() | ||
for i in range(10): | ||
linked_list.insert(i) | ||
|
||
print(linked_list) | ||
# 0<-->1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 | ||
print(linked_list.head) | ||
# 0 | ||
linked_list.delete_value(0) | ||
print(linked_list.head) | ||
# 1 | ||
print(linked_list) | ||
# 1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 | ||
linked_list.insert_at_position(1, 100) | ||
# 100<-->1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 | ||
print(linked_list) | ||
linked_list.delete_value(5) | ||
print(linked_list) | ||
# 100<-->1<-->2<-->3<-->4<-->6<-->7<-->8<-->9 | ||
print(linked_list.is_empty()) | ||
# False | ||
linked_list.insert_at_position(12, 200) | ||
|
||
for i in range(5): | ||
print(linked_list) | ||
linked_list.delete_value(linked_list.tail.data) | ||
# for each iterations | ||
# 100 < -->1 < -->2 < -->3 < -->4 < -->6 < -->7 < -->8 < -->9 < -->200 | ||
# 100 < -->1 < -->2 < -->3 < -->4 < -->6 < -->7 < -->8 < -->9 | ||
# 100 < -->1 < -->2 < -->3 < -->4 < -->6 < -->7 < -->8 | ||
# 100 < -->1 < -->2 < -->3 < -->4 < -->6 < -->7 | ||
# 100 < -->1 < -->2 < -->3 < -->4 < -->6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove all these doctests?!?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cclauss I had removed the previous doc test as I had completely changed the implementation. Apologies for that
I have updated the doc test with a set of new test cases and added in much more cases
Kindly review the new changes submitted for the Dubly linked List
Some functions are intentionally made of O(N) time complexity to provide a more user-friendly output on the console.
besides most functions are working in O(1) time complexity
Kindly update in case of furthermore changes are required