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

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
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...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool&...
In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool& found); Write the function definition as a recursive search, assuming an array-based implementation. Add a main function as a driver to test the BinarySearch. Test the edge cases.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT