Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
962 B

5 years ago
  1. class Stack:
  2. def __init__ (self):
  3. self.items = list()
  4. def push (self, item):
  5. self.items.append (item)
  6. def pop (self):
  7. if len(self.items) > 0:
  8. return self.items.pop()
  9. return ('Stack is empty')
  10. def isEmpty (self):
  11. return self.items == list()
  12. def size (self):
  13. return len(self.items)
  14. def peek (self):
  15. return self.items[0]
  16. def printStack (self):
  17. print(self.items)
  18. if __name__ == '__main__':
  19. my_stack = Stack()
  20. my_stack.push(1)
  21. my_stack.printStack()
  22. my_stack.push(5)
  23. my_stack.push(3)
  24. my_stack.printStack()
  25. print('Pop {} from stack'.format(my_stack.pop()))
  26. my_stack.printStack()
  27. print('Now stack size is {}'.format(my_stack.size()))
  28. print('First element in stack is {}'.format(my_stack.peek()))
  29. print('Pop {} from stack'.format(my_stack.pop()))
  30. my_stack.printStack()
  31. print('Pop {} from stack'.format(my_stack.pop()))
  32. my_stack.printStack()
  33. print('Now stack size is {}'.format(my_stack.size()))
  34. print(my_stack.pop())