Friday, 15 June 2018

To implement stack using list using python programming.

# To implement stack using list

stackList = []

def nrPush(ele='\0'):
    if ele!='\0':
        stackList.append(ele)
        print("PUSH operation successfully completed.")
    else:
        print("Operation failled.")
        print("Entered element is empty.")

def nrPop():
    if(len(stackList)>0):
        stackList.remove(stackList[-1])
        print("POP operation successfully completed.")
    else:
        print("Operation failled.")
        print("List is empty.")

while(True):
    print("\n\nChoose one option : ")
    print("1. PUSH operation,")
    print("2. POP operation.")
    print("3. Display List element.")
    print("e. Exit")
    op = input()
    if op=='1':
        ele = input("Enter element for PUSH operation.")
        nrPush(ele)
    elif op == '2':
        nrPop()
    elif op == '3':
        if(len(stackList)>0):
            print(stackList)
        else:
            print("List is Empty")
    elif op == 'e':
        break
    else:
        print("Choose correct option.")

No comments:

Post a Comment

Popular Posts