w3ajay

tally tutorial point, ms word 2013, ms excel 2013, ms powerpoint 2010,ccc question with answer in hindi 2021, Tally Prime in hindi , tally prime,Python,in python,programming in python,python

Tuesday, September 17, 2024

Code for insert element in stack python with example

 Stack in Python: How To Implement Python Stack?

Hello friends, 
                this series is very important for those who are preparing for Class 12th CBSE BOARD exam. It describes about stack implementation through which you can prepare for Class 12th CBSE BOARD and it will also help in exams which are based on the new syllabus.

Stack in Python

A stack is a linear structure implemented in LIFO (Last In First Out) manner where insertions and deletions are restricted to occur only at one end - Stack's top. LIFO means element last inserted would be the first one to be deleted.

Thus, we can say that a stack is a list of data that follows these rules :

1. Data can only be removed from the top (pop), i.e., the element at the top of the stack. The removal of element from a stack is technically called POP operation.

2. A new data element can only be added to the top of the stack (push). The insertion of element in a stack is technically called PUSH operation.

Functions with Python Stack

There are a bunch of useful functions in Python that help us deal with a stack efficiently. Let’s take a brief look at these functions –  

len()– This stack method is used for returning the size of the stack. This function can also be used in the definition of isempty() method in a Python stack.
append(n)– This Python function is used for inserting an element into the stack. The element to be pushed is passed in its argument.
pop()– This method, associated with the Python lists, is used for deleting the topmost element from the stack. 
peek()- it display last in first out element
display()-Display all element of stack

 Python Stack Code

# using list
stk = []
# append() function to push
# element in the stack
stk.append('first element')
stk.append('second element')
stk.append('third element')
print('inserted Elements in stack')
print(stk)

Output


Inserting element in stack with help of create function 



#create function for insert element in stack with help of push(append)
def push(stk):
    ele=int(input("Enter the your element:"))
    stk.append(ele)
    print(stk)
# -----------------main------------------- 
stk = []
while True:
    print("stack operation")
    print("Press 1. for Push(append/add)\n Press 5. for Stop Program ")
    chk=int(input("Enter the your Choice:"))
    if chk==1:
        push(stk)
    elif chk==5:
        break
    else:
        print("You are")
        

Implementation of Stack




Code for implement of stack

#create function for insert element in stack with help of push(append)
def push(stk):
    ele=int(input("Enter the your element:"))
    stk.append(ele)
    print(stk)
#create function for POP element in stack 
def peekelement(stk):
    print("Peek Element is=",stk[-1])
#create function for Display element in stack 
def display(stk):
    for i in range(len(stk)-1,-1,-1):
        print(stk[i])
# -----------------main------------------- 
stk = []
while True:
    print("stack operation")
    print("Press 1. for Push(append/add)\nPress 2. for POP\nPress 3. for PEEK\nPress 4. for DISPLAY\n Press 5. for Stop Program ")
    chk=int(input("Enter the your Choice:"))
    if chk==1:
        push(stk)
    elif chk==2:
        if len(stk)==0:
            print("Stack is Underflow...........")
        else:
            popelement(stk)
    elif chk==3:
        if len(stk)==0:
            print("Stack is Underflow...........")
        else:
            peekelement(stk)
    elif chk==4:
        if len(stk)==0:
            print("Stack is Underflow...........")
        else:
            display(stk)
    elif chk==5:
        break
    else:
        print("You are enter wrong number")

        

Thanks for Visiting my blog

No comments:

Post a Comment

for more information please share like comment and subscribe