#include <iostream> #include <string> // Book class class Book { public: std::string title; std::string author; std::string genre; Book(std::string title, std::string author, std::string genre) { this->title = title; this->author = author; this->genre = genre; } }; // Linked list node class class Node { public: Book data; Node(Book data) { this->data = data; } }; // Linked list class class LinkedList { public: Node* head; LinkedList() { head = nullptr; } void add(Book data) { Node* newNode = new Node(data); if (head == nullptr) { head = newNode; } else { } } } Book search(std::string title) { if (current->data.title == title) { return current->data; } } return Book("", "", ""); } }; // Stack class class Stack { public: Book* items; int top; Stack() { items = new Book[10]; top = -1; } void push(Book item) { items[++top] = item; } Book pop() { return items[top--]; } bool isEmpty() { return top == -1; } }; // Queue class class Queue { public: Book* items; int front; int rear; Queue() { items = new Book[10]; front = rear = 0; } void enqueue(Book item) { items[rear++] = item; } Book dequeue() { return items[front++]; } bool isEmpty() { return front == rear; } }; // Library class class Library { public: LinkedList books; LinkedList borrowers; Stack loans; Queue reservations; void addBook(std::string title, std::string author, std::string genre) { Book newBook(title, author, genre); books.add(newBook); } void borrowBook(std::string title) { Book book = books.search(title); if (book.title != "") { loans.push(book); std::cout << title << " borrowed successfully!" << std::endl; } } void returnBook() { if (!loans.isEmpty()) { Book book = loans.pop(); std::cout << book.title << " returned successfully!" << std::endl; } } void reserveBook(std::string title) { reservations.enqueue(Book(title, "", "")); std::cout << title << " reserved successfully!" << std::endl; } void displayBooks() { std::cout << current->data.title << std::endl; } } }; int main() { Library library; while (true) { std::cout << "1. Add book" << std::endl; std::cout << "2. Borrow book" << std::endl; std::cout << "3. Return book" << std::endl; std::cout << "4. Reserve book" << std::endl; std::cout << "5. Display books" << std::endl; std::cout << "6. Exit" << std::endl; int choice; std::cin >> choice; switch (choice) { case 1: { std::string title, author, genre; std::cout << "Enter book title: "; std::cin >> title; std::cout << "Enter book author: "; std::cin >> author; std::cout << "Enter book genre: "; std::cin >> genre; library.addBook(title, author, genre); break; } case 2: { std::string title; std::cout << "Enter book title to borrow: "; std::cin >> title; library.borrowBook(title); break; } case 3: { library.returnBook(); break; } case 4: { std::string title; std::cout << "Enter book title to reserve: "; std::cin >> title; library.reserveBook(title); break; } case 5: {
Standard input is empty
#include <iostream>
#include <string>
// Book class
class Book {
public:
std::string title;
std::string author;
std::string genre;
Book(std::string title, std::string author, std::string genre) {
this->title = title;
this->author = author;
this->genre = genre;
}
};
// Linked list node class
class Node {
public:
Book data;
Node* next;
Node(Book data) {
this->data = data;
this->next = nullptr;
}
};
// Linked list class
class LinkedList {
public:
Node* head;
LinkedList() {
head = nullptr;
}
void add(Book data) {
Node* newNode = new Node(data);
if (head == nullptr) {
head = newNode;
} else {
Node* current = head;
while (current->next) {
current = current->next;
}
current->next = newNode;
}
}
Book search(std::string title) {
Node* current = head;
while (current) {
if (current->data.title == title) {
return current->data;
}
current = current->next;
}
return Book("", "", "");
}
};
// Stack class
class Stack {
public:
Book* items;
int top;
Stack() {
items = new Book[10];
top = -1;
}
void push(Book item) {
items[++top] = item;
}
Book pop() {
return items[top--];
}
bool isEmpty() {
return top == -1;
}
};
// Queue class
class Queue {
public:
Book* items;
int front;
int rear;
Queue() {
items = new Book[10];
front = rear = 0;
}
void enqueue(Book item) {
items[rear++] = item;
}
Book dequeue() {
return items[front++];
}
bool isEmpty() {
return front == rear;
}
};
// Library class
class Library {
public:
LinkedList books;
LinkedList borrowers;
Stack loans;
Queue reservations;
void addBook(std::string title, std::string author, std::string genre) {
Book newBook(title, author, genre);
books.add(newBook);
}
void borrowBook(std::string title) {
Book book = books.search(title);
if (book.title != "") {
loans.push(book);
std::cout << title << " borrowed successfully!" << std::endl;
}
}
void returnBook() {
if (!loans.isEmpty()) {
Book book = loans.pop();
std::cout << book.title << " returned successfully!" << std::endl;
}
}
void reserveBook(std::string title) {
reservations.enqueue(Book(title, "", ""));
std::cout << title << " reserved successfully!" << std::endl;
}
void displayBooks() {
Node* current = books.head;
while (current) {
std::cout << current->data.title << std::endl;
current = current->next;
}
}
};
int main() {
Library library;
while (true) {
std::cout << "1. Add book" << std::endl;
std::cout << "2. Borrow book" << std::endl;
std::cout << "3. Return book" << std::endl;
std::cout << "4. Reserve book" << std::endl;
std::cout << "5. Display books" << std::endl;
std::cout << "6. Exit" << std::endl;
int choice;
std::cin >> choice;
switch (choice) {
case 1: {
std::string title, author, genre;
std::cout << "Enter book title: ";
std::cin >> title;
std::cout << "Enter book author: ";
std::cin >> author;
std::cout << "Enter book genre: ";
std::cin >> genre;
library.addBook(title, author, genre);
break;
}
case 2: {
std::string title;
std::cout << "Enter book title to borrow: ";
std::cin >> title;
library.borrowBook(title);
break;
}
case 3: {
library.returnBook();
break;
}
case 4: {
std::string title;
std::cout << "Enter book title to reserve: ";
std::cin >> title;
library.reserveBook(title);
break;
}
case 5: {