Stack implementation in Python


Just for fun 🙂


class Stack:
def __init__(self):
self.curSize = 0
self.curStack = []

def pop(self):
if self.curSize > 0:
self.curSize -= 1
self.curStack.remove(self.curStack[self.curSize])
print "Element removed"
else:
print "No elements into the stack!"

def push(self,element):
self.curStack.append(element)
self.curSize += 1
print "Element inserted"

def top(self):
if self.curSize > 0:
print self.curStack[self.curSize - 1]
else:
print "No elements into the stack!"

def printMe(self):
print self.curStack

if __name__ == "__main__":
gimme_stack = Stack()
gimme_stack.push(7)
gimme_stack.push(5)
gimme_stack.push(21)
gimme_stack.printMe()
gimme_stack.pop()
gimme_stack.printMe()
gimme_stack.pop()
gimme_stack.printMe()
gimme_stack.pop()
gimme_stack.printMe()
gimme_stack.pop()
gimme_stack.printMe()

Advertisements

Tags: ,

One response to “Stack implementation in Python”

  1. Gagan sahoo says :

    queue implementation in python

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: