Private struct Node This is a private type definition. It will model each of the
ID: 3902092 • Letter: P
Question
Private struct Node This is a private type definition. It will model each of the nodes that will serve as "chain links" in the list. Every time a book is inserted in the list, a new node needs to be created. This structure is private because its structure does not need to be known outside of the class. Book data; Node next; Node prev Private operator-(const BookList&) Assignment operator overloaded (private to :BookList& its use its use) Private BookList(const BookList) Copy constructor (private to Private _ head: Node Points to the first node in the list. Will be nullptr if the list is e The number of nodes in the list. Uses delete to free up every node in the list. When a node is freed up, the Book object it points to is also freed Initializes head to nullptr and size to zero. Calls Destroylist to free up all allocated size: unsigned int Private Private DestroyList(): void Public BookList Public Bisto stored in the list PublicInsert(Book,size t) Inserts this pointer to a Book object into the given position. Allocates a node to store this pointer to a Book object. If the list currently is empty, set head to this node. Otherwise the node is linked into the list size is updated. Returns true Notice that the BookStore AddBook method is : boolExplanation / Answer
Implemented Source Code:-
---------------------------------
#include<iostream>
#include<iomanip>
#include<string.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
class BookList
{
public:
int read(ifstream &infile);
int write(ofstream &outfile);
bool IsEmpty();
int size();
bool remove(int);
BookList();
~BookList();
bool Insert(struct Node *root, struct Node *new_node);
void preorderDisplay(struct Node* temp);
public:
int sizeofnodes;
struct Node *new_node, *root;
struct Node *get_node();
};
struct Node *BookList::get_node()
{
struct Node *temp;
temp = (struct Node *) malloc(sizeof(struct Node));
temp->prev = NULL;
temp->next = NULL;
return temp;
}
void BookList::preorderDisplay(struct Node* temp)
{
if(temp!=NULL)
{
cout<<temp->data<<"->";
preorderDisplay(temp->prev);
preorderDisplay(temp->next);
}
}
int BookList::read(ifstream &infile)
{
int item;
sizeofnodes=0;
struct Node *tmp, *parent;
root = NULL;
infile.open("data_binary.txt");//the data Contains list of integers
for(int i=0;i<10;i++)
{
infile>>item;
new_node = get_node();
new_node->data=item;
if (root == NULL)
root = new_node;
else
Insert(root, new_node);
sizeofnodes++;
}
infile.close();
return sizeofnodes;
}
int BookList::write(ofstream &outfile)
{
int fileLength=size();
outfile.open("BookList.txt",ios::app);
while(root!=NULL)
{
outfile<<root->data<<" ";
root=root->next;
}
return 0;
}
bool BookList::IsEmpty()
{
if(root==NULL)
return false;
else
return true;
}
int BookList::size()
{
int size=0;
if(root==NULL)
{
return -1;
}
else
{
while(root!=NULL)
{
size++;
root=root->next;
}
return size;
}
}
bool BookList::remove(int key)
{
struct Node *temp;
struct Node *parent;
struct Node *temp3;
temp=root;
parent=root;
if(temp==NULL)
printf(" The node is empty");
else
{
while(temp!=NULL && temp->data!=key)
{
parent=temp;
if(temp->data<key)
{
temp=temp->next;
}
else
{
temp=temp->prev;
}
}
}
temp3=temp;
if(temp==NULL)
printf(" The tree is Empty");
else if(temp==root)
{
if(temp->next==NULL && temp->prev==NULL)
{
root=NULL;
}
else if(temp->prev==NULL)
{
root=temp->next;
}
else if(temp->prev==NULL)
{
root=temp->next;
}
else
{
struct Node *temp1;
temp1 = temp->next;
while(temp1->prev!=NULL)
{
temp=temp1;
temp1=temp1->prev;
}
if(temp1!=temp->next)
{
temp->prev=temp1->next;
temp1->next=root->prev;
}
temp1->prev=root->prev;
root=temp1;
}
}
delete temp3;
}
bool BookList::Insert(struct Node *root, struct Node *new_node)
{
if(new_node->data < root->data)
{
if (root->prev == NULL)
root->prev = new_node;
else
Insert(root->prev, new_node);
}
if (new_node->data > root->data)
{
if(root->next == NULL)
root->next = new_node;
else
Insert(root->next, new_node);
}
}
BookList::BookList()
{
}
BookList::~BookList()
{
delete root;
}
int main()
{
ifstream infile;
ofstream outfile;
int datavalue,option;
BookList obj;
struct Node *get_node();
while(true)
{
cout<<" ** MENU **"<<endl;
cout<<" 1.Insert the Data"<<endl;
cout<<" 2.remove the Data"<<endl;
cout<<" 3.Size of Nodes"<<endl;
cout<<" 4.write data to the File"<<endl;
cout<<" 5.Display"<<endl;
cout<<" 6.exit"<<endl;
cout<<" Please Select any Choice"<<endl;
cin>>option;
switch(option)
{
case 1:
cout<<" Please Enter the New Node"<<endl;
cin>>datavalue;
obj.new_node = obj.get_node();
obj.new_node->data=datavalue;
if (obj.root == NULL)
obj.root = obj.new_node;
else
obj.Insert(obj.root, obj.new_node);
break;
case 2:
cout<<" Please Enter the Key to Delete:"<<endl;
cin>>datavalue;
obj.remove(datavalue);
break;
case 3:
obj.size();
break;
case 4:
obj.write(outfile);
break;
case 5:
obj.preorderDisplay(obj.root);
break;
case 6:
exit(0);
default:
cout<<" Invalid Option"<<endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.