Tuesday, 3 June 2025

GRADE 12 - PRACTICAL 12

'''GRADE 12: PRACTICAL 12

Create a list Library to implement a STACK data structure

in Python. The Library stack can hold a maximum of 5 records,

each representing a book. Each book is represented as :

{"BookID": <int>, "Title": <str>,

"Author": <str>,"Year": <str>}

Write a menu-driven program to perform :

a) push- Add a new book record to the stack.

b) pop- Delete the topmost book record from the stack.

c) display - To display all book records

d) peek- To view the topmost record of the stack

Handle Underflow and Overflow conditions for the stack .

'''

Library=[]

limit=5

def menu():

    while True:

        ch=int(input("""MENU:

1.PUSH

2.POP

3.DISPLAY

4.PEEK

5.EXIT

"""))

        if ch==1:

            bid=int(input("Enter book id "))

            title=input("enter title")

            auth=input("Enter author")

            year=input("Enter year")

            book={"BookID":bid, "Title":title,

"Author":auth,"Year":year}

            push(Library,book)

            

        elif ch==2:

            book=pop(Library)

            if book:

                print('Popped record : ',book)

                        

        elif ch==3:

            display(Library)


        elif ch==4:

            book=peek(Library)

            if book:

                print('Peekeed record : ',book)

            

        else:

            break


  

def push(Library,book):

    if len(Library)<limit:

        Library.append(book)

        print('Pushed record : ',book)

    else:

        print('OVERFLOW')



def pop(Library):

    if len(Library)>0:

        return Library.pop()

    else:

        print('UNDERFLOW')

        return None


def display(Library):

    print('Contents of Library')

    if len(Library)>0:

        for i in Library[::-1]:

            print(i)

    else:

        print('EMPTY')


def peek(Library):

    if Library:

        return Library[-1]

    else:

        print('UNDERFLOW')

        return None



menu()

            

        

    


No comments:

Post a Comment