-
-
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
Closed
Closed
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2e54707
Update doubly_linked_list.py
akashgkrishnan 44e105f
Update doubly_linked_list.py
akashgkrishnan efc1a11
Update doubly_linked_list.py
akashgkrishnan 8929b5b
Update doubly_linked_list.py
akashgkrishnan e934b9a
Update doubly_linked_list.py
akashgkrishnan 69180f7
Update the doubly lnked list file
akashgkrishnan ad17f04
New DLL implementation
akashgkrishnan e5d25e4
Update doubly_linked_list.py
akashgkrishnan 0a12a82
Update doubly_linked_list.py
akashgkrishnan 2d947c1
Updated doubly linked list
akashgkrishnan f7b2e99
Update doubly_linked_list.py
akashgkrishnan 57934ba
Rename doubly_linked_list.py to doubly_linked_list_two.py
akashgkrishnan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,24 +6,46 @@ | |||||
- 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: int, previous=None, next_node=None): | ||||||
self.data = data | ||||||
self.previous = previous | ||||||
self.next = next_node | ||||||
|
||||||
def __str__(self) -> str: | ||||||
return f"{self.data}" | ||||||
|
||||||
def get_data(self) -> int: | ||||||
return self.data | ||||||
|
||||||
def get_next(self): | ||||||
return self.next | ||||||
|
||||||
def get_previous(self): | ||||||
return self.previous | ||||||
|
||||||
|
||||||
class LinkedListIterator: | ||||||
def __init__(self, head): | ||||||
self.current = head | ||||||
|
||||||
def __iter__(self): | ||||||
return self | ||||||
|
||||||
def __next__(self): | ||||||
if not self.current: | ||||||
raise StopIteration | ||||||
else: | ||||||
value = self.current.get_data() | ||||||
self.current = self.current.get_next() | ||||||
return value | ||||||
|
||||||
|
||||||
class LinkedList: | ||||||
def __init__(self): | ||||||
self.head = None # First node in list | ||||||
self.tail = None # Last node in list | ||||||
|
@@ -32,89 +54,203 @@ def __str__(self): | |||||
current = self.head | ||||||
nodes = [] | ||||||
while current is not None: | ||||||
nodes.append(current) | ||||||
current = current.next | ||||||
nodes.append(current.get_data()) | ||||||
current = current.get_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 | ||||||
def __contains__(self, value: int): | ||||||
current = self.head | ||||||
while current: | ||||||
if current.get_data() == value: | ||||||
return True | ||||||
current = current.get_next() | ||||||
return False | ||||||
|
||||||
def __iter__(self): | ||||||
return LinkedListIterator(self.head) | ||||||
|
||||||
def get_head_data(self): | ||||||
if self.head: | ||||||
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.
Suggested change
Same with get_tail_data() |
||||||
return self.head.get_data() | ||||||
return None | ||||||
|
||||||
def get_tail_data(self): | ||||||
if self.tail: | ||||||
return self.tail.get_data() | ||||||
return None | ||||||
|
||||||
def set_head(self, node: Node) -> None: | ||||||
|
||||||
if self.head is None: | ||||||
self.head = node | ||||||
self.tail = 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 | ||||||
self.insert_before_node(self.head, node) | ||||||
|
||||||
def set_tail(self, node: Node) -> None: | ||||||
if self.head is None: | ||||||
self.set_head(node) | ||||||
else: | ||||||
self.tail.next = new_node | ||||||
new_node.previous = self.tail | ||||||
self.tail = new_node | ||||||
self.insert_after_node(self.tail, node) | ||||||
|
||||||
def delete_tail(self) -> str: | ||||||
if self.is_empty: | ||||||
return "List is empty" | ||||||
def insert(self, value: int) -> None: | ||||||
node = Node(value) | ||||||
if self.head is None: | ||||||
self.set_head(node) | ||||||
else: | ||||||
self.set_tail(node) | ||||||
|
||||||
def insert_before_node(self, node: Node, node_to_insert: Node) -> None: | ||||||
node_to_insert.next = node | ||||||
node_to_insert.previous = node.previous | ||||||
|
||||||
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 node.get_previous() is None: | ||||||
self.head = node_to_insert | ||||||
else: | ||||||
node.previous.next = node_to_insert | ||||||
|
||||||
return tail_data | ||||||
node.previous = node_to_insert | ||||||
|
||||||
def delete(self, data) -> str: | ||||||
current = self.head | ||||||
def insert_after_node(self, node: Node, node_to_insert: Node) -> None: | ||||||
node_to_insert.previous = node | ||||||
node_to_insert.next = node.next | ||||||
|
||||||
if node.get_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) | ||||||
|
||||||
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 get_node(self, item: int) -> Node: | ||||||
node = self.head | ||||||
while node: | ||||||
if node.get_data() == item: | ||||||
return node | ||||||
node = node.get_next() | ||||||
raise Exception("Node not found") | ||||||
|
||||||
if current == self.head: | ||||||
self.delete_head() | ||||||
def delete_value(self, value): | ||||||
node = self.get_node(value) | ||||||
|
||||||
elif current == self.tail: | ||||||
self.delete_tail() | ||||||
if node is not None: | ||||||
if node == self.head: | ||||||
self.head = self.head.get_next() | ||||||
|
||||||
else: # Before: 1 <--> 2(current) <--> 3 | ||||||
current.previous.next = current.next # 1 --> 3 | ||||||
current.next.previous = current.previous # 1 <--> 3 | ||||||
return data | ||||||
if node == self.tail: | ||||||
self.tail = self.tail.get_previous() | ||||||
|
||||||
@property | ||||||
def is_empty(self): # return True if the list is empty | ||||||
self.remove_node_pointers(node) | ||||||
|
||||||
@staticmethod | ||||||
def remove_node_pointers(node: Node) -> None: | ||||||
if node.get_next(): | ||||||
node.next.previous = node.previous | ||||||
|
||||||
if node.get_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 create_linked_list() -> None: | ||||||
""" | ||||||
>>> new_linked_list = LinkedList() | ||||||
>>> new_linked_list.get_head_data() is None | ||||||
True | ||||||
|
||||||
def __str__(self): | ||||||
return f"{self.data}" | ||||||
>>> new_linked_list.get_tail_data() is None | ||||||
True | ||||||
|
||||||
>>> new_linked_list.is_empty() | ||||||
True | ||||||
|
||||||
>>> new_linked_list.insert(10) | ||||||
>>> new_linked_list.get_head_data() | ||||||
10 | ||||||
|
||||||
>>> new_linked_list.get_tail_data() | ||||||
10 | ||||||
|
||||||
>>> new_linked_list.insert_at_position(position=3, value=20) | ||||||
>>> new_linked_list.get_head_data() | ||||||
10 | ||||||
|
||||||
>>> new_linked_list.get_tail_data() | ||||||
20 | ||||||
|
||||||
>>> new_linked_list.set_head(Node(1000)) | ||||||
>>> new_linked_list.get_head_data() | ||||||
1000 | ||||||
|
||||||
>>> new_linked_list.get_tail_data() | ||||||
20 | ||||||
|
||||||
>>> new_linked_list.set_tail(Node(2000)) | ||||||
>>> new_linked_list.get_head_data() | ||||||
1000 | ||||||
>>> new_linked_list.get_tail_data() | ||||||
2000 | ||||||
|
||||||
>>> for value in new_linked_list: | ||||||
... print(value) | ||||||
1000 | ||||||
10 | ||||||
20 | ||||||
2000 | ||||||
>>> new_linked_list.is_empty() | ||||||
False | ||||||
|
||||||
>>> for value in new_linked_list: | ||||||
... print(value) | ||||||
1000 | ||||||
10 | ||||||
20 | ||||||
2000 | ||||||
|
||||||
>>> 10 in new_linked_list | ||||||
True | ||||||
|
||||||
>>> new_linked_list.delete_value(value=10) | ||||||
>>> 10 in new_linked_list | ||||||
False | ||||||
|
||||||
>>> new_linked_list.delete_value(value=2000) | ||||||
>>> new_linked_list.get_tail_data() | ||||||
20 | ||||||
|
||||||
>>> new_linked_list.delete_value(value=1000) | ||||||
>>> new_linked_list.get_tail_data() | ||||||
20 | ||||||
>>> new_linked_list.get_head_data() | ||||||
20 | ||||||
|
||||||
>>> for value in new_linked_list: | ||||||
... print(value) | ||||||
20 | ||||||
>>> new_linked_list.delete_value(value=20) | ||||||
>>> for value in new_linked_list: | ||||||
... print(value) | ||||||
""" | ||||||
|
||||||
|
||||||
if __name__ == "__main__": | ||||||
import doctest | ||||||
|
||||||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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