Please define two functions below : 1. void reverse(LinkedList &LL) 2. LinkedList operator + (const LinkedList &LL1, con

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Please define two functions below : 1. void reverse(LinkedList &LL) 2. LinkedList operator + (const LinkedList &LL1, con

Post by answerhappygod »

Please define two functions below :
1. void reverse(LinkedList &LL)
2. LinkedList operator +(const LinkedList &LL1, constLinkedList &LL2)
Code description is in the code.
Please try to define them in one function.
I'll absolutely upvote your answer if they work properly. Thankyou.
///////////////////////Code startshere/////////////////////////
#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;
voidsetData(const int &);
voidsetLink(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(constint &d) { data = d; }
void Node::setLink(constNodePtr &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 thehead pointer member variable a nullptr value
LinkedList(const LinkedList&); //copy constructor (deep copy)
~LinkedList(); //destructor (must delete all thenodes from the heap)
LinkedList& operator=(const LinkedList &); //Assignment operator(deep copy)
bool isEmpty()const; //return true if the length of the callingobject 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 callingobject
voidhead_insert(const int &);//as described in the supplementary material
NodePtr search_node(constint &) const; //as describedin the supplementary material
voidinsert_after(const NodePtr &,const int &)const; //as described in the supplementarymaterial
voidremove_node(const NodePtr &); //as describedin the supplementary material
voidremove_node(const int &);//as described in the supplementary material
voidremove_all(const int &); //asdescribed in the supplementary material
voidtail_insert(const int &);//as described in the supplementary material
voidinsert_before(const NodePtr &,const int &); //as describedin 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()
{
//delete [] head;
head = nullptr;
}
LinkedList& LinkedList::operator=(const LinkedList &L)
{
this->head = L.head;
return*this;
}
bool LinkedList::isEmpty()const
{
if(this->getLength() == 0)
{
returntrue;
}
returnfalse;
}
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;
}
voidLinkedList::head_insert(const int&v)
{
NodePtr n = new Node(v);
n->setLink(head);
head = n;
}
NodePtr LinkedList::search_node(constint &v) const
{
NodePtr temp = head;
while(temp !=nullptr)
{
if(temp->getData() == v)
return temp;
else
temp =temp->getLink(); //i++
}
returnnullptr;
}
voidLinkedList::insert_after(const NodePtr &n,const int &v)const
{
if(n ==nullptr)
return;
else
{
NodePtr temp = newNode(v);
temp->setLink(n->getLink());//temp->link = n->getLink()
n->setLink(temp);
}
}
voidLinkedList::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();
}
}
}
voidLinkedList::remove_node(const int&v)
{
NodePtr n = search_node(v);
remove_node(n);
}
voidLinkedList::remove_all(const int&v)
{
NodePtr n;
do
{
n = search_node(v);
remove_node(n);
}while(n !=nullptr);
}
voidLinkedList::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);
}
}
voidLinkedList::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. Badnode argument." << endl;
else
insert_after(b, v);
}
ostream& operator << (ostream&out, const LinkedList &LL)
{
if (LL.isEmpty())
out << "EMPTY LINKEDLIST";
else
{
NodePtr temp = LL.head;
while(temp !=nullptr)
{
out << *temp<< " ";
temp =temp->getLink();
}
}
return out;
}
void reverse(LinkedList &LL)
{
/*
This function reverses the linked list objectparameter LL. Reversing a linked list means the order of the nodesin the linked list is reversed.
*/
//Put your code here
}
LinkedList operator + (constLinkedList &LL1, const LinkedList&LL2)
{
/*
This function first asserts that the lengths ofthe linked list object parameters LL1 and LL2 are equal and thenadds the bits in LL1 with the bits in LL2 the way we add binarynumbers in mathematics. The function stores the sum in a linkedlist object and then returns the linked list object. Assume thedata in each node of the linked lists is a bit (0 or 1).
*/
assert(LL1.getLength() == LL2.getLength());
//Put your remaining code here
}
///////////////////////Code endshere/////////////////////////
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply