Implement a single function below so that the program works properly.
1. int twosComplementBinaryToDecimal(const LinkedList &LL)
Function description is in the code below.
PLEASE! PLEASE! PLEASE DO NOT COPY AND PASTE OTHER PEOPLE'S ANSWER TO THIS QUESTION, THEY ARE DIFFERENT!
I'll NOT UPVOTE your answer if the answer doesn't work. BUT I'll ABSOLUTELY upvote your answer if it works properly. 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; }
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)
{
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 linked list 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 linked list object parameter LL. Assume the data in each node of the linked 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 object parameter LL is not empty and then computes and returns the decimal value represented by the bits of the nodes of the linked list assuming two's complement binary representation. 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//////////////////
Implement a single function below so that the program works properly. 1. int twosComplementBinaryToDecimal(const LinkedL
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am