Created
October 23, 2018 05:00
-
-
Save ridwanbejo/07f786a3d1e611d2a5e9750919ea0f43 to your computer and use it in GitHub Desktop.
Install dulu python, terus jalankan dengan perintah: python main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StackArray(object): | |
""" | |
Perbandingan sintaks Python dan Java | |
- self = this | |
- def = function | |
- None = null | |
""" | |
def create_stack(self, panjang): | |
self.panjang = panjang | |
self.stack = [None for x in range(0, panjang)] | |
def destroy_stack(self): | |
self.stack = None | |
def push(self, nilai): | |
if type(self.stack) != None: | |
if self.is_empty() == True: | |
for indeks in range(0, self.panjang): | |
if self.stack[indeks] == None: | |
self.stack[indeks] = nilai | |
break | |
else: | |
print "Stack sudah penuh, saat ini tidak bisa memasukkan nilai ke stack!" | |
else: | |
print "Stack sudah dihapus, tidak bisa memasukkan nilai ke stack!" | |
def pop(self): | |
if type(self.stack) != None: | |
for indeks in range(self.panjang-1, -1, -1): | |
if self.stack[indeks] != None: | |
self.stack[indeks] = None | |
break | |
else: | |
print "Stack sudah dihapus, tidak bisa memasukkan nilai ke stack!" | |
def is_empty(self): | |
status = False | |
for indeks in range(0, self.panjang): | |
if self.stack[indeks] == None: | |
status = True | |
return status | |
def size(self): | |
count_nilai = 0 | |
for indeks in range(0, self.panjang): | |
if self.stack[indeks] != None: | |
count_nilai = count_nilai + 1 | |
return count_nilai | |
def show(self): | |
for indeks in range(self.panjang-1, -1, -1): | |
print self.stack[indeks] | |
my_stack = StackArray() | |
my_stack.create_stack(10) | |
my_stack.push(10) | |
my_stack.push(20) | |
my_stack.push(30) | |
my_stack.push(40) | |
my_stack.push(50) | |
my_stack.push(60) | |
my_stack.push(70) | |
my_stack.push(80) | |
my_stack.push(90) | |
my_stack.push(100) | |
my_stack.push(110) | |
my_stack.push(120) | |
print my_stack.size() | |
my_stack.show() | |
my_stack.pop() | |
print my_stack.size() | |
my_stack.show() | |
my_stack.pop() | |
print my_stack.size() | |
my_stack.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment