Program 1
// Library Book Management System
#include <iostream>
#include <string>
using namespace std;
struct Book
{
int id;
string title;
string author;
Book* prev;
Book* next;
};
class Library
{
private:
Book* head;
public:
Library()
{
head = nullptr;
}
// Add a new book
void addBook(int id, string title, string author)
{
Book* newBook = new Book{id, title, author, nullptr, nullptr};
if (head == nullptr) {
head = newBook;
} else
{
Book* temp = head;
while (temp->next != nullptr) temp = temp->next;
temp->next = newBook;
newBook->prev = temp;
}
cout << "Book added successfully.\n";
}
// Display all books
void displayBooks()
{
if (head == nullptr) {
cout << "Library is empty.\n";
return;
}
Book* temp = head;
cout << "\nBooks in Library:\n";
while (temp != nullptr) {
cout << "ID: " << temp->id << ", Title: " << temp->title << ", Author: " << temp->author << "\n";
temp = temp->next;
}
}
// Search book by id
void searchBookById(int id)
{
Book *temp=head;
while(temp!=nullptr)
{
if(temp->id==id)
{
cout << "Book found: ID = " << temp->id << ", Author = " << temp->author << "\n";
return;
}
temp = temp->next;
}
cout << "Book not found with:" <<id<<" id \n";
}
// Search book by title
void searchBook(string title)
{
Book* temp = head;
while (temp != nullptr)
{
if (temp->title == title)
{
cout << "Book found: ID = " << temp->id << ", Author = " << temp->author << "\n";
return;
}
temp = temp->next;
}
cout << "Book not found.\n";
}
// Delete a book by ID
void deleteBook(int id)
{
Book* temp = head;
while (temp != nullptr && temp->id != id)
{
temp = temp->next;
}
if (temp == nullptr)
{
cout << "Book not found.\n";
return;
}
int ch;
cout << "Book found: ID = " << temp->id << ", Title = " << temp->title <<", Author = " << temp->author << "\n";
cout<<"\n Are You sure want to delete(1 for Yes)";
cin>>ch;
if(ch==1)
{
if (temp->prev != nullptr)
temp->prev->next = temp->next;
else
head = temp->next;
if (temp->next != nullptr)
temp->next->prev = temp->prev;
delete temp;
cout << "Book deleted successfully.\n";
}
}
// Update a book by ID
void updateBook(int id)
{
Book* temp = head;
while (temp != nullptr)
{
if (temp->id == id)
{
cout << "Enter new title: ";
cin.ignore();
getline(cin, temp->title);
cout << "Enter new author: ";
getline(cin, temp->author);
cout << "Book updated successfully.\n";
return;
}
temp = temp->next;
}
cout << "Book not found.\n";
}
};
int main()
{
Library lib; // Object of Library class
int choice, id;
string title, author;
do
{
cout << "\n---------------------Library Book Menu--------------------------\n";
cout << "\t\t1. Add Book\n\t\t2. Display Books\n\t\t3. Search Book\n\t\t4. Delete Book\n\t\t5. Update Book\n\t\t6. Search By Id\n\t\t7.Exit";
cout<<"\n------------------------------------------------------------\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter Book ID: ";
cin >> id;
cin.ignore();
cout << "Enter Title: ";
getline(cin, title);
cout << "Enter Author: ";
getline(cin, author);
lib.addBook(id, title, author);
break;
case 2:
lib.displayBooks();
break;
case 3:
cout << "Enter title to search: ";
cin.ignore();
getline(cin, title);
lib.searchBook(title);
break;
case 4:
cout << "Enter Book ID to delete: ";
cin >> id;
lib.deleteBook(id);
break;
case 5:
cout << "Enter Book ID to update: ";
cin >> id;
lib.updateBook(id);
break;
case 6:
cout << "Enter Book ID to search: ";
cin >> id;
lib.searchBookById(id);
break;
case 7:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 7);
return 0;
}