From 7376addcd5f786a3c46e48e5600cf88d363015c3 Mon Sep 17 00:00:00 2001 From: Mariusz Skoneczko Date: Fri, 18 Oct 2019 17:38:31 +1100 Subject: [PATCH] Implement Linked Queue and Linked Stack data structures (#1324) * Add LinkedQueue * Add LinkedStack --- data_structures/queue/linked_queue.py | 74 ++++++++++++++++++++++++++ data_structures/stacks/linked_stack.py | 67 +++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 data_structures/queue/linked_queue.py create mode 100644 data_structures/stacks/linked_stack.py diff --git a/data_structures/queue/linked_queue.py b/data_structures/queue/linked_queue.py new file mode 100644 index 000000000..614c60cd1 --- /dev/null +++ b/data_structures/queue/linked_queue.py @@ -0,0 +1,74 @@ +""" A Queue using a Linked List like structure """ +from typing import Any, Optional + + +class Node: + def __init__(self, data: Any, next: Optional["Node"] = None): + self.data: Any = data + self.next: Optional["Node"] = next + + +class LinkedQueue: + """ + Linked List Queue implementing put (to end of queue), + get (from front of queue) and is_empty + + >>> queue = LinkedQueue() + >>> queue.is_empty() + True + >>> queue.put(5) + >>> queue.put(9) + >>> queue.put('python') + >>> queue.is_empty(); + False + >>> queue.get() + 5 + >>> queue.put('algorithms') + >>> queue.get() + 9 + >>> queue.get() + 'python' + >>> queue.get() + 'algorithms' + >>> queue.is_empty() + True + >>> queue.get() + Traceback (most recent call last): + ... + IndexError: get from empty queue + """ + + def __init__(self) -> None: + self.front: Optional[Node] = None + self.rear: Optional[Node] = None + + def is_empty(self) -> bool: + """ returns boolean describing if queue is empty """ + return self.front is None + + def put(self, item: Any) -> None: + """ append item to rear of queue """ + node: Node = Node(item) + if self.is_empty(): + # the queue contains just the single element + self.front = node + self.rear = node + else: + # not empty, so we add it to the rear of the queue + assert isinstance(self.rear, Node) + self.rear.next = node + self.rear = node + + def get(self) -> Any: + """ returns and removes item at front of queue """ + if self.is_empty(): + raise IndexError("get from empty queue") + else: + # "remove" element by having front point to the next one + assert isinstance(self.front, Node) + node: Node = self.front + self.front = node.next + if self.front is None: + self.rear = None + + return node.data diff --git a/data_structures/stacks/linked_stack.py b/data_structures/stacks/linked_stack.py new file mode 100644 index 000000000..18ba87ddc --- /dev/null +++ b/data_structures/stacks/linked_stack.py @@ -0,0 +1,67 @@ +""" A Stack using a Linked List like structure """ +from typing import Any, Optional + + +class Node: + def __init__(self, data: Any, next: Optional["Node"] = None): + self.data: Any = data + self.next: Optional["Node"] = next + + +class LinkedStack: + """ + Linked List Stack implementing push (to top), + pop (from top) and is_empty + + >>> stack = LinkedStack() + >>> stack.is_empty() + True + >>> stack.push(5) + >>> stack.push(9) + >>> stack.push('python') + >>> stack.is_empty(); + False + >>> stack.pop() + 'python' + >>> stack.push('algorithms') + >>> stack.pop() + 'algorithms' + >>> stack.pop() + 9 + >>> stack.pop() + 5 + >>> stack.is_empty() + True + >>> stack.pop() + Traceback (most recent call last): + ... + IndexError: pop from empty stack + """ + + def __init__(self) -> None: + self.top: Optional[Node] = None + + def is_empty(self) -> bool: + """ returns boolean describing if stack is empty """ + return self.top is None + + def push(self, item: Any) -> None: + """ append item to top of stack """ + node: Node = Node(item) + if self.is_empty(): + self.top = node + else: + # each node points to the item "lower" in the stack + node.next = self.top + self.top = node + + def pop(self) -> Any: + """ returns and removes item at top of stack """ + if self.is_empty(): + raise IndexError("pop from empty stack") + else: + # "remove" element by having top point to the next one + assert isinstance(self.top, Node) + node: Node = self.top + self.top = node.next + return node.data