Wednesday, 14 January 2026

STACKS : DO ANY METHOD THATS EASY FOR YOU , THOUGH THEY MAY ASK PUSH( STACK, RECORD) . BOTH WILL FETCH YOU FULL MARKS. NO WORRIES

 METHOD 1 : 

# Stack and limit

stack = []

limit = 5


# Menu function

def menu():

    while True:

        print("1. PUSH Issue Record")

        print("2. POP Issue Record")

        print("3. DISPLAY Issue Records")

        print("4. CALCULATE Total Fine Amount")

        print("5. EXIT")


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


        if choice == 1:

            issue_id = int(input("Enter Issue ID: "))

            member = input("Enter Member Name: ")

            book = input("Enter Book Title: ")

            fine = float(input("Enter Fine Amount: "))

            record = [issue_id, member, book, fine]

            push(stack, record)


        elif choice == 2:

            result = pop(stack)

            if result == "UNDERFLOW":

                print("UNDERFLOW\n")

            else:

                print("Removed Record:", result, "\n")


        elif choice == 3:

            display(stack)


        elif choice == 4:

            calculate(stack)


        elif choice == 5:

            print("Exiting program.")

            break


        else:

            print("Invalid choice\n")



# i) PUSH operation

def push(stack, record):

    if len(stack) == limit:

        print("OVERFLOW\n")

    else:

        stack.append(record)

        print("Issue record added successfully\n")


# ii) POP operation

def pop(stack):

    if len(stack) == 0:

        return "UNDERFLOW"

    else:

        return stack.pop()


# iii) DISPLAY operation

def display(stack):

    if len(stack) == 0:

        print("STACK EMPTY\n")

    else:

        print("Issue Records in Stack:")

        for record in stack[::-1]:

            print(record)

        print()


# iv) CALCULATE total fine amount

def calculate(stack):

    if len(stack) == 0:

        print("STACK EMPTY\n")

    else:

        total = 0

        for record in stack:

            total += record[3]

        print("Total Fine Amount:", total, "\n")



# Call menu

menu()






METHOD 2 : 

stack = []

LIMIT = 5


def menu():

    while True:

        print("\n1. PUSH  2. POP  3. DISPLAY  4. CALCULATE  5. EXIT")

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


        if ch == 1:

            push()

        elif ch == 2:

            pop()

        elif ch == 3:

            display()

        elif ch == 4:

            calculate()

        elif ch == 5:

            print("Exiting program")

            break

        else:

            print("Invalid choice")


def push():

    if len(stack) >= LIMIT:

        print("OVERFLOW")

    else:

        issue_id = int(input("Issue ID: "))

        name = input("Member Name: ")

        book = input("Book Title: ")

        fine = float(input("Fine Amount: "))

        stack.append([issue_id, name, book, fine])


def pop():

    if len(stack) == 0:

        print("UNDERFLOW")

    else:

        print("Removed Record:", stack.pop())


def display():

    if len(stack) == 0:

        print("STACK EMPTY")

    else:

        for record in reversed(stack):

            print(record)


def calculate():

    total = 0

    for record in stack:

        total += record[3]

    print("Total Fine Amount =", total)


# Call menu function

menu()


No comments:

Post a Comment