Question

In: Computer Science

This is C++ there are intruction and descriptions. Please give me the answer because I understand...

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

Solutions

Expert Solution

// 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:


Related Solutions

can someone please answer for me that quaestions. please make sure that i understand your work...
can someone please answer for me that quaestions. please make sure that i understand your work and handwriting. thank you _____________________________________________________ 1. We will sketch some quadrics, but in order to make sure our graphs have some accuracy, we will project the surfaces onto the 3 coordinate planes. For each equation, draw four separate graphs for the surface S: i. the projection of S onto the xy-plane, ii. the projection of S onto the xz-plane, iii. the projection of S...
Please explain your answer thoroughly because I want to understand it well and also please include...
Please explain your answer thoroughly because I want to understand it well and also please include a diagram if possible Two objects which have mass: m1 = 10kg and m2 = 20kg. Both of them are moving at the velocity of: v1 = 20^i ms and v2 = 10^j ms and then the two objects collide completely inelastically. In what direction do the two objects go after the collision? After the collision, how much kinetic energy was lost?
i am student of BBA and i am making an internship please give me the answer...
i am student of BBA and i am making an internship please give me the answer Company: ABU DHABI POLICE UAE Job Position: Police Officer 1. Duties and Responsibilities (200 words) 2. My Experience (100 words) 3. Internship Lesson (5 lesson) (100 words) 4. Changes Would You Make in Internship (100 words)
I want letter about appreciation,to appreciate agency give me scholarship . please I want typing because...
I want letter about appreciation,to appreciate agency give me scholarship . please I want typing because I don't understand handwriting thanks
I do not understand this question could someone please answer and give meaning to this question....
I do not understand this question could someone please answer and give meaning to this question. MHC6305 Finanical Management. Compare and contrast cash accounting methodology and accrual accounting methodology in order to illustrate how each best works for different types of companies.
Please I need a clear explanation and clear handwriting with explanaing this for me because I...
Please I need a clear explanation and clear handwriting with explanaing this for me because I am realy confused and need help. Thanks in advance My lab was about  Magnets and Magnetic fields. This is the question I need to answer for! R3: Explain how a speaker works by explaining why each part is required. after that Summarize R3’s answer by explaining why the proffesor homemade earbud is so much superior to the one that you made.
Please give me some idea about this blog question: It is important to understand and analyze...
Please give me some idea about this blog question: It is important to understand and analyze a country’s political system before entering that market. Is it more important for a political system to be stable, or is it more important for it to be transparent?
Please use EXCEL to answer and show all formulas. I can't understand the answer if I...
Please use EXCEL to answer and show all formulas. I can't understand the answer if I can't see the formulas for each field. Thank you very much. Newman manufacturing is considering a cash purchase of the stock of Grips Tool. During the year just​ completed, Grips earned $4.25 per share and paid cash dividends of ​$2.55 per share ​(D0=$2.55). Grips' earnings and dividends are expected to grow at 25​% per year for the next 3​ years, after which they are...
Hello, please can you give me the answers of these questions. Thanks 12.1 Because of the...
Hello, please can you give me the answers of these questions. Thanks 12.1 Because of the possibility that a forensic accounting engagement may result in litigation, ·         (a) forensic accountants must be law enforcement officers, lawyers, or officers of the court. ·         (b) forensic accountants must be certified by the ACFE. ·         (c) forensic accountants do not rely on interviews as a source of evidence. ·         (d) forensic accountants are likely to have an adversarial relationship with the employees of...
can you please explain to me in simplest way that i can understand the cyert and...
can you please explain to me in simplest way that i can understand the cyert and march behaviour theory. kindly give an example for it. thank you so much.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT