Guest User

Untitled

a guest
Jun 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. # coding=utf-8
  2.  
  3. class Stack(object):
  4. def __init__(self):
  5. self.data = []
  6.  
  7. def pop(self):
  8. return self.data.pop()
  9.  
  10. def push(self, element):
  11. self.data.append(element)
  12.  
  13. def top(self):
  14. return self.data[-1]
  15.  
  16. def isEmpty(self):
  17. return len(self.data) == 0
  18.  
  19. class Queue(object):
  20. def __init__(self):
  21. self.data = []
  22.  
  23. def enqueue(self, element):
  24. self.data.append(element)
  25.  
  26. def dequeue(self):
  27. return self.data.pop(0)
  28.  
  29. def isEmpty(self):
  30. return len(self.data) == 0
  31.  
  32. if __name__ == "__main__":
  33. stack = Stack()
  34. print("stack operation...")
  35. print("push 1, 2, 3")
  36. stack.push(1)
  37. stack.push(2)
  38. stack.push(3)
  39. print("top", end=' ')
  40. print(stack.top())
  41. print("pop all elements:")
  42. while(not stack.isEmpty()):
  43. print(stack.pop())
  44.  
  45. print()
  46.  
  47. queue = Queue()
  48. print("queue operation...")
  49. print("enqueue 1,2,3")
  50. queue.enqueue(1)
  51. queue.enqueue(2)
  52. queue.enqueue(3)
  53. print("dequeue all elements")
  54. while(not queue.isEmpty()):
  55. print(queue.dequeue())
Add Comment
Please, Sign In to add comment