fork download
  1. class Node:
  2. def __init__(self, data):
  3. self.data = data
  4. self.next = None
  5.  
  6. class Stack:
  7. def __init__(self):
  8. self.top = None
  9.  
  10. def push(self, data):
  11. new_node = Node(data)
  12. new_node.next = self.top
  13. self.top = new_node
  14. print("Node is Inserted")
  15.  
  16. def pop(self):
  17. if self.is_empty():
  18. print("EMPTY STACK")
  19. return
  20. popped_element = self.top.data
  21. self.top = self.top.next
  22. print("Popped Element:", popped_element)
  23.  
  24. def display(self):
  25. if self.is_empty():
  26. print("EMPTY STACK")
  27. return
  28. cur = self.top
  29. stack_elements = []
  30. while cur:
  31. stack_elements.append(cur.data)
  32. cur = cur.next
  33. print("The stack is:", ' '.join(map(str, stack_elements)))
  34.  
  35. def is_empty(self):
  36. return self.top is None
  37.  
  38. stack = Stack()
  39.  
  40. while True:
  41. try:
  42. choice = int(input().strip())
  43. if choice == 1:
  44. element = input().strip()
  45. stack.push(element)
  46. elif choice == 2:
  47. stack.pop()
  48. elif choice == 3:
  49. stack.display()
  50. elif choice == 4:
  51. break
  52. else:
  53. print("Wrong choice")
  54. except ValueError:
  55. print("Wrong choice")
  56.  
Success #stdin #stdout 0.03s 9268KB
stdin
1 10
1 20
1 30
1 40
1 50
2
2
3
4
stdout
Wrong choice
Wrong choice
Wrong choice
Wrong choice
Wrong choice
EMPTY STACK
EMPTY STACK
EMPTY STACK