From 70cb0c00272df7632becb1f9ae5d4d28e0345dfe Mon Sep 17 00:00:00 2001 From: eveem Date: Thu, 18 Oct 2018 01:49:30 +0700 Subject: [PATCH] Add Stack --- other/Stack.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 other/Stack.py diff --git a/other/Stack.py b/other/Stack.py new file mode 100644 index 0000000..f341e8c --- /dev/null +++ b/other/Stack.py @@ -0,0 +1,46 @@ +class Stack: + def __init__ (self): + self.items = list() + + def push (self, item): + self.items.append (item) + + def pop (self): + if len(self.items) > 0: + return self.items.pop() + return ('Stack is empty') + + def isEmpty (self): + return self.items == list() + + def size (self): + return len(self.items) + + def peek (self): + return self.items[0] + + def printStack (self): + print(self.items) + +if __name__ == '__main__': + my_stack = Stack() + + my_stack.push(1) + my_stack.printStack() + + my_stack.push(5) + my_stack.push(3) + my_stack.printStack() + + print('Pop {} from stack'.format(my_stack.pop())) + my_stack.printStack() + print('Now stack size is {}'.format(my_stack.size())) + + print('First element in stack is {}'.format(my_stack.peek())) + print('Pop {} from stack'.format(my_stack.pop())) + my_stack.printStack() + print('Pop {} from stack'.format(my_stack.pop())) + my_stack.printStack() + print('Now stack size is {}'.format(my_stack.size())) + print(my_stack.pop()) +