Guest User

Untitled

a guest
Oct 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. class StackArray(object):
  2.  
  3. """
  4. Perbandingan sintaks Python dan Java
  5. - self = this
  6. - def = function
  7. - None = null
  8. """
  9. def create_stack(self, panjang):
  10. self.panjang = panjang
  11. self.stack = [None for x in range(0, panjang)]
  12.  
  13. def destroy_stack(self):
  14. self.stack = None
  15.  
  16. def push(self, nilai):
  17. if type(self.stack) != None:
  18. if self.is_empty() == True:
  19. for indeks in range(0, self.panjang):
  20. if self.stack[indeks] == None:
  21. self.stack[indeks] = nilai
  22. break
  23. else:
  24. print "Stack sudah penuh, saat ini tidak bisa memasukkan nilai ke stack!"
  25. else:
  26. print "Stack sudah dihapus, tidak bisa memasukkan nilai ke stack!"
  27.  
  28. def pop(self):
  29. if type(self.stack) != None:
  30. for indeks in range(self.panjang-1, -1, -1):
  31. if self.stack[indeks] != None:
  32. self.stack[indeks] = None
  33. break
  34. else:
  35. print "Stack sudah dihapus, tidak bisa memasukkan nilai ke stack!"
  36.  
  37. def is_empty(self):
  38. status = False
  39. for indeks in range(0, self.panjang):
  40. if self.stack[indeks] == None:
  41. status = True
  42.  
  43. return status
  44.  
  45. def size(self):
  46. count_nilai = 0
  47. for indeks in range(0, self.panjang):
  48. if self.stack[indeks] != None:
  49. count_nilai = count_nilai + 1
  50. return count_nilai
  51.  
  52. def show(self):
  53. for indeks in range(self.panjang-1, -1, -1):
  54. print self.stack[indeks]
  55.  
  56.  
  57. my_stack = StackArray()
  58. my_stack.create_stack(10)
  59. my_stack.push(10)
  60. my_stack.push(20)
  61. my_stack.push(30)
  62. my_stack.push(40)
  63. my_stack.push(50)
  64. my_stack.push(60)
  65. my_stack.push(70)
  66. my_stack.push(80)
  67. my_stack.push(90)
  68. my_stack.push(100)
  69. my_stack.push(110)
  70. my_stack.push(120)
  71.  
  72. print my_stack.size()
  73. my_stack.show()
  74.  
  75. my_stack.pop()
  76.  
  77. print my_stack.size()
  78. my_stack.show()
  79.  
  80. my_stack.pop()
  81.  
  82. print my_stack.size()
  83. my_stack.show()
Add Comment
Please, Sign In to add comment