From 080ec4b006bcc70c7a22300f48e069978bd61764 Mon Sep 17 00:00:00 2001 From: SubCoder1 Date: Sun, 6 Oct 2019 17:13:26 +0530 Subject: [PATCH 1/2] added doubly_linked_list python implementation --- .../Python/doubly_linked_list.py | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Data Structures/Doubly Linked List/Python/doubly_linked_list.py diff --git a/Data Structures/Doubly Linked List/Python/doubly_linked_list.py b/Data Structures/Doubly Linked List/Python/doubly_linked_list.py new file mode 100644 index 00000000..e1974c22 --- /dev/null +++ b/Data Structures/Doubly Linked List/Python/doubly_linked_list.py @@ -0,0 +1,71 @@ +class Node: + def __init__(self, value=None, nextNode=None, prevNode=None): + self.value = value + self.nextNode = nextNode + self.prevNode = prevNode + + def get_next_node(self): + return self.nextNode + + def get_prev_node(self): + return self.prevNode + + def get_node_val(self): + return self.value + +class DoublyLinkedList: + def __init__(self): + self.head = None + self.tail = None + self.chain_len = 0 + + def add_node_to_chain(self, value=None, Front=False): + new_node = Node(value=value) + if Front: + self.head.prevNode = new_node + new_node.nextNode = self.head + self.head = new_node + else: + if self.head is None: + self.head = new_node + self.tail = self.head + else: + self.tail.nextNode = new_node + new_node.prevNode = self.tail + self.tail = new_node + self.chain_len += 1 + + def get_head_node(self): + return self.head + + def get_tail_node(self): + return self.tail + + def get_chain_length(self): + return self.chain_len + + def print_chain(self): + head = self.head + while head: + print(' ',head.value, "->", end='') + head = head.get_next_node() + else: + print(' chain_end') + +if __name__ == '__main__': + my_dl_list = DoublyLinkedList() + + my_dl_list.add_node_to_chain(1) + my_dl_list.add_node_to_chain(2) + my_dl_list.add_node_to_chain(3) + my_dl_list.add_node_to_chain(4) + + print(f"Length of chain -> {my_dl_list.get_chain_length()}") + + my_dl_list.print_chain() + + print('-- Now adding a node to the front of the chain --') + + my_dl_list.add_node_to_chain(5, Front=True) + + my_dl_list.print_chain() \ No newline at end of file From a05dfe5a0a4276d274a06194072bd1de862dfa20 Mon Sep 17 00:00:00 2001 From: SubCoder1 Date: Sun, 6 Oct 2019 17:53:37 +0530 Subject: [PATCH 2/2] added circular_linked_list.py --- .../Python/circular_linked_list.py | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Data Structures/Circular Linked List/Python/circular_linked_list.py diff --git a/Data Structures/Circular Linked List/Python/circular_linked_list.py b/Data Structures/Circular Linked List/Python/circular_linked_list.py new file mode 100644 index 00000000..fd00e9f8 --- /dev/null +++ b/Data Structures/Circular Linked List/Python/circular_linked_list.py @@ -0,0 +1,82 @@ +class Node: + def __init__(self, value=None, nextNode=None, prevNode=None): + self.value = value + self.nextNode = nextNode + self.prevNode = prevNode + + def get_next_node(self): + return self.nextNode + + def get_prev_node(self): + return self.prevNode + + def get_node_val(self): + return self.value + +class CircularLinkedList: + def __init__(self): + self.head = None + self.tail = None + self.chain_len = 0 + + def add_node_to_chain(self, value=None, Front=False): + new_node = Node(value=value) + if Front: + self.head.prevNode = new_node + new_node.nextNode = self.head + self.head = new_node + # To make the linked_list circular, chain's head & tail should be connected + self.head.prevNode = self.tail + self.tail.nextNode = self.head + else: + if self.head is None: + self.head = new_node + self.tail = self.head + else: + self.tail.nextNode = new_node + new_node.prevNode = self.tail + self.tail = new_node + self.tail.nextNode = self.head + self.chain_len += 1 + + def get_head_node(self): + return self.head + + def get_tail_node(self): + return self.tail + + def get_chain_length(self): + return self.chain_len + + def print_chain(self, circular=False): + head = self.head + repeat = 0 + while head: + print(' ',head.get_node_val(), "->", end='') + if head is self.get_tail_node(): + if not repeat: + repeat += 1 + else: + print("end_of_repetition") + break + head = head.get_next_node() + else: + print(' chain_end') + +if __name__ == '__main__': + my_dl_list = CircularLinkedList() + + my_dl_list.add_node_to_chain(1) + my_dl_list.add_node_to_chain(2) + my_dl_list.add_node_to_chain(3) + my_dl_list.add_node_to_chain(4) + + print(f"Length of chain -> {my_dl_list.get_chain_length()}") + + my_dl_list.print_chain() + + print('-- Now adding a node to the front of the chain --') + + my_dl_list.add_node_to_chain(5, Front=True) + + my_dl_list.print_chain() \ No newline at end of file