-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibraryManagementSystem.h
More file actions
166 lines (145 loc) · 5.23 KB
/
LibraryManagementSystem.h
File metadata and controls
166 lines (145 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef LMS_LIBRARYMANAGEMENTSYSTEM_H
#define LMS_LIBRARYMANAGEMENTSYSTEM_H
#include <memory>
#include <functional>
#include <iostream>
#include <iomanip>
#include <string>
#include "BookNode.h"
class LibraryManagementSystem {
private:
shared_ptr<BookNode> head, tail;
int length;
shared_ptr<BookNode> getBookAtIndex(int index);
/**
* @brief Sorts the linked list of books in either ascending or descending order using selection sort.
*
* Example:
* Consider a list of books with IDs: 3 -> 1 -> 4 -> 2. Using selection sort with an ascending comparator,
* the list will be rearranged to: 1 -> 2 -> 3 -> 4.
* Using selection sort with a descending comparator, the list will be rearranged to: 4 -> 3 -> 2 -> 1.
*
* @param comparator A function that determines the sorting order.
* Example: comparator = [](shared_ptr<BookNode> a, shared_ptr<BookNode> b) { return a->getId() < b->getId(); }
*/
void selectionSort(const function<bool(shared_ptr<BookNode>, shared_ptr<BookNode>)>& comparator);
/**
* @brief Splits a doubly linked list into two halves.
*
* Example:
* Given a list: 1 -> 2 -> 3 -> 4 -> 5, the function splits it into:
* First half: 1 -> 2 -> 3
* Second half: 4 -> 5
*
* @param head The head of the list to split.
* @return A shared pointer to the head of the second half.
*/
shared_ptr<BookNode> Split(shared_ptr<BookNode> head);
/**
* @brief Merges two sorted doubly linked lists into one sorted list.
*
* Example:
* Given two sorted lists:
* List 1: 1 -> 3 -> 5
* List 2: 2 -> 4 -> 6
* The merged list will be: 1 -> 2 -> 3 -> 4 -> 5 -> 6.
*
* @param left The first sorted list.
* @param right The second sorted list.
* @param comparator A function that determines the merge order.
* @return A shared pointer to the head of the merged list.
*/
shared_ptr<BookNode> merge(shared_ptr<BookNode> left, shared_ptr<BookNode> right, function<bool(shared_ptr<BookNode>, shared_ptr<BookNode>)> comparator);
/**
* @brief Sorts a doubly linked list using merge sort.
*
* Example:
* Given an unsorted list: 4 -> 2 -> 5 -> 1 -> 3, the function will sort it to: 1 -> 2 -> 3 -> 4 -> 5.
*
* @param head The head of the list to sort.
* @param comparator A function that determines the sorting order.
* @return A shared pointer to the head of the sorted list.
*/
shared_ptr<BookNode> MergeSort(shared_ptr<BookNode> head, function<bool(shared_ptr<BookNode>, shared_ptr<BookNode>)> comparator);
public:
LibraryManagementSystem();
~LibraryManagementSystem();
/**
* @brief Adds a book to the front of the library.
*
* Example:
* Adding a book with ID 1 to the front of a list: 2 -> 3 results in: 1 -> 2 -> 3.
*
* @param Book The book node to add.
*/
void push_front(shared_ptr<BookNode> Book);
/**
* @brief Adds a book to the end of the library.
*
* Example:
* Adding a book with ID 4 to the end of a list: 1 -> 2 -> 3 results in: 1 -> 2 -> 3 -> 4.
*
* @param Book The book node to add.
*/
void push_back(shared_ptr<BookNode> Book);
/**
* @brief Inserts a book at a specified index.
*
* Example:
* Inserting a book with ID 2 at index 1 in the list: 1 -> 3 results in: 1 -> 2 -> 3.
*
* @param Book The book node to insert.
* @param Index The position to insert the book at.
*/
void insert(shared_ptr<BookNode> Book, int Index);
/**
* @brief Finds a book by its ID.
*
* Example:
* Searching for ID 2 in the list: 1 -> 2 -> 3 returns the node with ID 2.
*
* @param id The ID of the book to find.
* @return A shared pointer to the book node if found, nullptr otherwise.
*/
shared_ptr<BookNode> searchByID(int id);
/**
* @brief Deletes a book at a specified index.
*
* Example:
* Deleting a book at index 1 in the list: 1 -> 2 -> 3 results in: 1 -> 3.
*/
void remove(int index);
/**
* @brief Deletes a book from the back of the library.
*
* Example:
* Popping a book from the front of the list: 1 -> 2 -> 3 results in: 1 -> 2.
*/
void pop_back();
/**
* @brief Deletes a book from the front of the library.
*
* Example:
* Popping a book from the front of the list: 1 -> 2 -> 3 results in: 2 -> 3.
*/
void pop_front();
/**
* @brief Sorts the library using the specified sorting algorithm.
*
* Example:
* Calling sort with a comparator for ascending order on the list: 3 -> 1 -> 4 results in: 1 -> 3 -> 4.
*
* @param comparator The function that determines the sorting order.
* @param type The sorting algorithm to use ("selection" or "merge").
*/
void sort(function<bool(shared_ptr<BookNode>, shared_ptr<BookNode>)> comparator, string type = "selection");
/**
* @brief Displays the library in a tabular format.
*
* Example:
* For a list of books, the function outputs a table with columns for ID, Book Name, Author Name, Genre, and Quantity.
*/
void display();
void clear();
};
#endif //LMS_LIBRARYMANAGEMENTSYSTEM_H