Page 1 of 1

Please define the following two functions : 1. LinkedList operator + (const LinkedList &LL1, const LinkedList &LL2) 2. v

Posted: Sun Jul 10, 2022 11:30 am
by answerhappygod
Please define the following two functions :
1. LinkedList operator + (const LinkedList &LL1, const LinkedList &LL2)
2. void addOne(LinkedList &LL)
Code is described in the code below.
I'll absolutely upvote your answer if it works properly. Thank you.
The basic code is given. Please do not copy and paste an answer that does not belong to this question.
/////////////////////Code starts here/////////////////////
#include <iostream>
#include <cassert>
using namespace std;
class Node
{
typedef Node* NodePtr;
private:
int data;
NodePtr link;
public:
Node();
Node(const int &);
Node(const Node &);
int getData() const;
NodePtr getLink() const;
void setData(const int &);
void setLink(const NodePtr &);
friend ostream& operator << (ostream &, const Node &);
};
typedef Node* NodePtr;
Node::Node() : data(0), link(nullptr) {}
Node::Node(const int &d) : data(d), link(nullptr){}
Node::Node(const Node &n) : data(n.data), link(n.link){}
int Node::getData() const { return data; }
NodePtr Node::getLink() const { return link; }
void Node::setData(const int &d) { data = d; }
void Node::setLink(const NodePtr &p) { link = p; }
ostream& operator << (ostream& out, const Node& n)
{
out << n.data;
return out;
}
typedef Node* NodePtr;
class LinkedList
{
private:
NodePtr head;
public:
LinkedList(); //default constructor: assigns the head pointer member variable a nullptr value
LinkedList(const LinkedList &); //copy constructor (deep copy)
~LinkedList(); //destructor (must delete all the nodes from the heap)
LinkedList& operator= (const LinkedList &); //Assignment operator (deep copy)
bool isEmpty() const; //return true if the length of the calling object is 0 and false otherwise
NodePtr getHeadPtr() const; //return the head member variable of the calling object
int getLength() const; //return the number of nodes in the calling object
void head_insert(const int &); //as described in the supplementary material
NodePtr search_node(const int &) const; //as described in the supplementary material
void insert_after(const NodePtr &, const int &) const; //as described in the supplementary material
void remove_node(const NodePtr &); //as described in the supplementary material
void remove_node(const int &); //as described in the supplementary material
void remove_all(const int &); //as described in the supplementary material
void tail_insert(const int &); //as described in the supplementary material
void insert_before(const NodePtr &, const int &); //as described in the supplementary material
friend ostream& operator << (ostream&, const LinkedList &); //Implemented for you
};
LinkedList::LinkedList() : head(nullptr) {}
LinkedList::LinkedList(const LinkedList &L) { this->head = L.head; }
LinkedList::~LinkedList()
{
head = nullptr;
}
LinkedList& LinkedList::operator= (const LinkedList &L)
{
this->head = L.head;
return *this;
}
bool LinkedList::isEmpty() const
{
if(this->getLength() == 0)
{
return true;
}
return false;
}
NodePtr LinkedList::getHeadPtr() const
{
return (*this).head;
}
int LinkedList::getLength() const
{
int count = 0;
NodePtr temp = head;
while(temp != nullptr)
{
count++;
temp = temp->getLink();
}
return count;
}
void LinkedList::head_insert(const int &v)
{
NodePtr n = new Node(v);
n->setLink(head);
head = n;
}
NodePtr LinkedList::search_node(const int &v) const
{
NodePtr temp = head;
while(temp != nullptr)
{
if(temp->getData() == v)
return temp;
else
temp = temp->getLink(); //i++
}
return nullptr;
}
void LinkedList::insert_after(const NodePtr &n, const int &v) const
{
if(n == nullptr)
return;
else
{
NodePtr temp = new Node(v);
temp->setLink(n->getLink()); //temp->link = n->getLink()
n->setLink(temp);
}
}
void LinkedList::remove_node(const NodePtr &n)
{
//NodePtr temp = &head;
if(head == nullptr || n == nullptr)
return;
else if(head == n)
{
head = head->getLink();
delete n;
return;
}
else
{
NodePtr b = head;
while(b != nullptr)
{
if(b->getLink() == n)
{
b->setLink(n->getLink());
delete n;
return;
}
b = b->getLink();
}
}
}
void LinkedList::remove_node(const int &v)
{
NodePtr n = search_node(v);
remove_node(n);
}
void LinkedList::remove_all(const int &v)
{
NodePtr n;
do
{
n = search_node(v);
remove_node(n);
}while(n != nullptr);
}
void LinkedList::tail_insert(const int &v)
{
if(head == nullptr)
head_insert(v);
else
{
NodePtr b = head;
while(b->getLink() != nullptr)
b = b->getLink();
insert_after(b, v);
}
}
void LinkedList::insert_before(const NodePtr &n, const int &v)
{
//NodePtr b = &head;
if(head == nullptr)
{
if(n == nullptr)
{
head_insert(v);
}
else
{
cout << "Insertion failed. Bad node argument." << endl;
return;
}
}
if(n == nullptr)
{
tail_insert(v);
return;
}
if(head == n)
{
head_insert(v);
return;
}
NodePtr b = head;
while(b != nullptr)
{
if(b->getLink() == n)
break;
b = b->getLink();
}
if(b == nullptr)
cout << "Insertion failed. Bad node argument." << endl;
else
insert_after(b, v);
}
ostream& operator << (ostream &out, const LinkedList &LL)
{
if (LL.isEmpty())
out << "EMPTY LINKED LIST";
else
{
NodePtr temp = LL.head;
while(temp != nullptr)
{
out << *temp << " ";
temp = temp->getLink();
}
}
return out;
}
LinkedList operator + (const LinkedList &LL1, const LinkedList &LL2)
{
/*
This function first asserts that the lengths of the linked list object parameters LL1 and LL2 are equal and then adds the bits in LL1 with the bits in LL2 the way we add binary numbers in mathematics. The function stores the sum in a linked list object and then returns the linked list object. Assume the data in each node of the linked lists is a bit (0 or 1).
*/
assert(LL1.getLength() == LL2.getLength());
//Put your remaining code here
}
void addOne(LinkedList &LL)
{
/*
This function first asserts that the linked list object parameter LL is not empty and then adds 1 to the bits stored in the nodes of the linked list the way we add binary numbers in mathematics. Assume the data in each node of the linked list is a bit (0 or 1).
*/
assert(!LL.isEmpty());
//Put your remaining code here
}
/////////////////////Code ends here/////////////////////