In: Computer Science
This is C++
there are intruction and descriptions. Please give me the answer
because I understand the concept, but don't know how to put in the
actual problem yet.
Instructions and Assumptions
Declare and implement the three functions described
below. Your declarations should go in AnyList.h. Your definitions
should go in Functions.cpp.
For all of these functions, assume the list contains at least three
elements. No need to consider the empty list cases.
The Functions
1. Overload the insertion operator as a friend
function of AnyList. It should print only the first and last
element.
For example, if the list were [1, 3, 5, 7], it should print "1
7".
2. Overload the > operator as a non-member function. It should
compare two AnyList objects. Given two lists, ListA and ListB,
ListA > ListB if the largest element of ListA is greater than
the largest element of ListB.
For example, if ListA = [1, 2, 5] and ListB = [3, 4, 3], then ListA
> ListB because 5 > 4.
3. Write a function called findAndModify that is a member function
of AnyList. It should take one parameter - an integer called key.
It should return a boolean value, which will be true if the key was
found and false otherwise. This function should not print any error
messages.
The function should search the list for key. If it finds key, it
should add one node to the beginning of the list whose data should
be key + 10.
Examples:
Assume the list is [3, 5, 20, 8, 5, 20]
findAndModify(3) --> return true; resulting list is [13, 3, 5,
20, 8, 5, 20]
findAndModify(5) --> return true; resulting list is [15, 15, 3,
5, 20, 8, 5, 20]
findAndModify(100) --> return false; resulting list is [3, 5,
20, 8, 5, 20]
AnyList.h/////////
/////////////////////////////////////////////////////
// Put your name on this file.
// Put the declarations for the functions where indicated.
// Do NOT add any other functions to DoublyList.
// Do NOT change any other functions in this file.
// Turn in this file
////////////////////////////////////////////////////
#ifndef ANYLIST_H
#define ANYLIST_H
#include<iostream>
#include <string> //Need
to include for NULL
class Node
{
public:
Node() : data(0), next(nullptr) {}
Node(int theData, Node* newNext)
: data(theData),
next(newNext) {}
Node* getNext() const { return next; }
int getData() const { return data; }
void setData(int theData) { data = theData;
}
void setNext(Node* newNext) { next = newNext;
}
~Node() {}
private:
int data;
Node* next;
};
///////////////////////////////
// Declare Functions Here, if needed
//////////////////////////////
class AnyList{
public:
///////////////////////////////
// Declare Functions Here, if
needed
//////////////////////////////
/////////////////////////////////
// Do not modify anything below //
////////////////////////////////
AnyList();
AnyList(int* elems, int numElem);
void destroyList();
~AnyList();
private:
Node* first;
int count;
};
#endif
AnyList.cpp///////
/////////////////////////////////////////////
// DO NOT MODIFY THIS FILE //
/////////////////////////////////////////////
#include "AnyList.h"
//constructor
AnyList::AnyList(){
first = nullptr;
count = 0;
}
AnyList::AnyList(int* elems, int numElem) {
first = new Node(elems[0], nullptr);
Node* last = first;
for (int i = 0; i < numElem; i++) {
last->setNext(new
Node(elems[i], nullptr));
last =
last->getNext();
}
count = numElem;
}
void AnyList::destroyList(){
Node* temp = first;
while (first != nullptr){
first =
first->getNext();
delete temp;
temp = first;
}
count = 0;
}
//destructor
AnyList::~AnyList()
{
destroyList();
}
Functions.cpp/////////
#include "AnyList.h"
///////////////////////////////////////
// Put your name in this file
// Implement your functions in this file
// Submit this file
/////////////////////////////////////
////////////////////////////////////////
// Definition of Function 1 goes here
//////////////////////////////////////
////////////////////////////////////////
// Definition of Function 2 goes here
//////////////////////////////////////
////////////////////////////////////////
// Definition of Function 3 goes here
//////////////////////////////////////
Main,cpp///////
///////////////////////////////////////
// Use this file to test your code
// Don't submit this file
/////////////////////////////////////
#include "AnyList.h"
#include <iostream>
using namespace std;
int main() {
int elems[] = { 1, 2, 3, 4, 5, 6,
7, 8 };
AnyList testList(elems, 8);
return 0;
}
// AnyList.h
/////////////////////////////////////////////////////
// Put your name on this file.
// Put the declarations for the functions where indicated.
// Do NOT add any other functions to DoublyList.
// Do NOT change any other functions in this file.
// Turn in this file
////////////////////////////////////////////////////
#ifndef ANYLIST_H
#define ANYLIST_H
#include<iostream>
#include <string> //Need to include for NULL
class Node
{
public:
Node() : data(0), next(nullptr) {}
Node(int theData, Node* newNext)
: data(theData), next(newNext) {}
Node* getNext() const { return next; }
int getData() const { return data; }
void setData(int theData) { data = theData; }
void setNext(Node* newNext) { next = newNext; }
~Node() {}
private:
int data;
Node* next;
};
class AnyList{
public:
// Non-member functions
friend std::ostream& operator<<(std::ostream& out,
const AnyList& list);
friend bool operator>(const AnyList& list1, const
AnyList& list2);
// member function
bool findAndModify(int key);
/////////////////////////////////
// Do not modify anything below //
////////////////////////////////
AnyList();
AnyList(int* elems, int numElem);
void destroyList();
~AnyList();
private:
Node* first;
int count;
};
#endif
// end of AnyList.h
// AnyList.cpp
/////////////////////////////////////////////
// DO NOT MODIFY THIS FILE //
/////////////////////////////////////////////
#include "AnyList.h"
//constructor
AnyList::AnyList(){
first = nullptr;
count = 0;
}
AnyList::AnyList(int* elems, int numElem) {
first = new Node(elems[0], nullptr);
Node* last = first;
for (int i = 0; i < numElem; i++) {
last->setNext(new Node(elems[i], nullptr));
last = last->getNext();
}
count = numElem;
}
void AnyList::destroyList(){
Node* temp = first;
while (first != nullptr){
first = first->getNext();
delete temp;
temp = first;
}
count = 0;
}
//destructor
AnyList::~AnyList()
{
destroyList();
}
//end of AnyList.cpp
// Functions.cpp
#include "AnyList.h"
///////////////////////////////////////
// Put your name in this file
// Implement your functions in this file
// Submit this file
/////////////////////////////////////
// Overload the insertion operator to print only the first and
last element.
std::ostream& operator<<(std::ostream& out, const
AnyList& list)
{
Node *curr = list.first; // get the first node of list in curr
out<<curr->getData()<<" "; // display the first element
// loop to get the last node of the list
while(curr->getNext() != NULL)
curr = curr->getNext();
// display the last element of the list
out<<curr->getData();
return out;
}
// Overload the > operator as a non-member function. It
should compare two AnyList objects.
// Given two lists, ListA and ListB, ListA > ListB if the
largest element of ListA is greater than the largest element of
ListB.
bool operator>(const AnyList& list1, const AnyList&
list2)
{
int max1, max2;
// find the largest element of list1
Node* curr = list1.first;
max1 = curr->getData();
while(curr != NULL)
{
if(curr->getData() > max1)
max1 = curr->getData();
curr = curr->getNext();
}
// find the largest element of list2
curr = list2.first;
max2 = curr->getData();
while(curr != NULL)
{
if(curr->getData() > max2)
max2 = curr->getData();
curr = curr->getNext();
}
// compare largest elements of list1 and list2
return max1 > max2;
}
// It should take one parameter - an integer called key.
// It should return a boolean value, which will be true if the key
was found and false otherwise.
// The function should search the list for key.
// If it finds key, it should add one node to the beginning of the
list whose data should be key + 10.
bool AnyList:: findAndModify(int key)
{
bool found = false; // initialize found to false
Node* curr = first; // set curr to first node of list
// loop to find key in the list
while(curr != NULL)
{
if(curr->getData() == key) // key found, set found to true and
exit the loop
{
found = true;
break;
}
curr = curr->getNext();
}
if(found) // key was found in the list
{
// create a new node with value key+10 and next = first
Node *node = new Node(key+10, first);
first = node; // update first to node to insert node to the
beginning of the list
count++; // increment count
}
return found;
}
//end of Functions.cpp
//////////////////////////////////////
////Main,cpp///////
///////////////////////////////////////
// Use this file to test your code
// Don't submit this file
/////////////////////////////////////
#include "AnyList.h"
#include <iostream>
using namespace std;
int main() {
int elems[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
AnyList testList(elems, 8);
int elems1[] = {3, 5, 20, 8, 5, 20};
AnyList testList1(elems1, 6);
cout<<boolalpha;
cout<<"Test List: "<<testList<<endl;
cout<<"Test List1: "<<testList1<<endl;
cout<<"TestList > TestList1: "<<(testList >
testList1)<<endl;
cout<<"TestList1 > TestList: "<<(testList1 >
testList)<<endl;
cout<<"TestList.findAndModify(3):
"<<testList.findAndModify(3)<<endl;
cout<<"TestList: "<<testList<<endl;
cout<<"TestList.findAndModify(10):
"<<testList.findAndModify(10)<<endl;
cout<<"TestList: "<<testList<<endl;
return 0;
}
//end of Main.cpp
Output: