0% found this document useful (0 votes)
35 views24 pages

Agam DSA Project

Uploaded by

Agam Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views24 pages

Agam DSA Project

Uploaded by

Agam Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

DSA PROJECT

on

Simple Library
Management System

Submitted By-
Name- Agam Singh
Program- B.Tech(Computer Science and
Engineering)
Lovely Professional University.
Phagwara
Source Code-
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

class Book {
public:
string title;
string author;
int id;
bool issued;

Book(string title, string author, int id)


{
this->title = title;
this->author = author;
this->id = id;
this->issued = false;
}
};

class Node {
public:
Book* book;
Node* next;

Node(Book* book)
{
this->book = book;
this->next = nullptr;
}
};

class LinkedList {
private:
Node* head;

public:
LinkedList()
{
head = nullptr;
}

void addBook(string title, string author, int id)


{
Book* newBook = new Book(title, author, id);
Node* newNode = new Node(newBook);
if (head == nullptr) {
head = newNode;
}
else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}

cout << "Book added successfully." << endl;


}

int searchBook(string title)


{
Node* temp = head;
int index = 0;
while (temp != nullptr) {
if (temp->book->title == title) {
return index;
}
temp = temp->next;
index++;
}
return -1;
}

void issueBook()
{
string title;
cout << "Enter book title to issue: ";
cin.ignore();
getline(cin, title);
int index = searchBook(title);
if (index != -1) {
Node* temp = getNodeAtIndex(index);
if (temp->book->issued) {
cout << "Book already issued." << endl;
}
else {
temp->book->issued = true;
cout << "Book issued successfully." << endl;
cout << "Book issued using stack." << endl;
}
}
else {
cout << "Book not found." << endl;
}
}
void returnBook()
{
string title;
cout << "Enter book title to return: ";
cin.ignore();
getline(cin, title);

int index = searchBook(title);


if (index != -1) {
Node* temp = getNodeAtIndex(index);
if (temp->book->issued) {
temp->book->issued = false;
cout << "Book returned successfully." << endl;
}
else {
cout << "Book is not issued." << endl;
}
}
else {
cout << "Book not found." << endl;
}
}

void merge(Book* arr[], int left, int mid, int right)


{
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;

Book** L = new Book*[n1];


Book** R = new Book*[n2];

for (i = 0; i < n1; i++)


L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];

i = 0;
j = 0;
k = left;

while (i < n1 && j < n2) {


if (L[i]->title <= R[j]->title) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}

delete[] L;
delete[] R;
}

void mergeSort(Book* arr[], int left, int right)


{
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

void listAllBooks()
{
if (head == nullptr) {
cout << "No books in the library." << endl;
return;
}

int count = getNodeCount();

Book** bookArray = new Book*[count];


Node* temp = head;

for (int i = 0; i < count; i++) {


bookArray[i] = temp->book;
temp = temp->next;
}

mergeSort(bookArray, 0, count - 1);

cout << "List of all books:" << endl;


for (int i = 0; i < count; i++) {
cout << "Title: " << bookArray[i]->title << endl;
cout << "Author: " << bookArray[i]->author <<
endl;
cout << "ID: " << bookArray[i]->id << endl;
cout << "Issued: " << (bookArray[i]->issued ? "Yes"
: "No") << endl;
cout << endl;
}

delete[] bookArray;
}

void deleteBook()
{
if (head == nullptr) {
cout << "No books in the library." << endl;
return;
}
string title;
cout << "Enter book title to delete: ";
cin.ignore();
getline(cin, title);

int index = searchBook(title);


if (index != -1) {
Node* nodeToDelete;
if (index == 0) {
nodeToDelete = head;
head = head->next;
}
else {
Node* prevNode = getNodeAtIndex(index - 1);
nodeToDelete = prevNode->next;
prevNode->next = nodeToDelete->next;
}
delete nodeToDelete->book;
delete nodeToDelete;
cout << "Book deleted successfully." << endl;
}
else {
cout << "Book not found." << endl;
}
}

private:
Node* getNodeAtIndex(int index)
{
Node* temp = head;
for (int i = 0; i < index; i++) {
temp = temp->next;
}
return temp;
}

int getNodeCount()
{
int count = 0;
Node* temp = head;
while (temp != nullptr) {
count++;
temp = temp->next;
}
return count;
}
};

int main()
{
LinkedList library;
int choice;

do {
cout << "\nLibrary Management System\n";
cout << "1. Add Book\n";
cout << "2. Search Book\n";
cout << "3. Issue Book\n";
cout << "4. Return Book\n";
cout << "5. List All Books\n";
cout << "6. Delete Book\n";
cout << "0. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
string title, author;
int id;

cout << "Enter book title: ";


cin.ignore();
getline(cin, title);

cout << "Enter book author: ";


getline(cin, author);

cout << "Enter book ID: ";


cin >> id;

library.addBook(title, author, id);


break;
}
case 2: {
string title;

cout << "Enter book title to search: ";


cin.ignore();
getline(cin, title);

int index = library.searchBook(title);


if (index != -1) {
cout << "Book found at index " << index << "."
<< endl;
}
else {
cout << "Book not found." << endl;
}
break;
}
case 3:
library.issueBook();
break;
case 4:
library.returnBook();
break;
case 5:
library.listAllBooks();
break;
case 6:
library.deleteBook();
break;
case 0:
cout << "Exiting the program. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
break;
}
} while (choice != 0);

return 0;
}

Output-

You might also like