Implementing Stack with Python
I corrected a few problems below. Also, a 'stack', in abstract programming terms, is usually a collection where you add and remove from the top, but the way you implemented it, you're adding to the top and removing from the bottom, which makes it a queue.
class myStack:
def __init__(self):
self.container = [] # You don't want to assign [] to self - when you do that, you're just assigning to a new local variable called `self`. You want your stack to *have* a list, not *be* a list.
def isEmpty(self):
return self.size() == 0 # While there's nothing wrong with self.container == [], there is a builtin function for that purpose, so we may as well use it. And while we're at it, it's often nice to use your own internal functions, so behavior is more consistent.
def push(self, item):
self.container.append(item) # appending to the *container*, not the instance itself.
def pop(self):
return self.container.pop() # pop from the container, this was fixed from the old version which was wrong
def peek(self):
if self.isEmpty():
raise Exception("Stack empty!")
return self.container[-1] # View element at top of the stack
def size(self):
return len(self.container) # length of the container
def show(self):
return self.container # display the entire stack as list
s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())
A stack is a container (linear collection) in which dynamic set operations
are carried out as per the last-in first-out (LIFO) principle.
There is only one pointer - top
, which is used to perform these operations
CLRS implementation of stack using array:
class Stack:
"""
Last in first out (LIFO) stack implemented using array.
"""
def __init__(self, capacity=4):
"""
Initialize an empty stack array with default capacity of 4.
"""
self.data = [None] * capacity
self.capacity = capacity
self.top = -1
def is_empty(self):
"""
Return true if the size of stack is zero.
"""
if self.top == -1:
return True
return False
def push(self, element):
"""
Add element to the top.
"""
self.top += 1
if self.top >= self.capacity:
raise IndexError('Stack overflow!')
else:
self.data[self.top] = element
def pop(self):
"""
Return and remove element from the top.
"""
if self.is_empty():
raise Exception('Stack underflow!')
else:
stack_top = self.data[self.top]
self.top -= 1
return stack_top
def peek(self):
"""
Return element at the top.
"""
if self.is_empty():
raise Exception('Stack is empty.')
return None
return self.data[self.top]
def size(self):
"""
Return the number of items present.
"""
return self.top + 1
Testing the implemetation:
def main():
"""
Sanity test
"""
stack = Stack()
print('Size of the stack is:', stack.size())
stack.push(3)
print('Element at the top of the stack is: ', stack.peek())
stack.push(901)
print('Element at the top of the stack is: ', stack.peek())
stack.push(43)
print('Element at the top of the stack is: ', stack.peek())
print('Size of the stack is:', stack.size())
stack.push(89)
print('Element at the top of the stack is: ', stack.peek())
print('Size of the stack is:', stack.size())
#stack.push(9) # Raises IndexError
stack.pop()
print('Size of the stack is:', stack.size())
stack.pop()
print('Size of the stack is:', stack.size())
stack.pop()
print('Size of the stack is:', stack.size())
print('Element at the top of the stack is: ', stack.peek())
stack.pop()
#print('Element at the top of the stack is: ', stack.peek()) # Raises empty stack exception
if __name__ == '__main__':
main()
Assigning to self
won't turn your object into a list (and if it did, the object wouldn't have all your stack methods any more). Assigning to self
just changes a local variable. Instead, set an attribute:
def __init__(self):
self.stack = []
and use the attribute instead of just a bare self
:
def push(self, item):
self.stack.append(item)
Also, if you want a stack, you want pop()
rather than pop(0)
. pop(0)
would turn your data structure into a(n inefficient) queue.