Implement a single function below so that the program works properly. 1. int twosComplementBinaryToDecimal(const LinkedL
Posted: Tue Jul 12, 2022 8:04 am
Implement a single function below so that the program worksproperly.
1. int twosComplementBinaryToDecimal(const LinkedList&LL)
Function description is in the code below.
Please do not paste other answer for other question to thisquestion. I'll absolutely upvote your answer if it worksproperly. Thank you.
///////////////////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;}
NodePtrNode::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 pointermember variable a nullptr value
LinkedList(const LinkedList &); //copyconstructor (deep copy)
~LinkedList(); //destructor (must delete all the nodes from theheap)
LinkedList& operator=(const LinkedList &); //Assignmentoperator (deep copy)
bool isEmpty() const;//return true if the length of the calling object is 0 and falseotherwise
NodePtr getHeadPtr() const; //return thehead 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
NodePtrsearch_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;
}
NodePtrLinkedList::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)
{
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;
}
void reverse(LinkedList &LL)
{
/*
This function reverses the linked list object parameter LL.Reversing a linked list means the order of the nodes in the linkedlist is reversed.
*/
//Put your code here
LinkedList temp; //temp=12345 //54321
temp = LL;
NodePtr tempN = temp.getHeadPtr();
LL.~LinkedList();
while(tempN!= nullptr)
LL.head_insert(tempN->getData());
}
void flipBits(LinkedList &LL)
{
/*
This function flips the bits stored in the nodes of the linkedlist object parameter LL. Assume the data in each node of thelinked list is a bit (0 or 1).
*/
//Put your code here
NodePtr temp = LL.getHeadPtr();
while(temp!= nullptr)
{
if(temp->getData() == 0)
{
temp->setData(1);
}
else
temp->setData(0);
temp = temp->getLink();
}
}
int twosComplementBinaryToDecimal(const LinkedList&LL)
{
/*
This function first asserts that the linked list objectparameter LL is not empty and then computes and returns the decimalvalue represented by the bits of the nodes of the linked listassuming two's complement binary representation. Assume the data ineach node of the linked list is a bit (0 or 1).
*/
assert(!LL.isEmpty());
//Put your remaining code here
}
}
///////////////////Code ends here//////////////////
1. int twosComplementBinaryToDecimal(const LinkedList&LL)
Function description is in the code below.
Please do not paste other answer for other question to thisquestion. I'll absolutely upvote your answer if it worksproperly. Thank you.
///////////////////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;}
NodePtrNode::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 pointermember variable a nullptr value
LinkedList(const LinkedList &); //copyconstructor (deep copy)
~LinkedList(); //destructor (must delete all the nodes from theheap)
LinkedList& operator=(const LinkedList &); //Assignmentoperator (deep copy)
bool isEmpty() const;//return true if the length of the calling object is 0 and falseotherwise
NodePtr getHeadPtr() const; //return thehead 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
NodePtrsearch_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;
}
NodePtrLinkedList::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)
{
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;
}
void reverse(LinkedList &LL)
{
/*
This function reverses the linked list object parameter LL.Reversing a linked list means the order of the nodes in the linkedlist is reversed.
*/
//Put your code here
LinkedList temp; //temp=12345 //54321
temp = LL;
NodePtr tempN = temp.getHeadPtr();
LL.~LinkedList();
while(tempN!= nullptr)
LL.head_insert(tempN->getData());
}
void flipBits(LinkedList &LL)
{
/*
This function flips the bits stored in the nodes of the linkedlist object parameter LL. Assume the data in each node of thelinked list is a bit (0 or 1).
*/
//Put your code here
NodePtr temp = LL.getHeadPtr();
while(temp!= nullptr)
{
if(temp->getData() == 0)
{
temp->setData(1);
}
else
temp->setData(0);
temp = temp->getLink();
}
}
int twosComplementBinaryToDecimal(const LinkedList&LL)
{
/*
This function first asserts that the linked list objectparameter LL is not empty and then computes and returns the decimalvalue represented by the bits of the nodes of the linked listassuming two's complement binary representation. Assume the data ineach node of the linked list is a bit (0 or 1).
*/
assert(!LL.isEmpty());
//Put your remaining code here
}
}
///////////////////Code ends here//////////////////