Write a function to implement String reversal in python using stack.
In [9]:
class stack(object):
'''Class to implement stack data structure with the following methods:
-push(element)
-pop()'''
def __init__(self):
self.s = []
def push(self,elem):
self.s.append(elem)
def pop(self):
p = self.s[-1]
del self.s[-1]
return p
def reverseString(string):
'''Function to reverse string using stack'''
s = stack()
for i in xrange(len(string)):
s.push(string[i])
f = ''
for i in xrange(len(string)):
f+=s.pop()
return f
In [ ]: