This project is a simple but functional simulation of library management using basic data structures
class Book: def init(self, title, author): self.title = title self.author = author
class BookArray: def init(self): self.books = []
def add_book(self, title, author):
self.books.append(Book(title, author))
print(f"Book '{title}' by {author} added to the library.")
def remove_book(self, title):
for book in self.books:
if book.title == title:
self.books.remove(book)
print(f"Book '{title}' removed from the library.")
return
print("Book not found.")
def view_books(self):
if self.books:
print("\nBooks in the library:")
for book in self.books:
print(f"'{book.title}' by {book.author}")
else:
print("No books in the library.")
def search_book(self, title):
for book in self.books:
if book.title == title:
print(f"Book found: '{book.title}' by {book.author}")
return
print("Book not found.")
class Node: def init(self, book): self.book = book self.next = None
class BookLinkedList: def init(self): self.head = None
def add_book_beginning(self, title, author):
new_node = Node(Book(title, author))
new_node.next = self.head
self.head = new_node
print(f"Book '{title}' by {author} added at the beginning.")
def add_book_end(self, title, author):
new_node = Node(Book(title, author))
if not self.head:
self.head = new_node
else:
last = self.head
while last.next:
last = last.next
last.next = new_node
print(f"Book '{title}' by {author} added at the end.")
def remove_book_beginning(self):
if self.head:
book = self.head.book
self.head = self.head.next
print(f"Book '{book.title}' by {book.author} removed from the beginning.")
else:
print("No books to remove.")
def remove_book_end(self):
if self.head:
if not self.head.next:
book = self.head.book
self.head = None
print(f"Book '{book.title}' by {book.author} removed from the end.")
else:
temp = self.head
while temp.next and temp.next.next:
temp = temp.next
book = temp.next.book
temp.next = None
print(f"Book '{book.title}' by {book.author} removed from the end.")
else:
print("No books to remove.")
def view_books(self):
if self.head:
print("\nBooks in the library:")
temp = self.head
while temp:
print(f"'{temp.book.title}' by {temp.book.author}")
temp = temp.next
else:
print("No books in the library.")
def search_book(self, title):
temp = self.head
while temp:
if temp.book.title == title:
print(f"Book found: '{temp.book.title}' by {temp.book.author}")
return
temp = temp.next
print("Book not found.")
class BookStack: def init(self): self.stack = []
def push(self, title, author):
self.stack.append(Book(title, author))
print(f"Book '{title}' by {author} has been returned to the library.")
def pop(self):
if self.stack:
book = self.stack.pop()
print(f"Book '{book.title}' by {book.author} has been removed from the return stack.")
else:
print("No books to remove from the return stack.")
def peek(self):
if self.stack:
book = self.stack[-1]
print(f"Last book returned: '{book.title}' by {book.author}")
else:
print("No books in the return stack.")
def is_empty(self):
return len(self.stack) == 0
from collections import deque
class BookQueue: def init(self): self.queue = deque()
def enqueue(self, title, author):
self.queue.append(Book(title, author))
print(f"Book '{title}' by {author} has been borrowed.")
def dequeue(self):
if self.queue:
book = self.queue.popleft()
print(f"Book '{book.title}' by {book.author} has been returned to the library.")
else:
print("No books to return.")
def peek(self):
if self.queue:
book = self.queue[0]
print(f"Next book to be borrowed: '{book.title}' by {book.author}")
else:
print("No books in the queue.")
def is_empty(self):
return len(self.queue) == 0
def main(): # Creating instances of each data structure array_library = BookArray() linked_list_library = BookLinkedList() stack_library = BookStack() queue_library = BookQueue()
while True:
print("\nLibrary Management System")
print("1. Array Operations (Add, Remove, View, Search Book)")
print("2. Linked List Operations (Add, Remove, View, Search Book)")
print("3. Stack Operations (Return Book Undo)")
print("4. Queue Operations (Borrow Book)")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == '1':
print("\nArray Operations:")
print("1. Add Book")
print("2. Remove Book")
print("3. View Books")
print("4. Search Book")
action = input("Enter your action: ")
if action == '1':
title = input("Enter book title: ")
author = input("Enter book author: ")
array_library.add_book(title, author)
elif action == '2':
title = input("Enter book title to remove: ")
array_library.remove_book(title)
elif action == '3':
array_library.view_books()
elif action == '4':
title = input("Enter book title to search: ")
array_library.search_book(title)
elif choice == '2':
print("\nLinked List Operations:")
print("1. Add Book at Beginning")
print("2. Add Book at End")
print("3. Remove Book from Beginning")
print("4. Remove Book from End")
print("5. View Books")
print("6. Search Book")
action = input("Enter your action: ")
if action == '1':
title = input("Enter book title: ")
author = input("Enter book author: ")
linked_list_library.add_book_beginning(title, author)
elif action == '2':
title = input("Enter book title: ")
author = input("Enter book author: ")
linked_list_library.add_book_end(title, author)
elif action == '3':
linked_list_library.remove_book_beginning()
elif action == '4':
linked_list_library.remove_book_end()
elif action == '5':
linked_list_library.view_books()
elif action == '6':
title = input("Enter book title to search: ")
linked_list_library.search_book(title)
elif choice == '3':
print("\nStack Operations (Return Book):")
print("1. Return Book")
print("2. Undo Return")
print("3. View Last Return")
action = input("Enter your action: ")
if action == '1':
title = input("Enter book title: ")
author = input("Enter book author: ")
stack_library.push(title, author)
elif action == '2':
stack_library.pop()
elif action == '3':
stack_library.peek()
elif choice == '4':
print("\nQueue Operations (Borrow Book):")
print("1. Borrow Book")
print("2. Return Book")
print("3. View Next Borrow")
action = input("Enter your action: ")
if action == '1':
title = input("Enter book title: ")
author = input("Enter book author: ")
queue_library.enqueue(title, author)
elif action == '2':
queue_library.dequeue()
elif action == '3':
queue_library.peek()
elif choice == '5':
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")
if name == "main": main()