Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Read all the parts of this question 2. so you know what the parvou need to wr

ID: 3909617 • Letter: 2

Question

2. Read all the parts of this question 2. so you know what the parvou need to wrt and make sure they all fit together to make a complete protYou only need to code for this program except for the functions build and output the functions build and output, but not write the code for thenm program. You need to write all the You only need to declare so you know what the parts of the program are #include using namespace std class LinkedList: I/ prototype, needed to deal with the order of class declarations 2a. Define a Node class, for use in a linked list. It contains a member data field of type Node named next. The Node contains the data, rather than pointing to the data shall be an array of two doubles. The Node class shall have a constructor that takes a wo double parameters, and stores the values of the parameters in the Node data. T LinkedList class shall be a friend of the Node class. Make all the data fields, all the constructors, and all the member functions private so that no one can use them except for the LinkedList class.

Explanation / Answer

Assumption :

Code :

#include <iostream>

using namespace std;


class LinkedList;


class Node
{
private:
    Node * next;
    double data[2];
public:
    Node(double a, double b)        // takes 2 double values as input and push them in its data array
    {
        data[0] = a;
        data[1] = b;
        next = NULL;
    }
    friend class LinkedList;        // this means that the LinkedList class can access private data of this class
};

class LinkedList
{
public:
    Node *head = NULL;              // initializing head


                                    // this function takes 2 values and creates a node for them
                                    // then places that node at the start of the linked list
                                    // then asks for user if there are more input
                                    // if the user enters 1 then user wants to input more values
                                    // if the user enters -1 then user wants to close this list
    void build()
    {
        cout << " Enter two values to be put in the linked list:";
        double a,b;
        cin >> a;
        cin >> b;
        Node* node = new Node(a,b); // creating new node
        node->next = this->head;    // placing its next to the current head node
        this->head = node;          // making it the new head

        int check = 0;
        cout << " If you have two more values to input Enter 1.";
        cout << "Else enter -1 (If these are all the values you wanted to enter). INPUT : ";
        cin >> check;
        if(check == 1)
            build();
    }

    void output()                   // this function outputs the current list
    {
        Node* travel = this->head;
        while(travel!=NULL)
        {
            cout << "( " << travel->data[0] << " , " << travel->data[1] << " ) --> ";
            travel = travel->next;
        }
        cout << "END" << endl;
    }

    double computeAverage()         // this function computes the average of all the values in the list
                                    // and returns the avg value
    {
        Node*temp = this->head;
        int i = 0;
        double avg = 0.0;
        while(temp!=NULL)
        {
            avg = (avg*i + (temp->data[0] + temp->data[1]) )/(i+1) ;
            temp = temp->next;
            i ++;
        }
        return avg;
    }

    bool exists(double target)      // this functions returns true if a specified value if found in the list
                                    // else it returns false
    {
        Node * check = this->head;
        while(check!=NULL)
        {
            if((check->data[0]==target)||(check->data[1]==target))
                return true;
            check = check->next;
        }
        return false;
    }
};

int main()
{
    LinkedList list1;
    cout << "List1 initialized ";
    list1.build();
    list1.output();
    double avg1 = list1.computeAverage();
    cout << "The average of all the values input in the list is : " << avg1 << endl;
    LinkedList list2;
    cout << "List2 initialized ";
    list2.build();
    list2.output();
    if(list2.exists(3.0))
        cout << "The value does exist in the linked list ";
    else cout << "The value does not exist in the linked list ";
    return 0;
}

*******************NOTE*******************

I have made comments where I thought needed, If there is still any doubt or any explanation or you want to change the code. Please reply in the comments.
If everything is fine, Please rate accordingly... :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote