fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. // Book class
  5. class Book {
  6. public:
  7. std::string title;
  8. std::string author;
  9. std::string genre;
  10.  
  11. Book(std::string title, std::string author, std::string genre) {
  12. this->title = title;
  13. this->author = author;
  14. this->genre = genre;
  15. }
  16. };
  17.  
  18. // Linked list node class
  19. class Node {
  20. public:
  21. Book data;
  22. Node* next;
  23.  
  24. Node(Book data) {
  25. this->data = data;
  26. this->next = nullptr;
  27. }
  28. };
  29.  
  30. // Linked list class
  31. class LinkedList {
  32. public:
  33. Node* head;
  34.  
  35. LinkedList() {
  36. head = nullptr;
  37. }
  38.  
  39. void add(Book data) {
  40. Node* newNode = new Node(data);
  41. if (head == nullptr) {
  42. head = newNode;
  43. } else {
  44. Node* current = head;
  45. while (current->next) {
  46. current = current->next;
  47. }
  48. current->next = newNode;
  49. }
  50. }
  51.  
  52. Book search(std::string title) {
  53. Node* current = head;
  54. while (current) {
  55. if (current->data.title == title) {
  56. return current->data;
  57. }
  58. current = current->next;
  59. }
  60. return Book("", "", "");
  61. }
  62. };
  63.  
  64. // Stack class
  65. class Stack {
  66. public:
  67. Book* items;
  68. int top;
  69.  
  70. Stack() {
  71. items = new Book[10];
  72. top = -1;
  73. }
  74.  
  75. void push(Book item) {
  76. items[++top] = item;
  77. }
  78.  
  79. Book pop() {
  80. return items[top--];
  81. }
  82.  
  83. bool isEmpty() {
  84. return top == -1;
  85. }
  86. };
  87.  
  88. // Queue class
  89. class Queue {
  90. public:
  91. Book* items;
  92. int front;
  93. int rear;
  94.  
  95. Queue() {
  96. items = new Book[10];
  97. front = rear = 0;
  98. }
  99.  
  100. void enqueue(Book item) {
  101. items[rear++] = item;
  102. }
  103.  
  104. Book dequeue() {
  105. return items[front++];
  106. }
  107.  
  108. bool isEmpty() {
  109. return front == rear;
  110. }
  111. };
  112.  
  113. // Library class
  114. class Library {
  115. public:
  116. LinkedList books;
  117. LinkedList borrowers;
  118. Stack loans;
  119. Queue reservations;
  120.  
  121. void addBook(std::string title, std::string author, std::string genre) {
  122. Book newBook(title, author, genre);
  123. books.add(newBook);
  124. }
  125.  
  126. void borrowBook(std::string title) {
  127. Book book = books.search(title);
  128. if (book.title != "") {
  129. loans.push(book);
  130. std::cout << title << " borrowed successfully!" << std::endl;
  131. }
  132. }
  133.  
  134. void returnBook() {
  135. if (!loans.isEmpty()) {
  136. Book book = loans.pop();
  137. std::cout << book.title << " returned successfully!" << std::endl;
  138. }
  139. }
  140.  
  141. void reserveBook(std::string title) {
  142. reservations.enqueue(Book(title, "", ""));
  143. std::cout << title << " reserved successfully!" << std::endl;
  144. }
  145.  
  146. void displayBooks() {
  147. Node* current = books.head;
  148. while (current) {
  149. std::cout << current->data.title << std::endl;
  150. current = current->next;
  151. }
  152. }
  153. };
  154.  
  155. int main() {
  156. Library library;
  157.  
  158. while (true) {
  159. std::cout << "1. Add book" << std::endl;
  160. std::cout << "2. Borrow book" << std::endl;
  161. std::cout << "3. Return book" << std::endl;
  162. std::cout << "4. Reserve book" << std::endl;
  163. std::cout << "5. Display books" << std::endl;
  164. std::cout << "6. Exit" << std::endl;
  165. int choice;
  166. std::cin >> choice;
  167.  
  168. switch (choice) {
  169. case 1: {
  170. std::string title, author, genre;
  171. std::cout << "Enter book title: ";
  172. std::cin >> title;
  173. std::cout << "Enter book author: ";
  174. std::cin >> author;
  175. std::cout << "Enter book genre: ";
  176. std::cin >> genre;
  177. library.addBook(title, author, genre);
  178. break;
  179. }
  180. case 2: {
  181. std::string title;
  182. std::cout << "Enter book title to borrow: ";
  183. std::cin >> title;
  184. library.borrowBook(title);
  185. break;
  186. }
  187. case 3: {
  188. library.returnBook();
  189. break;
  190. }
  191. case 4: {
  192. std::string title;
  193. std::cout << "Enter book title to reserve: ";
  194. std::cin >> title;
  195. library.reserveBook(title);
  196. break;
  197. }
  198. case 5: {
Success #stdin #stdout 0.02s 25896KB
stdin
Standard input is empty
stdout
#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: {