Question: Need help with these adding these functions to the code below.Please implement these functions to remove nodes from the list, as well as an alternate
Need help with these adding these functions to the code below.Please implement these functions to remove nodes from the list, as well as an alternate way of iterating through your nodes to print them using recursion.
Removal functions:
RemoveHead Deletes the first Node in the list.Returns whether or not the Node was removed
RemoveTail Deletes the last Node, returning whether or not the operation was successful
RemoveAt Deletes the index th Node from the list, returning whether or not the operation was successful
Remove Remove ALL Nodes containing values matching that of the passed in paramter Returns how many instances were removed
Clear Deletes all Nodes. Dont forget the node counthow many nodes do you have after you deleted all of them
Recursive Print Functions:
PrintForwardRecursive This function takes in a pointer to a Nodea starting node.From that node,recursively visit each node that follows, in forward order, and print their values. This function MUST be implemented using recursion, or tests using it will be
worth no points.
PrintReverseRecursive Same deal as PrintForwardRecursive, but in reverse. Must use recursion
#include
#include
#include
using namespace std;
template class T
class LinkedList
public:
class Node
public:
Node
thisnext thisprev nullptr;
T data;
Node next, prev;
;
LinkedList
head tail nullptr;
length ;
~LinkedList
if head nullptr
return;
Node current head;
Node prev nullptr;
while current nullptr
prev current;
current currentnext;
delete prev;
delete current;
LinkedListconst LinkedList& otherList
head nullptr;
tail nullptr;
length ;
Node current otherList.Head;
while current nullptr
AddTailcurrentdata;
current currentnext;
void AddHeadconst T data
if head nullptr
head new Node;
headdata data;
tail head;
else
Node newnode new Node;
newnodedata data;
newnodenext head;
headprev newnode;
head headprev;
length;
void AddTailconst T data
if tail nullptr
tail new Node;
taildata data;
head tail;
else
Node newnode new Node;
newnodedata data;
newnodeprev tail;
tailnext newnode;
tail tailnext;
length;
unsigned int NodeCount const
return length;
void PrintForward
Node ptr head;
while ptr nullptr
cout ptrdata endl;
ptr ptrnext;
void PrintReverse
Node ptr tail;
while ptr nullptr
cout ptrdata endl;
ptr ptrprev;
void AddNodesHeadconst T arr const unsigned int count
for int i count ; i ; i
AddHeadarri;
void AddNodesTailconst T arr const unsigned int count
for unsigned int i ; i count; i
AddTailarri;
Node Findconst T val
Node front head, back tail;
Node ptr nullptr;
if front nullptr && back nullptr
return nullptr;
while front back
if frontdata val
ptr front;
break;
else if backdata val
ptr back;
break;
front frontnext;
back backprev;
return ptr;
const Node Findconst T val const
Node front head, back tail;
Node ptr nullptr;
if front nullptr && back nullptr
return nullptr;
while front back
if frontdata val
ptr front;
break;
else if backdata val
ptr back;
break;
front frontnext;
back backprev;
return ptr;
void FindAllvector& nodes, const T searchval
Node ptr head;
while ptr nullptr
if ptrdata searchval
nodes.pushbackptr;
ptr ptrnext;
T& operatorconst unsigned int index
if index length throw outofrangeOut of Range";
Node ptr head;
for unsigned int i ; i index; i
ptr ptrnext;
return ptrdata;
Node Head const
return head;
Node Tail const
return tail;
Node GetNodeconst unsigned int index
if index length
throw outofrangeOut of Range";
Node ptr head;
for unsigned int i ; i index && ptr nullptr; i
ptr ptrnext;
return ptr;
LinkedList& operatorconst LinkedList& otherList
Node temp head;
Node prev nullptr;
while temp nullptr
prev temp;
temp tempnext;
delete prev;
head nullptr;
tail nullptr;
length ;
Node current otherList.Head;
while current nullptr
AddTalength ;
Node current otherList.Head;
while current I nullptr
AddTail current data;
current current
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
