Please define the following two functions :
void flipBits(LinkedList &LL)
void reverse(LinkedList &LL)
Code description is onside the code
I'll absolutely upvote your answer if it works properly. Thankyou.
//////////////////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;
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 flipBits(LinkedList &LL)
{
/*
This function flips the bits stored in the nodesof the linked list object parameter LL. Assume the data in eachnode of the linked list is a bit (0 or 1).
*/
//Put your code here
}
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
}
///////////////////Code ends here/////////////////////
Please define the following two functions : void flipBits(LinkedList &LL) void reverse(LinkedList &LL) Code description
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am