Stacks

The concept behind stacks can be learnt from the following video tutorial

A program that implements Stacks is as follows:


#class for defining a stack
class Stack:
    def __init__(self):
        self.items = []
        
    #method for pushing an item on a stack
    def push(self,item):
        self.items.append(item)
        
    #method for popping an item from a stack
    def pop(self):
        return self.items.pop()
    
    #method to check whether the stack is empty or not
    def isEmpty(self):
        return (self.items == [])
    
    #method to get the top of the stack
    def topOfStack(self):
        return len(self.items)
    
    def __str__(self):
        return str(self.items)
    


stck = Stack()

stck.push(5)
stck.push(10)
stck.push(15)
stck.push(20)

print stck
#Output = [5, 10, 15, 20]

stck.pop()

print stck
#Output = [5, 10, 15]

print stck.topOfStack()
#Output = 3
            
            
        

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s