I am giving u complete code. It works properly
But add max and mean functions so that it can work properly.
*
/LinkList.h/
*
#include "Node.h"
*
/* The LinkList class declaration*/
class** LinkList
{
public:
*** LinkList();
*** void** add (int** addObject);
*** int**** get();
*** bool** next();
***
*** friend void* traverse(LinkList list);
*** friend LinkList addNodes();
*** friend bool isSorted(LinkList );
***
private:
*** int* size;
*** Node *** headNode;
*** Node *** currentNode;
*** Node *** lastCurrentNode;
*
};
*
/LinkList.cpp/
*
*
#include "LinkList.h"
*
*
/* The LinkList class implementation*/
*
/* Constructor */
LinkList::LinkList()
{
headNode** =** new Node();
*********** headNode->setNext(NULL);
*********** currentNode** =** NULL;
*********** lastCurrentNode* =** NULL;
*********** size** =** 0;
}
***********
/* add() class method */
void** LinkList::add (int** addObject)
{
*** Node *** newNode** =** new** Node();
*** newNode->set(addObject);
*** if( currentNode** !=** NULL )
*** {
******* newNode->setNext(currentNode->getNext());
******* currentNode->setNext( newNode );
******* lastCurrentNode** =** currentNode;
******* currentNode** =** newNode;
**** }
**** else
**** {
*********** newNode->setNext(NULL);
*********** headNode->setNext(newNode);
*********** lastCurrentNode** =** headNode;
*********** currentNode** =** newNode;
***** }
***** size ++;
}
*
/* get() class method */
int** LinkList::get()
{
*********** if (currentNode* !=* NULL)
*********** *return** currentNode->get();
}
*
/* next() class method */
bool** LinkList::next()
{
*** if (currentNode* ==* NULL)** return* false;
*
*** lastCurrentNode* =* currentNode;
*** currentNode* =* currentNode->getNext();
*** if (currentNode == NULL || size == 0)
return* false;
*** else
*********** return* true;
}
*
/* Friend function to traverse linked list */
void traverse(LinkList list)
{
*** Node* savedCurrentNode* =* list.currentNode;
*** list.currentNode* =* list.headNode;
***
*** for(int i = 1; list.next(); i++)
*** {
******* cout << "\n Element " << i << " of the list is* " << list.get()<< endl;
*** }
***
*** list.currentNode* =* savedCurrentNode;
}
*
/* Friend function to add Nodes into the list */
LinkList addNodes(){
***
*********** LinkList list;
*********** int count = 0;
*********** cout* << "\n Enter the length of the LinkList you want to create: ";
*********** cin>>count;
*********** int temp;
*********** for(int i = 0; i < count ; i++)
*********** {
*************** cout* <<"\n Enter the Element No # "<<i+1<<": ";********
***************
*************** cin>>temp;
***************
*************** list.add(temp);
***************
*********** }***
*********** cout* <<* "\n---------------------------------------------";
*********** cout* <<* "\n**** List size is = "* <<* list.size* <<'\n';********
*********** cout* <<* "\n---------------------------------------------";***********
*********** return list;*************
*********** ** }
*****bool isSorted(LinkList list){
****
*** Node* savedCurrentNode* =* list.currentNode;
*
*** list.currentNode* =* list.headNode;
*** list.next();* // move to first element in the list
***
*** bool sorted = true;
***
*** for(int i = 0; i < list.size-1 ; i++)
******* {
********** int current = list.get();* // get current node value in the list
********** list.next();* // moved the list to next node
********** int next = list.get(); // get next node value in the list
********** cout<<"\nComparing current value: "<<current<<" and next value: "<<next<<endl;
********** if (current > next)
********** sorted = false;**********
********** cout<<"bool sorted value is: "<<sorted<<endl;*********
******* }*******
***** list.currentNode* =* savedCurrentNode;***
*** return sorted;
****
****
*** }******
*
/Node.h/
*
#include** <iostream>
#include** <stdlib.h>
using namespace std;*
/* The Node class declaration */*
class** Node
{
*** public:
*********** *** int* get();
*********** *** void set(int );
*********** *** Node * getNext();
*********** *** void setNext(Node *);
*
**** private:
****** int* object;
****** Node * nextNode;
};********
***********
/Node.cpp/
*
#include "Node.h"*
/* The Node class implementation*/*
int Node:: get() { ***
*** return** object; ** *
*** }
void Node:: set(int** object) {
***
*** this->object** =** object;
*** }*
Node * Node::getNext() {
***
*** return** nextNode;
***
*** }
void Node:: setNext(Node** * nextNode) {
***
*** this->nextNode** =** nextNode;
***
*** }
*
*********** /main.cpp/
*
#include** "LinkList.h"*
int main()
{
*** LinkList list* =* addNodes();
*** traverse(list);
*
*** if(isSorted(list))
*** cout<<"\n\n----------- List is sorted----------\n\n";
*** else
*** cout<<"\n\n----------- List is not sorted-------\n\n";*
*** system("pause");
*** return 0;
}
*
Suppose we have to add two basic statistics functions using LinkList, these functions
description is given below,
Our first function is,
Mean:
Description:
This function will calculate mean of the all the elements present in the list and will show
the result. Its formula is,
Mean = Total of all elements of the list / Length of the list.
If list is empty (has length equal to zero) it will show message mean is undefined
Max:
Description:
This function will find the maximum element present in the list and will show it.
If list is empty (has length equal to zero) it will show message undefined value.
*
After that you will add two friend functions in second LinkList as we have added
isSorted function in *project,
friend void mean(LinkList list)
/*it will show the mean of link list elements, but if the list has zero length then it
will display the result “that mean is undefined for this list” */
friend void max(LinkList list)
/*it will show the max element from the list, if list has zero length then it will
display the result “that max element is undefined for this list” */
*
Redefine the above course so it can work properly. Add Mean and Max functions