Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. class Node:
  2. def __init__(self):
  3. self.data = None
  4. self.nextNode = None
  5.  
  6. class LLStack:
  7. def __init__(self):
  8. self.head = None
  9. self.tail = None
  10.  
  11. def Push(self, value):
  12. current = Node()
  13. current.data = value
  14. if self.head == None:
  15. current.nextNode = None
  16. self.head = current
  17. else:
  18. current.nextNode = self.head
  19. self.head = current
  20.  
  21.  
  22. def Pop(self):
  23. if self.head == None:
  24. print("Stack Underflow")
  25. elif self.head.nextNode == None:
  26. self.head = None
  27. else:
  28. self.head = self.head.nextNode
  29.  
  30. def viewStack(self):
  31. outString = ""
  32. if not(self.head == None):
  33. current = self.head
  34. while not(current == None):
  35. outString = outString + str(current.data) + ", "
  36. current = current.nextNode
  37. print(outString)
  38. else:
  39. print("Nothing in stack")
  40.  
  41. stack = LLStack()
  42. stack.viewStack()
  43. stack.Pop()
  44. stack.viewStack()
  45. stack.Push(5)
  46. stack.viewStack()
  47. stack.Push(10)
  48. stack.viewStack()
  49. stack.Pop()
  50. stack.viewStack()
  51. stack.Pop()
  52. stack.viewStack()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement