From 2e54707bcac377b11407898436a49f7169c6777f Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 1 Oct 2020 13:08:17 +0530 Subject: [PATCH 01/12] Update doubly_linked_list.py Update the implementation of linked list made changes for optimizing added more helper methods used new variable names and function names --- .../linked_list/doubly_linked_list.py | 180 ++++++++++-------- 1 file changed, 100 insertions(+), 80 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 1b4005f59fae..2476e3e857b4 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -6,21 +6,30 @@ - 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 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: """ >>> 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 + >>> linked_list.is_empty == True + True + >>> linked_list.insert(0) + >>> linked_list.is_empty == False + True + >>> linked_list.delete_value(0) + >>> linked_list.is_empty == True True """ @@ -32,89 +41,100 @@ 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) + 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 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): + node = Node(value) + if self.head is None: + self.set_head(node) + else: + self.set_tail(node) - 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 + def insert_before_node(self, node: Node, node_to_insert: Node) -> None: + node_to_insert.next = node + node_to_insert.previous = node.previous - return tail_data + if node.previous is None: + self.head = node_to_insert + else: + node.previous.next = node_to_insert - def delete(self, data) -> str: - current = self.head + node.previous = node_to_insert - 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_after_node(self, node: Node, node_to_insert: Node) -> None: + node_to_insert.previous = node + node_to_insert.next = node.next - if current == self.head: - self.delete_head() + if node.next is None: + self.tail = node_to_insert + else: + node.next.previous = node_to_insert - elif current == self.tail: - self.delete_tail() + node.next = 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_at_position(self, position: int, value: int) -> None: + if position == 1: + self.insert(value) + 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) - @property - def is_empty(self): # return True if the list is empty - return self.head is None + 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) -class Node: - def __init__(self, data): - self.data = data - self.previous = None - self.next = None + if node is not None: + if node == self.head: + self.head = self.head.next - def __str__(self): - return f"{self.data}" + 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 + + + @property + def is_empty(self): + # return True if the list is empty + return self.head is None From 44e105fe5f0c9f2a19ef2ec60ac322e63cb57c7c Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 1 Oct 2020 13:15:23 +0530 Subject: [PATCH 02/12] Update doubly_linked_list.py removed comments --- data_structures/linked_list/doubly_linked_list.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 2476e3e857b4..1b6b7e9a7533 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -135,6 +135,5 @@ def remove_node_pointers(node: Node) -> None: @property - def is_empty(self): - # return True if the list is empty + def is_empty(self): return self.head is None From efc1a117e8cc14e21d3ee7b1c16c45aa4653bc10 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 1 Oct 2020 14:05:46 +0530 Subject: [PATCH 03/12] Update doubly_linked_list.py --- data_structures/linked_list/doubly_linked_list.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 1b6b7e9a7533..518c3c0b3747 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -137,3 +137,5 @@ def remove_node_pointers(node: Node) -> None: @property def is_empty(self): return self.head is None + + From 8929b5be7d97834f5004b1190394f2a8e56a5aaf Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 1 Oct 2020 14:19:50 +0530 Subject: [PATCH 04/12] Update doubly_linked_list.py --- data_structures/linked_list/doubly_linked_list.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 518c3c0b3747..ad32f3954b27 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -23,13 +23,13 @@ def __str__(self): class LinkedList: """ >>> linked_list = LinkedList() - >>> linked_list.is_empty == True + >>> linked_list.is_empty() == True True >>> linked_list.insert(0) - >>> linked_list.is_empty == False + >>> linked_list.is_empty() == False True >>> linked_list.delete_value(0) - >>> linked_list.is_empty == True + >>> linked_list.is_empty() == True True """ @@ -134,8 +134,7 @@ def remove_node_pointers(node: Node) -> None: node.previous = None - @property - def is_empty(self): + def is_empty(self): return self.head is None From e934b9a0e7297f47e10c934e9ff888aed789f3ae Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 1 Oct 2020 18:26:44 +0530 Subject: [PATCH 05/12] Update doubly_linked_list.py Updated the complete linked list implementation --- .../linked_list/doubly_linked_list.py | 51 ++++++++++++------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index ad32f3954b27..d13e6130714c 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -21,18 +21,6 @@ def __str__(self): class LinkedList: - """ - >>> linked_list = LinkedList() - >>> linked_list.is_empty() == True - True - >>> linked_list.insert(0) - >>> linked_list.is_empty() == False - True - >>> linked_list.delete_value(0) - >>> linked_list.is_empty() == True - True - """ - def __init__(self): self.head = None # First node in list self.tail = None # Last node in list @@ -89,8 +77,6 @@ def insert_after_node(self, node: Node, node_to_insert: Node) -> None: node.next = node_to_insert def insert_at_position(self, position: int, value: int) -> None: - if position == 1: - self.insert(value) current_position = 1 new_node = Node(value) node = self.head @@ -133,8 +119,39 @@ def remove_node_pointers(node: Node) -> None: node.next = None node.previous = None - def is_empty(self): return self.head is None - - + + +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 From 69180f7c5778c098ba2754aecad696eff10f8231 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 07:25:11 +0530 Subject: [PATCH 06/12] Update the doubly lnked list file New Implementation Added in new doc test cases as per new implementation. --- .../linked_list/doubly_linked_list.py | 154 ++++++++++++++---- 1 file changed, 121 insertions(+), 33 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index d13e6130714c..2dc278cfd04b 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -11,10 +11,10 @@ class Node: - def __init__(self, data, previous=None, next=None): + def __init__(self, data, previous=None, next_node=None): self.data = data self.previous = previous - self.next = next + self.next = next_node def __str__(self): return f"{self.data}" @@ -33,6 +33,24 @@ def __str__(self): current = current.next return "<-->".join(str(node) for node in nodes) + def __contains__(self, value): + current = self.head + while current: + if current.data == value: + return True + current = current.next + return False + + def get_head_data(self): + if self.head: + return self.head.data + return None + + def get_tail_data(self): + if self.tail: + return self.tail.data + return None + def set_head(self, node: Node) -> None: if self.head is None: @@ -107,6 +125,8 @@ def delete_value(self, value): self.tail = self.tail.previous self.remove_node_pointers(node) + else: + return 'Node not found' @staticmethod def remove_node_pointers(node: Node) -> None: @@ -123,35 +143,103 @@ def is_empty(self): return self.head is None -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): +def create_linked_list(): + """ + >>> new_linked_list = LinkedList() + >>> new_linked_list.get_head_data() is None + True + + >>> 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 + + >>> new_linked_list.is_empty() + False + + >>> 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=5000) + 'Node not found' + + >>> 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 + """ + linked_list = LinkedList() + for i in range(10): + linked_list.insert(value=i) + + print(linked_list) + # 0<-->1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 + print(linked_list.head) + # 0 + linked_list.delete_value(value=0) + print(linked_list.head) + # 1 + print(linked_list) + # 1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 + linked_list.insert_at_position(position=1, value=100) + # 100<-->1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 + print(linked_list) + linked_list.delete_value(value=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 + # 100<-->1<-->2<-->3<-->4<-->6<-->7<-->8<-->9 + print(linked_list.is_empty()) + # False + linked_list.insert_at_position(position=12, value=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 + + +if __name__ == '__main__': + import doctest + + doctest.testmod() From ad17f04b69a52bd936d1df5ffe3c7548c4843563 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 07:50:30 +0530 Subject: [PATCH 07/12] New DLL implementation Fixed pre commit fails for PR Added in more test cases deleted the unwanted comments --- .../linked_list/doubly_linked_list.py | 50 +++++++------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 2dc278cfd04b..e2c42c1e0b81 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -204,39 +204,25 @@ def create_linked_list(): 20 >>> new_linked_list.get_head_data() 20 + + >>> new_linked_list_2 = LinkedList() + >>> for value in range(1,10): + ... new_linked_list_2.insert(value=value) + >>> print(new_linked_list_2) + 1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 + >>> print(new_linked_list_2.get_head_data()) + 1 + >>> new_linked_list_2.delete_value(value=0) + 'Node not found' + >>> print(new_linked_list_2) + 1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 + >>> new_linked_list_2.insert_at_position(position=1, value=100) + >>> print(new_linked_list_2) + 100<-->1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 + >>> new_linked_list_2.delete_value(value=5) + >>> print(new_linked_list_2) + 100<-->1<-->2<-->3<-->4<-->6<-->7<-->8<-->9 """ - linked_list = LinkedList() - for i in range(10): - linked_list.insert(value=i) - - print(linked_list) - # 0<-->1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 - print(linked_list.head) - # 0 - linked_list.delete_value(value=0) - print(linked_list.head) - # 1 - print(linked_list) - # 1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 - linked_list.insert_at_position(position=1, value=100) - # 100<-->1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 - print(linked_list) - linked_list.delete_value(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(position=12, value=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 if __name__ == '__main__': From e5d25e4280f17d94b5cdf6909396a2b85fd4259e Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 07:54:09 +0530 Subject: [PATCH 08/12] Update doubly_linked_list.py updated the quotes(pre-commit fail) --- data_structures/linked_list/doubly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index e2c42c1e0b81..55f88d3f0f25 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -225,7 +225,7 @@ def create_linked_list(): """ -if __name__ == '__main__': +if __name__ == "__main__": import doctest doctest.testmod() From 0a12a82fca53845c4c7519fd4d08fda758079743 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 07:58:34 +0530 Subject: [PATCH 09/12] Update doubly_linked_list.py Update quotes to fix pre-commit fails --- data_structures/linked_list/doubly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 55f88d3f0f25..5a5b9d034ebe 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -126,7 +126,7 @@ def delete_value(self, value): self.remove_node_pointers(node) else: - return 'Node not found' + return "Node not found" @staticmethod def remove_node_pointers(node: Node) -> None: From 2d947c1f95fed1e7f5c4ce98cbde67f4524c1a2d Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 14:48:56 +0530 Subject: [PATCH 10/12] Updated doubly linked list Update Doubly LinkedList created Linked List iterator class Removed the <--> string earlier used to specify link in the linked list added few more getter methods Added type hints --- .../linked_list/doubly_linked_list.py | 113 +++++++++++------- 1 file changed, 69 insertions(+), 44 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 5a5b9d034ebe..c3f21bb57ef6 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -11,14 +11,39 @@ class Node: - def __init__(self, data, previous=None, next_node=None): + def __init__(self, data: int, previous=None, next_node=None): self.data = data self.previous = previous self.next = next_node - def __str__(self): + 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): @@ -29,26 +54,29 @@ def __str__(self): current = self.head nodes = [] while current is not None: - nodes.append(current.data) - current = current.next - return "<-->".join(str(node) for node in nodes) + nodes.append(current.get_data()) + current = current.get_next() + return " ".join(str(node) for node in nodes) - def __contains__(self, value): + def __contains__(self, value: int): current = self.head while current: - if current.data == value: + if current.get_data() == value: return True - current = current.next + current = current.get_next() return False + def __iter__(self): + return LinkedListIterator(self.head) + def get_head_data(self): if self.head: - return self.head.data + return self.head.get_data() return None def get_tail_data(self): if self.tail: - return self.tail.data + return self.tail.get_data() return None def set_head(self, node: Node) -> None: @@ -65,7 +93,7 @@ def set_tail(self, node: Node) -> None: else: self.insert_after_node(self.tail, node) - def insert(self, value): + def insert(self, value: int) -> None: node = Node(value) if self.head is None: self.set_head(node) @@ -76,7 +104,7 @@ def insert_before_node(self, node: Node, node_to_insert: Node) -> None: node_to_insert.next = node node_to_insert.previous = node.previous - if node.previous is None: + if node.get_previous() is None: self.head = node_to_insert else: node.previous.next = node_to_insert @@ -87,7 +115,7 @@ 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.next is None: + if node.get_next() is None: self.tail = node_to_insert else: node.next.previous = node_to_insert @@ -106,34 +134,32 @@ def insert_at_position(self, position: int, value: int) -> None: node = node.next self.insert_after_node(self.tail, new_node) - def get_node(self, item): + def get_node(self, item: int) -> Node: node = self.head while node: - if node.data == item: + if node.get_data() == item: return node - node = node.next - return None + node = node.get_next() + raise Exception("Node not found") def delete_value(self, value): node = self.get_node(value) if node is not None: if node == self.head: - self.head = self.head.next + self.head = self.head.get_next() if node == self.tail: - self.tail = self.tail.previous + self.tail = self.tail.get_previous() self.remove_node_pointers(node) - else: - return "Node not found" @staticmethod def remove_node_pointers(node: Node) -> None: - if node.next: + if node.get_next(): node.next.previous = node.previous - if node.previous: + if node.get_previous(): node.previous.next = node.next node.next = None @@ -143,7 +169,7 @@ def is_empty(self): return self.head is None -def create_linked_list(): +def create_linked_list() -> None: """ >>> new_linked_list = LinkedList() >>> new_linked_list.get_head_data() is None @@ -182,9 +208,22 @@ def create_linked_list(): >>> 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 @@ -192,9 +231,6 @@ def create_linked_list(): >>> 10 in new_linked_list False - >>> new_linked_list.delete_value(value=5000) - 'Node not found' - >>> new_linked_list.delete_value(value=2000) >>> new_linked_list.get_tail_data() 20 @@ -205,23 +241,12 @@ def create_linked_list(): >>> new_linked_list.get_head_data() 20 - >>> new_linked_list_2 = LinkedList() - >>> for value in range(1,10): - ... new_linked_list_2.insert(value=value) - >>> print(new_linked_list_2) - 1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 - >>> print(new_linked_list_2.get_head_data()) - 1 - >>> new_linked_list_2.delete_value(value=0) - 'Node not found' - >>> print(new_linked_list_2) - 1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 - >>> new_linked_list_2.insert_at_position(position=1, value=100) - >>> print(new_linked_list_2) - 100<-->1<-->2<-->3<-->4<-->5<-->6<-->7<-->8<-->9 - >>> new_linked_list_2.delete_value(value=5) - >>> print(new_linked_list_2) - 100<-->1<-->2<-->3<-->4<-->6<-->7<-->8<-->9 + >>> for value in new_linked_list: + ... print(value) + 20 + >>> new_linked_list.delete_value(value=20) + >>> for value in new_linked_list: + ... print(value) """ From f7b2e99a75d1557acb89eb01bf8f02f3b75e89b6 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 17:08:07 +0530 Subject: [PATCH 11/12] Update doubly_linked_list.py Added in more test cases as requested --- data_structures/linked_list/doubly_linked_list.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index c3f21bb57ef6..4328bb249cb9 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -247,6 +247,19 @@ def create_linked_list() -> None: >>> new_linked_list.delete_value(value=20) >>> for value in new_linked_list: ... print(value) + >>> for value in range(1,10): + ... new_linked_list.insert(value=value) + >>> for value in new_linked_list: + ... print(value) + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 """ From 57934bacaea446dd923e019b2105109bc59e2b37 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 18:03:39 +0530 Subject: [PATCH 12/12] Rename doubly_linked_list.py to doubly_linked_list_two.py doubly_linked_list_two --- .../{doubly_linked_list.py => doubly_linked_list_two.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/linked_list/{doubly_linked_list.py => doubly_linked_list_two.py} (100%) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list_two.py similarity index 100% rename from data_structures/linked_list/doubly_linked_list.py rename to data_structures/linked_list/doubly_linked_list_two.py