diff --git a/Data_Structure/src/Linked Lists/Doubly_Linked_list.py b/Data_Structure/src/Linked Lists/Doubly_Linked_list.py new file mode 100644 index 00000000..0cd890e7 --- /dev/null +++ b/Data_Structure/src/Linked Lists/Doubly_Linked_list.py @@ -0,0 +1,101 @@ +# -*- coding: utf-8 -*- +""" +Created on Thu Oct 3 15:54:30 2019 + +@author: mishr +""" +#doubly Linked List + +class Node: + + def __init__(self, data=None): + + self.data = data + self.next_node = None + self.prev_node = None + +class doubly_linked_list: + + def __init__(self): + self.head = Node() + + def append(self, data): + + new_node = Node(data) + current = self.head + while current.next_node != None: + current = current.next_node + + current.next_node = new_node + new_node.prev_node = current + new_node.next_node = None + + def length(self): + + current = self.head + counter = 0 + while current.next_node != None: + counter += 1 + current = current.next_node + + return counter + + def delete_at(self, index): + if index >= self.length(): + print("Index Out Of bounds") + return None + current_index = 0 + current_node = self.head + while True: + last_node = current_node + previous_node = current_node.prev_node + current_node = current_node.next_node + + if current_index == index: + last_node.next_node = current_node.next_node + last_node.prev_node = previous_node + return + current_index += 1 + + def get_value_at(self, index): + + if index >= self.length(): + print("Index Out Of bounds") + return None + current_index = 0 + current_node = self.head + while True: + current_node = current_node.next_node + if current_index == index: + return current_node.data + current_index += 1 + + def display(self): + + node_list = [] + current = self.head + + while current.next_node is not None: + + current = current.next_node + node_list.append(current.data) + + print(node_list) + +if __name__ == '__main__': + + dll = doubly_linked_list() + + dll.append(1) + dll.append(2) + dll.append(3) + dll.append(4) + dll.append(5) + dll.append(6) + dll.append(7) + dll.append(8) + dll.display() + dll.delete_at(3) + dll.display() + dll.get_value_at(4) + \ No newline at end of file diff --git a/Data_Structure/src/Linked Lists/Singly_Linked_List.py b/Data_Structure/src/Linked Lists/Singly_Linked_List.py new file mode 100644 index 00000000..9376aa27 --- /dev/null +++ b/Data_Structure/src/Linked Lists/Singly_Linked_List.py @@ -0,0 +1,94 @@ +# -*- coding: utf-8 -*- +""" +Created on Thu Oct 3 15:13:10 2019 + +@author: mishr +""" + +#Singly Linked List + +class Node: + + def __init__(self, data=None, next_node=None): + self.data = data + self.next_node = next_node + + + +class singly_linked_list: + def __init__(self): + + self.head = Node() + + def append(self, data): + + new_node = Node(data) + current = self.head + while current.next_node != None: + current = current.next_node + current.next_node = new_node + + def length(self): + + current = self.head + counter = 0 + while current.next_node != None: + counter += 1 + current = current.next_node + + return counter + + def get_value_at(self, index): + + if index >= self.length(): + print("Index Out Of bounds") + return None + current_index = 0 + current_node = self.head + while True: + current_node = current_node.next_node + if current_index == index: + return current_node.data + current_index += 1 + + def delete_at(self, index): + + if index >= self.length(): + print("Index Out Of bounds") + return None + current_index = 0 + current_node = self.head + while True: + last_node = current_node + current_node = current_node.next_node + if current_index == index: + last_node.next_node = current_node.next_node + return + current_index += 1 + + def display(self): + + node_list = [] + current = self.head + while current.next_node != None: + current = current.next_node + node_list.append(current.data) + + print(node_list) + +if __name__ == '__main__': + my_list = singly_linked_list() + my_list.append(0) + my_list.append(1) + my_list.append(2) + my_list.append(3) + my_list.append(4) + my_list.append(5) + my_list.display() + my_list.get_value_at(3) + my_list.delete_at(3) + my_list.display() + #can use loop to take inputs + + + \ No newline at end of file diff --git a/Data_Structure/src/Queue.py b/Data_Structure/src/Queue/Queue.py similarity index 100% rename from Data_Structure/src/Queue.py rename to Data_Structure/src/Queue/Queue.py diff --git a/Data_Structure/src/Queue_Circular.py b/Data_Structure/src/Queue/Queue_Circular.py similarity index 100% rename from Data_Structure/src/Queue_Circular.py rename to Data_Structure/src/Queue/Queue_Circular.py diff --git a/Data_Structure/src/Stack.py b/Data_Structure/src/Stack/Stack.py similarity index 100% rename from Data_Structure/src/Stack.py rename to Data_Structure/src/Stack/Stack.py diff --git a/Data_Structure/src/Stack_Linked_List.py b/Data_Structure/src/Stack/Stack_Linked_List.py similarity index 100% rename from Data_Structure/src/Stack_Linked_List.py rename to Data_Structure/src/Stack/Stack_Linked_List.py diff --git a/Data_Structure/src/Binary_Search_Tree.py b/Data_Structure/src/Trees/Binary_Search_Tree.py similarity index 100% rename from Data_Structure/src/Binary_Search_Tree.py rename to Data_Structure/src/Trees/Binary_Search_Tree.py diff --git a/Data_Structure/src/basic_structure_tree.py b/Data_Structure/src/Trees/basic_structure_tree.py similarity index 100% rename from Data_Structure/src/basic_structure_tree.py rename to Data_Structure/src/Trees/basic_structure_tree.py diff --git a/Data_Structure/src/identical_tree.py b/Data_Structure/src/Trees/identical_tree.py similarity index 100% rename from Data_Structure/src/identical_tree.py rename to Data_Structure/src/Trees/identical_tree.py