From 50c35b796c3adaf9a0d0c13fbe95e270d7b1464d Mon Sep 17 00:00:00 2001 From: Wenru Dong Date: Mon, 8 Oct 2018 21:04:25 +0100 Subject: [PATCH] implementation of linked-list-based stack in python --- python/08_stack/linked_stack.py | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 python/08_stack/linked_stack.py diff --git a/python/08_stack/linked_stack.py b/python/08_stack/linked_stack.py new file mode 100644 index 0000000..2329d21 --- /dev/null +++ b/python/08_stack/linked_stack.py @@ -0,0 +1,49 @@ +""" + Stack based upon linked list + 基于链表实现的栈 + + Author: Wenru +""" + +from typing import Optional + +class Node: + + def __init__(self, data: int, next=None): + self._data = data + self._next = next + + +class LinkedStack: + """A stack based upon singly-linked list. + """ + def __init__(self): + self._top: Node = None + + def push(self, value: int): + new_top = Node(value) + new_top._next = self._top + self._top = new_top + + def pop(self) -> Optional[int]: + if self._top: + value = self._top._data + self._top = self._top._next + return value + + def __repr__(self) -> str: + current = self._top + nums = [] + while current: + nums.append(current._data) + current = current._next + return " ".join(f"{num}]" for num in nums) + +if __name__ == "__main__": + stack = LinkedStack() + for i in range(9): + stack.push(i) + print(stack) + for _ in range(3): + stack.pop() + print(stack) \ No newline at end of file