Sei sulla pagina 1di 11

Data Structures & Algorithms

CSC 221

ASSIGNMENT
NO # 4

Muhammad Farhan
01-134182-097
Aizaz Ahmed
01-134182-010
BS CS-(3A)

Department of Computer Sciences


BAHRIA UNIVERSITY, ISLAMABAD
Problem Description

Library Management System

Book Node:

Create a BookNode class to store the Title, Author and ISBN of each book. Provide appropriate
constructors, get(), set() and display functions in the class.

Problem Description:

Write a C++ program which allows a librarian to manage data for a number of books and store
the entered data in a BST. Each node of the BST should contain an object of class BookNode. Data
is to be arranged with respect to book title.

You program should allow the librarian to perform any of the given functionalities by giving him
a menu to choose from. Basic functionalities include:
1) to add new books’ data
2) to search for a book using title as the search parameter and display all its information
3) to modify an existing book’s data
4) to delete a book’s record
5) to display the list of currently present books

CODE

//BNode.h:

#pragma once
#include<iostream>
#include<string>
#include<conio.h>

using namespace std;

class BNode
{
private:
string Title;
string Author;
int ISBN;
public:
BNode();
BNode* left;
BNode* right;
BNode();
BNode(string, string, int);
void setti();
void setau();
void setis();
string getti();
string getau();
int getis();
void display();
void Change(BNode**);
};

//BNode.cpp:

#include"BNode.h"

BNode::BNode()
{
Title = "\0";
Author = "\0";
ISBN = 0;
left = right = NULL;
}
BNode::BNode(string ti, string au, int is)
{
Title = ti;
Author = au;
ISBN = is;
left = right = NULL;
}
void BNode::setti()
{
cout << "Enter Title Name : ";
cin.ignore();
getline(cin, Title);
}
void BNode::setau()
{
cout << "Enter Author Name : ";
cin.ignore();
getline(cin, Author);
}
void BNode::setis()
{
cout << "Enter ISBN Number : ";
cin >> ISBN;
}
string BNode::getti()
{
return Title;
}
string BNode::getau()
{
return Author;
}
int BNode::getis()
{
return 0;
}
int BNode::getis()
{
return ISBN;
}
void BNode::display()
{
cout << "Book Name : " << Title << endl;
cout << "Author Name : " << Author << endl;
cout << "ISBN NUmber : " << ISBN << endl;
}
void BNode::Change(BNode** a)
{
char ch;
(*a)->display();
cout << endl;
cout << "Press '1' to change title." << endl;
cout << "Press '2' to change author name.";
cout << "Press '3' to change ISBN number.";
cout << "Press any key to exit.";
cin >> ch;
if (ch == '1')
{
(*a)->setti();
}
else if (ch == '2')
{
(*a)->setau();
}
else if (ch == '3')
{
(*a)->setis();
}
else
{
cout << "NO change in the data of available book.";
}
}

//Bst.h:

#pragma once
#include"BNode.h"
class BST
{
public:
BNode* root;
BST();
bool IsEmpty();
void Insert(BNode**, string, string, int);
bool Search(BNode*, string);
void Modify(BNode**, string);
void Delete(BNode**, string);
BNode* GetParent(BNode*, string);
BNode* FindMin(BNode*);
void displayBST(BNode*);
};

//BST.cpp:
#include "BST.h"

