Membalikkan string menggunakan tumpukan dalam python
def createStack():
stack=[ ]
return stack
def size(stack):
return len(stack)
def isEmpty(stack):
if size(stack) == 0:
return true
def push(stack,item):
stack.append(item)
def pop(stack):
if isEmpty(stack): return
return stack.pop()
def reverse(string):
n = len(string)
stack = createStack()
for i in range(0,n,1):
push(stack,string[i])
string=""
for i in range(0,n,1):
string+=pop(stack)
return string
s = "SoftHunt"
print ("The original string is : ",end="")
print (s)
print ("The reversed string(using stack) is : ",end="")
print (reverse(s))
Outrageous Ostrich