Stack implementation using python language

download (1)Stack implementation is very tough and challenging with C and C++. We have to struggle a lot with pointers if we are programming in C and C++. But it becomes very easy in the case of Python language, Because we dont have any pointers in Python language. In C and C++ we have to declare the datatype for the variable as int , float , char etc… and while passing an array we have to stick with the syntax, else we will get a lot of errors. But in python it is very easy and simple, we just to pass the name of the list thats all.

For example in C and C++

int a,b;

char a,b;

But in python it becomes very simple than this, we dont need to declare any datatype for variable, only variable name is enough in the case of python language.

For example in Python

a,b,var

It can take the input as integer type, float type and character also, So it is very easy in Python.

But in Python we have to care a lot on our indentation. So it is one of the best programming language for programmers. Everything is inbuilt in Python itself. Stack implementation is very easy in Python, we can use list and then we have to use inbuilt functions in Python.

Inbuilt Functions to be invoked

append() and pop()

Using these two things we can easily built up a Stack. We have to remember our OOPS concept in while implementing this.

For linux users Python is pre-installed and we can use the command prompt to run it. Or else we can use an IDE for Python called as IDLE. Python version is of two types 3._._ and 2._._ , but the following code works in Python version 2._._ . You can download idle and install from  http://www.python.org/getit/.

In stack we have got five very important function, push(), pop(), top(), isempty() and size().

push()- It pushes the element in the stack.

pop()- It removes the top element in the stack.

top()- It returns the top most element in the stack.

isempty()- It checks whether the stack is empty of not. If the stack is empty it returns True and if it is not empty it returns false.

size()- It returns the size of the stack.

To get the code Click  HERE.

Thanks for reading our post. Stay tuned for our next post.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA Image

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>