BST::BST()
{
this->root = NULL;
}
bool BST::IsEmpty()
{
bool status;
if (root == NULL)
{
status= true;
}
else
{
status= false;
}
return status;
}
void BST::Insert(BNode** temp, string TI, string AU, int IS)
{
if (*temp == NULL)
{
BNode* node = new BNode(TI, AU, IS);
*temp = node;
}
else
{
string mat = (*temp)->getti();
if (TI < mat)
{
Insert(&(*temp)->left, TI, AU, IS);
}
else if (TI >= mat)
{
Insert(&(*temp)->right, TI, AU, IS);
}
}
}
bool BST::Search(BNode* temp, string TI)
{
if (temp == NULL)
{
return false;
}
else
{
string mat = temp->getti();
if (TI == mat)
{
return true;
}
else if (TI > mat)
{
return Search(temp->right, TI);
}
else
{
return Search(temp->left, TI);
}
}
}
void BST::Modify(BNode** temp, string TI)
{
if (*temp == NULL)
{
cout << TI << " Book is not available in the library.";
}
else
{
string mat = (*temp)->getti();
if (TI == mat)
{
(*temp)->Change(&(*temp));
}
else if (TI < mat)
{
Modify(&(*temp)->left, TI);
}
else if (TI > mat)
{
Modify(&(*temp)->right, TI);
}
}
}
void BST::Delete(BNode** temp, string TI)
{
if (*temp == NULL)
{
cout << TI << " Book Not Found" << endl;
}
else
{
string mat = (*temp)->getti();
if (TI < mat)
{
Delete(&(*temp)->left, TI);
}
else if (TI > mat)
{
Delete(&(*temp)->right, TI);
}
else if (TI == mat)
{
if (((*temp)->left == NULL) && ((*temp)->right == NULL))
{
BNode *Par = GetParent(*temp, TI);
if (Par->left == *temp)
{
Par->left = NULL;
}
else if (Par->right == *temp)
{
Par->right = NULL;
}
*temp = NULL;
delete *temp;
}
else if ((((*temp)->left != NULL) && ((*temp)->right == NULL)) ||
(((*temp)->left == NULL) && ((*temp)->right != NULL)))
{
BNode *Par = GetParent(root, (*temp)->getti());
if ((*temp)->left != NULL)
{
if (Par->left == *temp)
{
Par->left = (*temp)->left;
*temp = Par->left;
}
else if (Par->right == *temp)
{
Par->right = (*temp)->left;
*temp = Par->right;
}
}
else if ((*temp)->right != NULL)
{
if (Par->left == *temp)
{
Par->left = (*temp)->right;
*temp = Par->right;
}
else if (Par->right == *temp)
{
Par->right = (*temp)->right;
*temp = Par->right;
}
}
}
else if (((*temp)->right != NULL) && ((*temp)->left != NULL))
{
BNode *min = FindMin((*temp)->right);
string m = min->getti();
Delete(&(*temp), min->getti());
(*temp)->getti() = m;
}
}
}
}
BNode* BST::GetParent(BNode* temp, string TI)
{
if (temp == NULL)
{
return NULL;
}
else if ((temp->left != NULL) && (temp->left->getti() == TI))
{
return temp;
}
else if ((temp->right != NULL) && (temp->right->getti() == TI))
{
return temp;
}
else if (TI < temp->getti())
{
return GetParent(temp->left, TI);
}
else if (TI > temp->getti())
{
return GetParent(temp->right, TI);
}
return temp;
}
void BST::displayBST(BNode* a)
{
if (a != NULL)
{
displayBST(a->left);
cout << endl;
a->display();
cout << endl;
displayBST(a->right);
}

BNode* BST::FindMin(BNode* temp)


{
if (temp->left == NULL)
{
return temp;
}
else
{
return FindMin(temp->left);
}
}

//Drive.cpp:

#include"BST.h"
void main()
{
BST tree;
char ch;
int isbnum;
bool status = false;
string Titlename, Authorname;
while (true)
{
cout << "Press '1' to add new books data."<<endl;
cout << "Press '2' to search for a book." << endl;
cout << "Press '3' to modify existing data for current book." << endl;
cout << "Press '4' to delete a books data." << endl;
cout << "Press '5' to display all books." << endl;
cout << "Press 'E' for exit. " << endl;
cin >> ch;
if (ch == 'E' || ch == 'e')
{
ch = 'E';
}
switch (ch)
{
case '1':
{
cout << "Enter name of book title : ";
cin.ignore();
getline(cin, Titlename);
cout << "Enter name Of book author : ";
cin.ignore();
getline(cin, Authorname);
cout << "Enter book ISBN number : ";
cin >> isbnum;
tree.Insert(&tree.root, Titlename, Authorname, isbnum);
break;
}
case '2':
{
cout << "Enter name of book title to search : ";
cin.ignore();
getline(cin, Titlename);
status = tree.Search(tree.root, Titlename);
cout << endl << endl;
if (status == true)
{
cout << Titlename << "Book is available in library.";
}
else
{
cout << Titlename << "Book is not available in library.";
}
break;
}
case '3':
{
cout << "Enter Book Title To Search : ";
cin.ignore();
getline(cin, Titlename);
cout << endl << endl;
tree.Modify(&tree.root, Titlename);
break;
}
case '4':
{
cout << "Enter Book Title To Delete : ";
cin.ignore();
getline(cin, Titlename);
cout << endl << endl;
tree.Delete(&tree.root, Titlename);
break;
}
case '5':
{
if (tree.root == NULL)
{
cout << "Library Is Currently Empty.";
}
else
{
tree.DisplayBST(tree.root);
}
break;
}
case 'E':
{
exit(0);
break;
}
default:
{
cout << endl;
cout << "You selected wrong option please try again";
}
}
cout << endl << endl;

}
_getch();
}
OUTPUT

Potrebbero piacerti anche