A list NUM has some integer numbers. Write down a program to prepare a stack of even integers from the given list NUM.

 


Q.1) A list NUM has some integer numbers. Write down a program to prepare a stack of even integers from the given list NUM. The program should have user- defined functions which perform the following operations based on this list.

Traverse the content of the list and push the EVEN numbers onto the stack.

Pop and display the content of the stack.



 stack = []


def push():

    for ele in N :

        if ele % 2 == 0:

            stack.append(ele)

        else:

            continue

        

def pop():

    if stack==[]:

        print("Stack is empty.")

    else:

        top=len(stack)-1

        ele=stack[top]

        print("Element to be deleted is:",ele)

        stack.pop(top)


def display():

    if stack==[]:

        print("stack is empty")

    else:

        l=len(stack) - 1

        for i in range(l,-1,-1):

            print(stack[i],end = " ")

            

            

ans = 'y'

while(ans == 'y'):       

    print("***** STACK MENU *****")

    print("1. Add elements to stack.")

    print("2. Delete element from stack.")

    print("3. Display stack.")

    choice = int(input("Enter your choice:"))

    if choice  == 1:

        push()

    elif choice == 2:

        pop()

    elif choice == 3 :

        display()

    else:

        print("Invalid choice .... ")

    ans=input("Do you want to perform more ? if yes press 'y':")

 SET - A Q1 ) 👆




For odd numbers

   1. A list NUM1 has some integer numbers. Write down a program to prepare a stack of even integers from the given list NUM1. The program should have user- defined functions which perform the following operations based on this list.

Traverse the content of the list and push the ODD numbers onto the stack.

Pop and display the content of the stack.


stack = []


def push():

    for ele in N :

        if ele % 2 != 0:

            stack.append(ele)

        else:

            continue

        

def pop():

    if stack==[]:

        print("Stack is empty.")

    else:

        top=len(stack)-1

        ele=stack[top]

        print("Element to be deleted is:",ele)

        stack.pop(top)


def display():

    if stack==[]:

        print("stack is empty")

    else:

        l=len(stack) - 1

        for i in range(l,-1,-1):

            print(stack[i],end = " ")

            

            

ans = 'y'

while(ans == 'y'):       

    print("***** STACK MENU *****")

    print("1. Add elements to stack.")

    print("2. Delete element from stack.")

    print("3. Display stack.")

    choice = int(input("Enter your choice:"))

    if choice == 1:

        push()

    elif choice == 2:

        pop()

    elif choice == 3 :

        display()

    else:

        print("Invalid choice .... ")

    ans=input("Do you want to perform more ? if yes press 'y':")








No comments:

Powered by Blogger.