Stack in Data Structures

A stack represents a group of elements stored in LIFO (Last In First Out) order. This means that the element which is stored as a last element into the stack will be the first element to be removed from the stack. Inserting elements into the stack is called “Push Operation” and removing elements from the stack is called “Pop Operation”. Searching for an element and returning it without removing it from the stack is called “Peep Operation”. Insertion and deletion of an elements takes place only one side of the stack called “Top of the Stack” the other side of the stack is called Bottom of the Stack.

Stacks must strictly follow LIFO order where the last element pushed into the top of the stack should be popped first. A stack is internally used by the operating system to save the program execution environment, it means that when the program is running, the state of variable and information about the execution status are stored in a Stack. Another use of Stack is expression evaluation. While evaluating expression like ax+by*5. They are converted into postfix or prefix notations using a Stack and Stored into the Stack. Later they are retried from the Stack and evaluated accordingly to the certain rules.

Push Operation: It means inserting elements at the Top of the Stack. This can be done with the help of append() method of list as st.append (element), where st is the list.

Pop Operation: It means removing top most element from the Stack, this can be perform using pop () method of list as st.pop(). This method returns the removed element that can be displayed.

Peep Operation: It means returning the top most element without deleting it from the stack. Peep is also known as “Peek Operation”. This is done by returning st[n-1], Where n is the number of elements of the stack. So if the stack has 5 elements the top most element position will be 4, since the elements are referred from 0th to 4th positions.

Searching Operation: It means knowing the position of an element in the stack from the top of the Stack. For this purpose the list’s index () method can be used as st.index(element), which returns the position number ”n“ of the element from the beginning (or bottom) of the Stack. Once this is known, you can get its position from the top of the stack as size of the stack -n.

Empty Stack or Not: This can be judged by simply testing whether the list ‘st’ is empty or not, we can use an expression as: ‘return st == []’. That returns True if ‘st’ is empty else False.

Here is the python Program to create a Stack:

from stack import Stack
s=Stack()
choice=0
element=None
while True:
print('-'*15,'Stack Operations','-'*15)
    print('1. Push operation\n2. Pop opeartion\n3. Peep operation\n4. Search
operation\n5. See the Stack\n6. Exit')
    choice = int(input("Enter your choice: "))
    if choice == 1:
        element = input("Enter the element: ")
        s.push(element)
    elif choice == 2:
        if element == -1:
            print("The Stack is empty.")
        else:
            element = s.pop()
            print("The Popped element is: ",element)
    elif choice == 3:
        element = s.peep()
        if element == -1:
            print("The Stack is empty.")
        else:
            print("Topmost element is: ",element)
    elif choice == 4:
        element = input("Enter the element: ")
        pos=s.search(element)
        if pos == -1:
            print("The Stack is empty: ")
        elif pos == -2:
            print("The element is not found in the Stack.")
        else:
            print("The element is found at the position: ",pos)
    elif choice == 5:
        print("Stack is: ",s.display())
    elif choice == 6:
        break
    else:
        print("invalid input!.")

Now save this program as “stack.py” as a python module then you can use all the Stack properties of this python file by simply importing the file as a module like “from stack import Stack” and then you have to create the object of the Stack class, it is “s” in our case.


The below code shows you how to use this class or python module in other program.

from stack import Stack
s=Stack()
choice=0
element=None
while True:
    print('-'*15,'Stack Operations','-'*15)
    print('1. Push operation\n2. Pop opeartion\n3. Peep operation\n4. Search
operation\n5. See the Stack\n6. Exit')
    choice = int(input("Enter your choice: "))
    if choice == 1:
        element = input("Enter the element: ")
        s.push(element)
    elif choice == 2:
        if element == -1:
            print("The Stack is empty.")
        else:
            element = s.pop()
            print("The Popped element is: ",element)
    elif choice == 3:
        element = s.peep()
        if element == -1:
            print("The Stack is empty.")
        else:
            print("Topmost element is: ",element)
    elif choice == 4:
        element = input("Enter the element: ")
        pos=s.search(element)
        if pos == -1:
            print("The Stack is empty: ")
        elif pos == -2:
            print("The element is not found in the Stack.")
        else:
            print("The element is found at the position: ",pos)
    elif choice == 5:
        print("Stack is: ",s.display())
    elif choice == 6:
        break
    else:
        print("invalid input!.")

Output:

--------------- Stack Operations ---------------
1. Push operation
2. Pop opeartion
3. Peep operation
4. Search operation
5. See the Stack
6. Exit
Enter your choice: 1
Enter the element: 1
--------------- Stack Operations ---------------
1. Push operation
2. Pop opeartion
3. Peep operation
4. Search operation
5. See the Stack
6. Exit
Enter your choice: 1
Enter the element: Silan
--------------- Stack Operations ---------------
1. Push operation
2. Pop opeartion
3. Peep operation
4. Search operation
5. See the Stack
6. Exit
Enter your choice: 1
Enter the element: 2
--------------- Stack Operations ---------------
1. Push operation
2. Pop opeartion
3. Peep operation
4. Search operation
5. See the Stack
6. Exit
Enter your choice: 3
Topmost element is: 2
--------------- Stack Operations ---------------
1. Push operation
2. Pop opeartion
3. Peep operation
4. Search operation
5. See the Stack
6. Exit
Enter your choice: 4
Enter the element: Silan
The element is found at the position: 2
--------------- Stack Operations ---------------
1. Push operation
2. Pop opeartion
3. Peep operation
4. Search operation
5. See the Stack
6. Exit
Enter your choice: 5
Stack is: ['1', 'Silan', '2']
--------------- Stack Operations ---------------
1. Push operation
2. Pop opeartion
3. Peep operation
4. Search operation
5. See the Stack
6. Exit
Enter your choice: 2
The Popped element is: 2
--------------- Stack Operations ---------------
1. Push operation
2. Pop opeartion
3. Peep operation
4. Search operation
5. See the Stack
6. Exit
Enter your choice: 5
Stack is: ['1', 'Silan']
--------------- Stack Operations ---------------
1. Push operation
2. Pop opeartion
3. Peep operation
4. Search operation
5. See the Stack
6. Exit
Enter your choice: 6

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext