Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file.
Write a recursive function to print the list in order.
Write a recursive function to print the list in reverse order.
Write a recursive function to print every other node in the list in order.
Write a recursive function to return the number of nodes in the list.
Write a Boolean function that implements the recursive version of sequential search.
THIS IS THE TEMPLATE BELOW
The functions needed are not included in the template
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include
using namespace std;
template
class LinkedList
{
private:
// Declare a structure for the list
struct ListNode
{
T value;
struct ListNode *next;
};
ListNode *head; // List head pointer
public:
LinkedList() // Constructor
{ head = nullptr; }
~LinkedList(); // Destructor
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList();
int search(T); // search function
T getTotal();
int numNodes();
T getAverage();
T getLargest();
int getLargestPosition();
T getSmallest();
int getSmallestPosition();
};
//**************************************************
// appendNode appends a node containing the value *
// pased into newValue, to the end of the list. *
//**************************************************
template
void LinkedList::appendNode(T newValue)
{
ListNode *newNode, *nodePtr = nullptr;
// Allocate a new node & store newValue
newNode = new ListNode;
newNode->value = newValue;
newNode->next = nullptr;
// If there are no nodes in the list
// make newNode the first node
if (!head)
head = newNode;
else // Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;
// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node
nodePtr->next = newNode;
}
}
//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************
template
void LinkedList::displayList()
{
ListNode *nodePtr = nullptr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
//**************************************************
// The insertNode function inserts a node with *
// newValue copied to its value member. *
//**************************************************
template
void LinkedList::insertNode(T newValue)
{
ListNode *newNode, *nodePtr, *previousNode = nullptr;
// Allocate a new node & store newValue
newNode = new ListNode;
newNode->value = newValue;
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = nullptr;
}
else // Otherwise, insert newNode
{
// Initialize nodePtr to head of list and
// previousNode to a null pointer.
nodePtr = head;
previousNode = nullptr;
// Skip all nodes whose value member is less
// than newValue.
while (nodePtr != nullptr && nodePtr->value < newValue)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == nullptr)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise, insert it after the prev. node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
//*****************************************************
// The deleteNode function searches for a node *
// with searchValue as its value. The node, if found, *
// is deleted from the list and from memory. *
//*****************************************************
template
void LinkedList::deleteNode(T searchValue)
{
ListNode *nodePtr, *previousNode = nullptr;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == searchValue)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to searchValue.
while (nodePtr != nullptr && nodePtr->value != searchValue)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
template
LinkedList::~LinkedList()
{
ListNode *nodePtr, *nextNode = nullptr;
nodePtr = head;
while (nodePtr != nullptr)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
//*****************************************************
// search member function *
//This function performs a linear search of the list. *
//*****************************************************
template
int LinkedList::search(T val)
{
int count = 1;
ListNode *nodePtr = nullptr;
nodePtr = head;
while (nodePtr)
{
if( nodePtr->value == val)
{
return count;
}
else
{
nodePtr = nodePtr->next;
count++;
}
}
return 0;
}
//*************************************************
// The getTotal function returns the total of *
// all the nodes in the list. *
//*************************************************
template
T LinkedList::getTotal()
{
T total = 0;
ListNode *nodePtr;
nodePtr = head;
while (nodePtr != NULL)
{
total = total + nodePtr->value;
nodePtr = nodePtr->next;
}
return total;
}
//************************************************
// The numNodes function returns the number of *
// nodes in the list. *
//************************************************
template
int LinkedList::numNodes()
{
int count = 0;
ListNode *nodePtr;
nodePtr = head;
while (nodePtr != NULL)
{
count++;
nodePtr = nodePtr->next;
}
return count;
}
//*****************************************************
// The getAverage function returns the average *
// of the values in the list. *
//*****************************************************
template
T LinkedList::getAverage()
{
return getTotal() / numNodes();
}
//*************************************************
// The getLargest function returns the largest *
// value in the list. *
//*************************************************
template
T LinkedList::getLargest()
{
T largest;
ListNode *nodePtr = NULL;
if (head != NULL)
{
largest = head->value;
nodePtr = head->next;
}
while (nodePtr != NULL)
{
if (nodePtr->value > largest)
largest = nodePtr->value;
nodePtr = nodePtr->next;
}
return largest;
}
//*************************************************
// The getLargestPosition function returns the *
// position of the largest value in the list. *
//*************************************************
template
int LinkedList::getLargestPosition()
{
T largest;
int position = -1;
int count = -1;
ListNode *nodePtr = head;
if (head != NULL)
{
largest = head->value;
position = 0;
nodePtr = head->next;
count = 1;
}
while (nodePtr != NULL)
{
if (nodePtr->value > largest)
{
largest = nodePtr->value;
position = count;
}
nodePtr = nodePtr->next;
count++;
}
return position;
}
//*************************************************
// The getSmallest function returns the smallest *
// value in the list. *
//*************************************************
template
T LinkedList::getSmallest()
{
T smallest;
ListNode *nodePtr = NULL;
if (head != NULL)
{
smallest = head->value;
nodePtr = head->next;
}
while (nodePtr != NULL)
{
if (nodePtr->value < smallest)
smallest = nodePtr->value;
nodePtr = nodePtr->next;
}
return smallest;
}
//*************************************************
// The getSmallestPosition function returns the *
// position of the smallest value in the list. *
//*************************************************
template
int LinkedList::getSmallestPosition()
{
T smallest;
int position = -1;
int count = -1;
ListNode *nodePtr = head;
if (head != NULL)
{
smallest = head->value;
position = 0;
nodePtr = head->next;
count = 1;
}
while (nodePtr != NULL)
{
if (nodePtr->value < smallest)
{
smallest = nodePtr->value;
position = count;
}
nodePtr = nodePtr->next;
count++;
}
return position;
}
#endifHere is the solution to above problem in C++. Please read the comments for more information and GIve a thumbs up if you like the soluton
C++ CODE
#include <iostream>
using namespace std;
//node
template <typename T>
class node
{
public:
T data;
node<T> * next;
};
//linked list template class
template <typename T>
class LinkedList
{
public :
node <T> * head;
LinkedList()
{
head=NULL;
}
//adding node to linked list
void addNode(T data)
{
if(head==NULL)
{
head=new node<T>();
head->data=data;
head->next=NULL;
return;
}
node<T> * ptr = head;
//going to end
while(ptr->next!=NULL)
ptr=ptr->next;
node<T> * temp = new node<T>();
temp->data= data;
temp->next=NULL;
ptr->next=temp;
}
//printing inorder sequentially
void printInOrder(node<T> * ptr)
{
if(ptr==NULL)
{
cout<<endl;
return;
}
//printing node and calling recursive function
cout<<ptr->data<<", ";
printInOrder(ptr->next);
}
void printReverse(node<T> * ptr)
{
if(ptr==NULL)
{
return;
}
//first calling recursive function and than printing
//resulting in reverse order
printReverse(ptr->next);
cout<<ptr->data<<", ";
}
//printing every other node
void printEveryOther(node<T> * ptr)
{
static int flag=0;
if(ptr==NULL)
{
cout<<endl;
return;
}
//using %2 to print all the even location linked list element
if(flag%2==0)
cout<<ptr->data<<", ";
flag++;
printEveryOther(ptr->next);
}
int noOfNodes(node<T> * ptr)
{
static int len=0;
if(ptr==NULL)
{
return len;
}
//using static variable to store total count in the linked
list
len++;
noOfNodes(ptr->next);
}
bool search(node<T> * ptr, T data)
{
if(ptr==NULL)
{
return false;
}
//comparing data and searching
if(ptr->data==data)
return true;
search(ptr->next,data);
}
};
int main() {
//adding nodes to linked list
LinkedList<int> l;
l.addNode(1);
l.addNode(2);
l.addNode(3);
l.addNode(4);
l.addNode(5);
l.addNode(6);
cout<<"PRINT IN ORDER"<<endl;
l.printInOrder(l.head);
cout<<"PRINT REVERSE ORDER"<<endl;
l.printReverse(l.head);
cout<<endl;
cout<<"EVERY OTHER NODE"<<endl;
l.printEveryOther(l.head);
cout<<"PRINT NUMBER OF NODES:
"<<l.noOfNodes(l.head)<<endl;;
cout<<"SEARCH THE NUMBER 4:"<<endl;
//searching for node 4
if(l.search(l.head,4))
cout<<"NUMBER FOUND";
else
cout<<"NOT FOUND";
return 0;
}
SCREENSHOT OF OUTPUT

Using the provided Linked List template, add the following recursive functions and demonstrate them in a...
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...
In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode { public: T data; ListNode<T>* next; ListNode(T data) { this->data = data;...
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
linked list operation
/***************************************************************************************
This function creates a new node with the information give as a parameter and looks
for the right place to insert it in order to keep the list organized
****************************************************************************************/
void insertNode(string first_name, string last_name, string phoneNumber)
{
ContactNode *newNode;
ContactNode *nodePtr;
ContactNode *previousNode = nullptr;
newNode = new ContactNode;
/***** assign new contact info to the new node here *****/
if (!head) // head points to nullptr meaning list is empty
{
head = newNode;...
can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...
What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...
Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has fields Node *headPtr, Node *tailPtr and int length, where the struct type Node has fields prev and next of type Node* along with data of type T. The prev and next pointers of each Node points to the previous and next Nodes in the list (or are respectively null in the case of the list’s head or tail node). We wish to detect "invalid"...
***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful. Also note that the function must work no matter how many nodes...