diff --git a/Sprint-2/.gitignore b/Sprint-2/.gitignore new file mode 100644 index 0000000..0cafc1c --- /dev/null +++ b/Sprint-2/.gitignore @@ -0,0 +1 @@ +.venv/ \ No newline at end of file diff --git a/Sprint-2/implement_linked_list/README.md b/Sprint-2/implement_linked_list/README.md index 50c44a8..bec923c 100644 --- a/Sprint-2/implement_linked_list/README.md +++ b/Sprint-2/implement_linked_list/README.md @@ -9,3 +9,8 @@ It should support the following operations. Each operation should have a O(1) wo * `remove` takes a handle from `push_head`, and removes that element from the list. There are some tests in `linked_list_test.py` for your implementation - feel free to write more. + +* A branch was later created so that a PR can be pushed. + + +* Make a new branch for implementing linked list in python diff --git a/Sprint-2/implement_linked_list/linked_list.py b/Sprint-2/implement_linked_list/linked_list.py index e69de29..84a354a 100644 --- a/Sprint-2/implement_linked_list/linked_list.py +++ b/Sprint-2/implement_linked_list/linked_list.py @@ -0,0 +1,64 @@ +class Node: + def __init__(self, data): + self.data = data + self.previous = None + self.next = None + +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def push_head(self, data): + new_head_node = Node(data) + if self.head is not None: + new_head_node.next = self.head + self.head.previous = new_head_node + + self.head = new_head_node + if self.tail is None: + self.tail = new_head_node + + return new_head_node + + + + def pop_tail(self): + if self.tail is not None: + tail_node = self.tail + previous = self.tail.previous + self.tail = previous + if self.tail is not None: + self.tail.next = None + else: + self.head = None + else: + raise IndexError("Unable to remove from empty linked list") + + return tail_node.data + + + + def remove(self, node): + if node.previous is not None: + node.previous.next = node.next + else: + self.head = node.next + + if node.next is not None: + node.next.previous = node.previous + else: + self.tail = node.previous + + + + + + + + + + + + +