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

Given the IntLinkedList class below, complete the public functions: You are to a

ID: 3677008 • Letter: G

Question

Given the IntLinkedList class below, complete the public functions:

You are to assume the existance of the node class from previous problem.

class LinkedList {

         private:

node* head;    //reference to first node in the list

         public:

// Effect: adds value to the front of the linked list

// Precondition : None

// Postcondition: list has one more value

             void IntLinkedList::addToFront( int value);

                    

  // Effect: computes the average of the values in the list

// Precondition: list is not empty

// Returns: average of all data in this IntLinkedList

             double IntLinkedList::average( );               

// Effect: determines the largest value in the list

//Precondition: list is not empty

// Returns: largest value in this IntLinkedList

             int IntLinkedList::largest() ;

Explanation / Answer

/*
* IntLinkedList.cpp
*
* Created on: Mar 12, 2016
*      Author: Satish-Ravi
*/

#include "IntLinkedList.h"

IntLinkedList::IntLinkedList() {
   // TODO Auto-generated constructor stub
   head = NULL;
}

// Effect: adds value to the front of the linked list

// Precondition : None

// Postcondition: list has one more value

void IntLinkedList::addToFront(int value) {
   node *newhead = new node;
   newhead->data = value;
   newhead->next = head;
   head = newhead;
}

// Effect: computes the average of the values in the list

// Precondition: list is not empty

// Returns: average of all data in this IntLinkedList

double IntLinkedList::average() {
   node *curr = head;
   int total = 0;
   int count = 0;
   while (curr != NULL) {
       total += curr->data;
       curr = curr->next;
       count++;
   }
   double avg = ((double) total) / count;
   return avg;
}

// Effect: determines the largest value in the list

//Precondition: list is not empty

// Returns: largest value in this IntLinkedList

int IntLinkedList::largest() {
   node *curr = head;
   int max = 0;
   while (curr != NULL) {
       if (max < curr->data)
           max = curr->data;
       curr = curr->next;

   }
   return max;
}

IntLinkedList::~IntLinkedList() {
   // TODO Auto-generated destructor stub
}

int main() {
   IntLinkedList list = IntLinkedList();
   list.addToFront(10);
   list.addToFront(2);
   list.addToFront(20);
   list.addToFront(8);

   cout << "Avg of list items: " << list.average() <<endl;
   cout << "Max of list items: " << list.largest() <<endl;
   return 1;
}

--output--

Avg of list items: 10
Max of list items: 20

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