Question

Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive...

Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive integers in the linked list are equal (return true in this case, false otherwise). You may assume that the list is not empty.

A recursion “helper” function is already included in class IntegerLinkedList. You only need to write the recursive function.

For example, checkRecurseHelper() should return true for 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.

Note:

A non-recursive version of the function will get no credit. The function should not have any loops at all. Do not use any global variables. Do not add member variables to the class. Do not change the contents of the linked list.

Write the recursive function in IntegerLinkedList.cpp.

You can change the main function for your own testing. Your code will be tested with a similar main function.

*IntegerLinkedList.cpp

// ADD ANSWER TO THIS FILE

#include "IntegerLinkedList.h"

bool IntegerLinkedList::checkList() {
   // COMPLETE THIS FOR PROBLEM 2
}

bool IntegerLinkedList::checkRecurse (SNode *ptr) {
// COMPLETE THIS FOR PROBLEM 3


}

void IntegerLinkedList::addFront(int x) {
SNode *tmp = head;
   head = new SNode;
   head->next = tmp;
   head->elem = x;
}

// recursion helper function called from main for PROBLEM 3
bool IntegerLinkedList::checkRecurseHelper () {
return checkRecurse(head);
}

* IntegerLinkedList.h

#pragma once

class SNode {
public:
int elem;
SNode *next;
};

class IntegerLinkedList {
private:
SNode *head;
bool checkRecurse (SNode *ptr); // for Problem 3; Implement in IntegerLinkedList.cpp

public:
IntegerLinkedList(): head(nullptr) {}
void addFront(int x);

bool checkList(); // for Problem 2; Implement in IntegerLinkedList.cpp

// recursion helper function called from main for PROBLEM 3
bool checkRecurseHelper ();
};

* prob3.cpp

//
// EDIT THIS FILE ONLY FOR YOUR OWN TESTING
// WRITE YOUR CODE IN IntegerLinkedList.cpp
//

#include
#include
#include "IntegerLinkedList.h"

using std::string;
using std::cout;
using std::endl;
bool checkAnswer(const string &nameOfTest, bool received, bool expected);

int main() {
   cout << "Check if two consecutive elements are equal (RECURSIVE)" << endl;
   {
       IntegerLinkedList mylist;
       mylist.addFront(57);
       mylist.addFront(23);
       mylist.addFront(23);
       mylist.addFront(20);
       mylist.addFront(13);
       cout << "List: 13 -> 20 -> 23 -> 23 -> 57" << endl;
       checkAnswer("", mylist.checkRecurseHelper(), true);
}
{
       IntegerLinkedList mylist;
       mylist.addFront(5);
       mylist.addFront(10);
       mylist.addFront(23);
       mylist.addFront(17);
       mylist.addFront(23);
       cout << "List: 23 -> 17 -> 23 -> 10 -> 5" << endl;
       checkAnswer("", mylist.checkRecurseHelper(), false);
}
{
       IntegerLinkedList mylist;
       mylist.addFront(17);
       mylist.addFront(17);
       mylist.addFront(25);
       mylist.addFront(10);
       mylist.addFront(5);
       cout << "List: 5 -> 10 -> 25 -> 17 -> 17" << endl;
       checkAnswer("", mylist.checkRecurseHelper(), true);
}
   {
       IntegerLinkedList mylist;
       mylist.addFront(17);
       cout << "List: 17" << endl;
       checkAnswer("", mylist.checkRecurseHelper(), false);
}
   // system("pause"); // comment/uncomment if needed
}

bool checkAnswer(const string &nameOfTest, bool received, bool expected) {
       if (received == expected) {
               cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
               return true;
       }
       cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
       return false;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For this, we can forumulate our recursive function IntegerLinkedList::checkRecurse (SNode *ptr) as follows.

Base case 1: if ptr is NULL, result is obviously false

Base case 2 : if ptr is not NULL and ptr->next is also NULL, we return true if ptr->elem is equal to ptr->next->elem

Recursive case: Else, we can call the same function recursively with ptr->next as argumement

This formulation nicely translates to code. Following is the code of IntegerLinkedList.cpp

---- code begin ----

 #include "IntegerLinkedList.h" bool IntegerLinkedList::checkList() { // COMPLETE THIS FOR PROBLEM 2 } bool IntegerLinkedList::checkRecurse (SNode *ptr) { // COMPLETE THIS FOR PROBLEM 3 if (!ptr) return false; if (ptr->next && ptr->elem == ptr->next->elem) return true; return checkRecurse(ptr->next); } void IntegerLinkedList::addFront(int x) { SNode *tmp = head; head = new SNode; head->next = tmp; head->elem = x; } // recursion helper function called from main for PROBLEM 3 bool IntegerLinkedList::checkRecurseHelper () { return checkRecurse(head); }
 ---- code end ------ 

Also, iostream has to be added to include of prob3.cpp.

Output:

Add a comment
Know the answer?
Add Answer to:
Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are to write two functions, printString() and testString(), which are called from the main function....

    You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints characters to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains two consecutive characters that are the same, false otherwise. See the main() to see how the two functions are called. Some sample runs are given below: string: “hello” printString prints: h e l l o testString returns: true...

  • In C++: You are to write two functions, printString() and testString(), which are called from the...

    In C++: You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints every character in the string to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains characters that are in sorted order, false otherwise. You may assume that all characters are lowercase and only alphabetical characters are present. See the main() to see how the two functions are...

  • Your task is to complete the following function/functions: 1. Given a position in the linked 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...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • C++, Change the destroy_list function in the header file to a recursive destroy_list function, main is...

    C++, Change the destroy_list function in the header file to a recursive destroy_list function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();    void...

  • (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class...

    (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance...

  • You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided)....

    You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance from...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • In C++, for the provided template linked list class create a derived class of it which...

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

  • You are making a .h file. Implement a recursive binary search function. bSearch passes in an...

    You are making a .h file. Implement a recursive binary search function. bSearch passes in an array of integers, the size of the array, and the value the function is searching for. The function should return the index where found or -1 if not found. You will want to implement a recursive helper function that passes in the array, the value to be located, and the beginning and ending of the range of values to search within. Use the provided...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT