Question

In: Computer Science

Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of...

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);
}
  
};

Solutions

Expert Solution

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;
}


Related Solutions

Is there any way to do this function //Recursive method to check if two numbers form...
Is there any way to do this function //Recursive method to check if two numbers form a given number private static boolean addition(int[] array, int start, int end, int n) { if(start < end) { if (array[start] + array[end] == n) { return true; } if (array[start] + array[end] > n) { return addition(array, start, end-1, n); } if (array[start] + array[end] < n) { return addition(array, start+1, end, n); } } return false; }
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the...
public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the same length, return false if not the same // 2. return true if both given arrays are equals(same values in the same indices), false otherwise if(arr1.length != arr2.length){ return false; } for(int i = 0; i < arr1.length; i++){ if(arr1[i] != arr2[i]){ } } return true; } public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your code in main
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[],...
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search...
(JAVA) Balanced Class diagram: Balanced + swing(int[] a) : boolean For this exercise, you will have...
(JAVA) Balanced Class diagram: Balanced + swing(int[] a) : boolean For this exercise, you will have to implement the class diagram above. Basically, the objective of this exercise is to develop the class and the previous method, in such a way that it tells us if an array is balanced or not. We say that an array is balanced if the sum of the elements belonging to the first half of the array (not including the element in the middle),...
Write a recursive function to check if a string whose each character is stored in a...
Write a recursive function to check if a string whose each character is stored in a separate node in a doubly linked list, is a palindrome. Use the code available from DoublyLinkedList.hpp on our Github. // // Doubly-linked list with 2 dummy nodes // #pragma once #include <stdexcept> template<typename T> struct Node { T data; Node<T>* next; Node<T>* prev; Node() = delete; // Intentionally no default constructor Node( const T & element ) : data( element ), next( nullptr ),...
Develop an x86 assembly language program that properly executes an "is even" function: bool isEven(int value)...
Develop an x86 assembly language program that properly executes an "is even" function: bool isEven(int value) If the value passed in is even, return 1 If the value passed in is odd, return 0 Remember that the return value must be placed in the EAX register Make sure that any registers (not EAX) used by this function are placed back to their initial states prior to exiting Allow an end user to test this function in the following manner: Prompt...
/** * Write a recursive function that accepts a Queue<Integer>. It * should change every int...
/** * Write a recursive function that accepts a Queue<Integer>. It * should change every int in this queue to be double its original * value. You may NOT use loops or any other data structures besides * the queue passed in as a parameter. You may use a helper function. * @param q */ public static void doubleElements(Queue<Integer> q) {}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT