-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibraryManagementSystem.cpp
More file actions
325 lines (266 loc) · 8.37 KB
/
LibraryManagementSystem.cpp
File metadata and controls
325 lines (266 loc) · 8.37 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "LibraryManagementSystem.h"
shared_ptr<BookNode> LibraryManagementSystem::getBookAtIndex(int index) {
shared_ptr<BookNode> current;
if (index <= length / 2) {
current = head;
for (int i = 0; i < index; i++) {
current = current->next;
}
} else {
current = tail;
for (int i = length-1; i > index; i--) {
current = current->prev;
}
}
return current;
}
void LibraryManagementSystem::selectionSort(const function<bool(shared_ptr<BookNode>, shared_ptr<BookNode>)>& comparator){
if (!head) return;
auto current = head;
while (current)
{
auto minOrMax = current;
auto next = current->next;
while (next)
{
if (comparator(minOrMax, next)) minOrMax = next;
next = next->next;
}
if (minOrMax != current){
if (minOrMax->prev) minOrMax->prev->next = minOrMax->next;
if (minOrMax->next) minOrMax->next->prev = minOrMax->prev;
if (current == head) head = minOrMax;
minOrMax->prev = current->prev;
minOrMax->next = current;
if (current->prev) current->prev->next = minOrMax;
current->prev = minOrMax;
current = minOrMax;
}
current = current->next;
}
}
shared_ptr<BookNode> LibraryManagementSystem::Split(shared_ptr<BookNode> head){
auto slow = head;
auto fast = head;
while(fast->next && fast->next->next){
slow = slow->next;
fast = fast->next->next;
}
auto temp = slow->next;
slow->next = nullptr;
if (temp != nullptr ) temp->prev = nullptr;
return temp;
}
shared_ptr<BookNode> LibraryManagementSystem::merge(shared_ptr<BookNode> left, shared_ptr<BookNode> right, function<bool(shared_ptr<BookNode>, shared_ptr<BookNode>)> comparator){
if (!left) return right;
if (!right) return left;
if (comparator(left, right)){
right->next = merge(left, right->next, comparator);
if (right->next) right->next->prev = right;
right->prev = nullptr;
return right;
}
else{
left->next = merge(left->next, right, comparator);
if (left->next) left->next->prev = left;
left->prev = nullptr;
return left;
}
}
shared_ptr<BookNode> LibraryManagementSystem::MergeSort(shared_ptr<BookNode> head, function<bool(shared_ptr<BookNode>, shared_ptr<BookNode>)> comparator){
if (!head || !head->next) return head;
auto mid = Split(head);
head = MergeSort(head, comparator);
mid = MergeSort(mid, comparator);
return merge(head, mid, comparator);
}
LibraryManagementSystem::LibraryManagementSystem() : head(nullptr), tail(nullptr), length(0){}
void LibraryManagementSystem::push_front(shared_ptr<BookNode> Book)
{
if (!head)
{
head = tail = Book;
}
else
{
Book->next = head;
head->prev = Book;
head = Book;
}
length++;
}
void LibraryManagementSystem::push_back(shared_ptr<BookNode> Book)
{
if (!head)
{
head = tail = Book;
}
else
{
Book->prev = tail;
tail->next = Book;
tail = Book;
}
length++;
}
void LibraryManagementSystem::insert(shared_ptr<BookNode> Book, int Index) {
if (!head) {
cout << "Library is empty." << endl;
cout << "Pushing the book to the end of the library." << endl;
push_back(Book);
return;
}
if (Index < 0 || Index >= length) {
cout << "Index out of range." << endl;
return;
}
if (Index == 0) {
push_front(Book);
return;
}
if (Index == length - 1) {
push_back(Book);
return;
}
shared_ptr<BookNode> current = getBookAtIndex(Index);
// Update pointers to insert the new node
Book->next = current;
Book->prev = current->prev;
current->prev->next = Book;
current->prev = Book;
// Increment the length of the library
length++;
}
void LibraryManagementSystem::remove(int Index)
{
// If the library is empty, print an error message and do nothing
if (!head)
{
cout << "Library is empty." << endl;
return;
}
//check the validity of the index
if (Index < 0 || Index >= length)
{
cout << "Index out of range." << endl;
return;
}
if (Index == 0)
{
pop_front();
return;
}
if (Index == length - 1)
{
pop_back();
return;
}
shared_ptr<BookNode> current = getBookAtIndex(Index);
// If the book to be deleted is not the head or tail of the library, update the next and previous pointers of the adjacent nodes
current->prev->next = current->next; // Set the previous node's next pointer to the next node
current->next->prev = current->prev; // Set the next node's previous pointer to the previous node
// Decrement the length of the library
length--;
}
void LibraryManagementSystem::pop_back() {
if (!head)
{
cout << "Library is empty." << endl;
return;
}
if (head == tail) head = tail = nullptr;
else
{
tail = tail->prev;
tail->next = nullptr;
}
length--;
}
void LibraryManagementSystem::pop_front() {
if (!head)
{
cout << "Library is empty." << endl;
return;
}
if (head == tail) head = tail = nullptr;
else
{
head = head->next;
head->prev = nullptr;
}
length--;
}
shared_ptr<BookNode> LibraryManagementSystem::searchByID(int id)
{
if (!head)
{
cout << "Library is empty." << endl;
return nullptr;
}
shared_ptr<BookNode> left = head, right = tail;
while (left && right && (left != right->next))
{
if (left->getId() == id) return left;
if (right->getId() == id) return right;
left = left->next;
right = right->prev;
}
return nullptr; // Book not found
}
void LibraryManagementSystem::sort(function<bool(shared_ptr<BookNode>, shared_ptr<BookNode>)> comparator, string type){
if (type == "selection") selectionSort(comparator);
if (type == "merge"){
head = MergeSort(head, comparator);
auto current = head;
while (current->next) current = current->next;
tail = current;
}
}
void LibraryManagementSystem::display()
{
if (!head)
{
cout << "The library is empty." << endl;
return;
}
int IDPADDING = head->getIdPadding(),
NAMEPADDING = head->getNamePadding(),
AUTHORMARGEN = head->getAuthorPadding(),
GENREMARGEN = head->getGenrePadding(),
QMARGEN = max(head->getQuantityPadding(), 8)+1;
cout << "+" << string(IDPADDING + 1, '-') << "+"
<< string(NAMEPADDING+1, '-') << "+"
<< string(AUTHORMARGEN+1, '-') << "+"
<< string(GENREMARGEN+1, '-') << "+"
<< string(QMARGEN, '-') << "+\n";
// Print table header
cout << left << "|" << setw(IDPADDING) << "ID"
<< " |" << setw(NAMEPADDING) << "Book Name"
<< " |" << setw(AUTHORMARGEN) << "Author Name"
<< " |" << setw(GENREMARGEN) << "Genre"
<< " |" << setw(QMARGEN) << "Quantity" << "|\n";
cout << "+" << string(IDPADDING + 1, '-') << "+" << string(NAMEPADDING + 1, '-') << "+" << string(AUTHORMARGEN + 1, '-') << "+" << string(GENREMARGEN + 1, '-') << "+" << string(QMARGEN, '-') << "+\n";
// Traverse the list and print book details
auto temp = head;
while (temp)
{
cout << left << "|" << setw(IDPADDING) << temp->getId()
<< " |" << setw(NAMEPADDING) << temp->getBookName()
<< " |" << setw(AUTHORMARGEN) << temp->getAuthorName()
<< " |" << setw(GENREMARGEN) << temp->getBookGenre()
<< " |" << setw(QMARGEN) << temp->getQuantity() << "|\n";
temp = temp->next;
}
cout << "+" << string(IDPADDING + 1, '-') << "+" << string(NAMEPADDING + 1, '-') << "+" << string(AUTHORMARGEN + 1, '-') << "+" << string(GENREMARGEN + 1, '-') << "+" << string(QMARGEN, '-') << "+\n";
}
void LibraryManagementSystem::clear() {
while (head) {
pop_front(); // Remove the front element until the list is empty
}
tail.reset(); // Ensure the tail pointer is reset to nullptr
length = 0; // Reset the length counter
cout << "Library cleared." << endl;
}
LibraryManagementSystem::~LibraryManagementSystem() {
clear();
}