In: Computer Science
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);
}
};
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.
==========================================