Python Implemented Stack [ code included ]

A stack is one such data structure algorithm that every computer science student must learn it. It is used in so many places to implement such a crucial task that without stack it is almost unimaginable to run vital system of a computer.

Stacks are used in computer hardware like a keyboard. it is also used in binary search, merge sort, postfix evaluation, conversion of infix expression to postfix, etc.

A stack is present in almost everywhere in our daily system, though we used to implement it using so many languages, here in this article we will implement it using python list.

How Stack is implemented in Python

A stack is a data algorithm that is inspired by real-life containers. You can remove last inserted items from the container first.  The same we used in python list to implement a stack.  Whole Stack program is divided into four parts

  1. Function to insert a new element at the end in our list – push( )
  2. function to remove last element from the list  – pop ( )
  3. Function  to display stack content – display

All these functions are now invoked thorough a menu drive program to mimic stack.

Source code of  Python-based Stack

# program to generate stack using list
# made by           : rakesh kumar
# last revision     : 20/03/2018

#function to add element at the end of list
def push(stack,x):
    stack.append(x)
#function to remove last element from list
def pop(stack):
    n = len(stack)
    if(n<=0):
        print("Stack empty....Pop not possible")
    else:
        stack.pop()

#function to display stack entry
def display(stack):
    if len(stack)<=0:
        print("Stack empty...........Nothing to display")
    for i in stack:
        print(i,end=" ")
#main program starts from here
x=[]
choice=0
while (choice!=4):
    print("\n\t\t\t stack menu \n\t1. push \n\t2. pop \n\t3. Display \n\t4. Exit")
    choice = int(input("Enter your choice"))

    if(choice==1):
        value = int(input("Enter value "))
        push(x,value)
    if(choice==2):
        pop(x)
    if(choice==3):
        display(x)
    if(choice==4):
        print("You selected to close this program")

Explanation of Python-based Stack

Since Python does not support array and link-list like C and C++, here we have implemented stack using python list. python list support a lot of built-in methods like append() and pop().

Append() method automatically add an element at the last position of the list and the size of list is only dependent on the memory size of your system. The below code is doing the same here in this push function( )

#function to add element at the end of list 
def push(stack,x): 
     stack.append(x)

Here stack is the name of list and x is that element that we are actually inserting in the list.

Pop( ) is another list method that is used to remove the last element from the list but before removing any element from the list, just checking the present number of elements in the list.

#function to remove last element from list
def pop(stack):
    n = len(stack)
    if(n<=0):
        print("Stack empty....Pop not possible")
    else:
        stack.pop()

The same has been implemented for displaying the stack content.

#function to display stack entry
def display(stack):
    if len(stack)<=0:
        print("Stack empty...........Nothing to display")
    for i in stack:
        print(i,end=" ")

All these functions now have been invoked and the working of this whole program is behaving like a stack. The output of stack program is as follow

C:/Python37/python.exe e:/python/DataStructure/stack.py

stack menu
1. push
2. pop
3. Display
4. Exit
Enter your choice : 1

Well, there are some scope for improvement in this program, If you are able to find out that scope, please feel free to contact us via email.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.