Question

In: Computer Science

You are given the partial implementation of class IntegerLinkedList which stores integers in the inked list....

You are given the partial implementation of class IntegerLinkedList which stores integers in the inked list. Add a public member function that does the following:

// Complete this for Problem 2

int getSmallestIndex(): return the smallest of integers stored in the linked list.

A main file (prob2.cpp) is provided:

#include <iostream>
#include <string>
#include "IntegerLinkedList.h"

using std::string;
using std::cout;
using std::endl;

int main() {
        {
                IntegerLinkedList mylist;
                mylist.addFront(10);
                mylist.addFront(17);
                mylist.addFront(23);
                mylist.addFront(17);
                mylist.addFront(92);

                if (mylist.getIndexSmallest() == 4)
                  cout << "PASSED" << endl;
                else
                  cout << "Result did not match expected answer: 4" << endl;
        }
        {
                IntegerLinkedList mylist;
                mylist.addFront(10);
                mylist.addFront(17);
                mylist.addFront(23);
                mylist.addFront(37);
                mylist.addFront(2);

                if (mylist.getIndexSmallest() == 0)
                  cout << "PASSED" << endl;
                else
                  cout << "Result did not match expected answer: 0" << endl;
        }
        // system("pause"); // comment/uncomment if needed
}
// ADD ANSWER TO THIS FILE

#pragma once

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

class IntegerLinkedList {
private:
  SNode *head;
  
  bool isLesser (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 getIndexSmallest(); // COMPLETE THIS FOR PROBLEM 2

  // recursion helper function called from main
  bool isLesserHelper (int compare) {
    return isLesser(head, compare);
  }
  
};

Add a recursive function called isLesser to class IntegerLinkedList to calculate the smallest of the linked list’s data values (same as in Problem 2 but recursive).

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

//PROBLEM 3

#include <iostream>
#include <string>
#include "IntegerLinkedList.h"

using std::string;
using std::cout;
using std::endl;

int main() {
        {
                IntegerLinkedList mylist;
                mylist.addFront(10);
                mylist.addFront(17);
                mylist.addFront(23);
                mylist.addFront(17);
                mylist.addFront(92);

                if (mylist.isLesserHelper(15))
                  cout << "PASSED" << endl;
                else
                  cout << "Result did not match expected answer: true" << endl;

                if (!mylist.isLesserHelper(5))
                  cout << "PASSED" << endl;
                else
                  cout << "Result did not match expected answer: false" << endl;

                if (mylist.isLesserHelper(100))
                  cout << "PASSED" << endl;
                else
                  cout << "Result did not match expected answer: true" << endl;
        }
        // system("pause"); // comment/uncomment if needed

}
// ADD ANSWER TO THIS FILE

#pragma once

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

class IntegerLinkedList {
private:
  SNode *head;
  
  bool isLesser (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 getIndexSmallest(); // COMPLETE THIS FOR PROBLEM 2

  // recursion helper function called from main
  bool isLesserHelper (int compare) {
    return isLesser(head, compare);
  }
  
};

Solutions

Expert Solution

DID BOTH THE FUNCTION, PLEASE CHECK, PLEASE UPVOTE

#pragma once

class SNode {

public:

int data;

SNode *next;

};

class IntegerLinkedList {

private:

SNode *head;

bool isLesser (SNode *ptr, int compare) {

if(ptr == NULL)

return false;

if(compare > ptr->data)

return true;

return isLesser(ptr->next, compare);

}

public:

IntegerLinkedList() {

head = nullptr;

}

void addFront(int x) {

SNode *tmp = head;

head = new SNode;

head->next = tmp;

head->data = x;

}

int getIndexSmallest(){

SNode *tmp = head;

int min = 100000, index = -1, count = 0;

while(tmp != NULL){

if(tmp->data < min){

index = count;

min = tmp->data;

}

++count;

tmp = tmp->next;

}

return index;

}

// recursion helper function called from main

bool isLesserHelper (int compare) {

return isLesser(head, compare);

}

};

============================

SEE OUTPUT

PLEASE COMMENT if there is any concern.

==========================================


Related Solutions

You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class that holds the information for each item for the inn. GildedRose is a class that holds an internal listing of many Item objects. This inventory should hold at least 10 items. For this you can use arrays, the std::array class, or even the vector class. Complete the implementation of these classes, adding public/private member variables and functions as needed. You should choose an appropriate...
You are provided with a partial implementation of a templated singly-linked list in LinkedList.h, with some...
You are provided with a partial implementation of a templated singly-linked list in LinkedList.h, with some missing functionality. It contains a templated Node class and a templated LinkedList class. Do not modify the class definitions. The linked list class contains the following methods: • LinkedList – Constructor of a linked list with a head pointing to null (implemented). • ~LinkedList – Destructor of a linked list. • deleteFromHead – Removes and returns content of the first node of the list....
Given the class below, which answer choice would be an implementation of the default constructor (without...
Given the class below, which answer choice would be an implementation of the default constructor (without an initialization list)? class MyInteger { private: int num; public: MyInteger(); MyInteger(int newint); void setInt(int newint); int getInt(); int operator[](int index); };    MyInteger::MyInteger(int newNum) { num = newNum; }     MyInteger::MyInteger() { num = 0; }     MyInteger::MyInteger(int newNum = 0) { num = newNum; }     MyInteger::MyInteger(int newNum) : num{0} {;}     MyInteger::MyInteger(int newNum = 0) : num{0} {;}     MyInteger::MyInteger() :...
/** * This class maintains an arbitrary length list of integers. * * In this version:...
/** * This class maintains an arbitrary length list of integers. * * In this version: * 1. The size of the list is *VARIABLE* after the object is created. * 2. The code assumes there is at least one element in the list. * * This class introduces the use of structural recursion. * * @author Raymond Lister * @version May 2016 * */ public class ListOfNVersion03PartB {    private int thisNumber; // the number stored in this node...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT