In: Computer Science
Add a recursive Boolean function called bool
isGreater(int compare) to class IntegerLinkedList to check
if any of the linked list data values is greater than the
given parameter, compare.
For example, isGreater(25) should return true and isGreater(100)
should return false for the the linked list shown below. A main
function (prob3.cpp) is given to you to add data values to the
linked list and test your function. Other examples are given in the
main function.
// ADD ANSWER TO THIS FILE
#pragma once
class SNode {
public:
int data;
SNode *next;
};
class IntegerLinkedList {
private:
SNode *head;
bool isGreater (SNode *ptr, int compare) {
return false; // COMPLETE THIS FOR PROBLEM 3
}
public:
IntegerLinkedList() {
head = nullptr;
}
void addFront(int x) {
SNode *tmp = head;
head = new SNode;
head->next = tmp;
head->data = x;
}
int getIndexLargest(); // COMPLETE THIS FOR PROBLEM 2
// recursion helper function called from main
bool isGreaterHelper (int compare) {
return isGreater(head, compare);
}
};
Program
//#pragma once
#include<iostream>
using namespace std;
class SNode {
public:
int data;
SNode *next;
};
class IntegerLinkedList {
private:
SNode *head;
bool isGreater (SNode *ptr, int compare)
{
if(ptr==nullptr) return false;
if(ptr->data>compare) return true;
return isGreater(ptr->next, compare);
}
public:
IntegerLinkedList() {
head = nullptr;
}
void addFront(int x) {
SNode *tmp = head;
head = new SNode;
head->next = tmp;
head->data = x;
}
//int getIndexLargest(); // COMPLETE THIS FOR PROBLEM 2
// recursion helper function called from main
bool isGreaterHelper (int compare) {
return isGreater(head, compare);
}
};
int main()
{
IntegerLinkedList *ob = new IntegerLinkedList();
ob->addFront(10);
ob->addFront(10);
ob->addFront(20);
ob->addFront(30);
ob->addFront(40);
ob->addFront(50);
bool f = ob->isGreaterHelper(100);
cout<<boolalpha<<f;
return 0;
}