[CODE=cpp]////////////////////////////// LinkList.cpp
#include "LinkList.h "
/* The LinkList class implementation*/
/* Constructor */
LinkList::LinkL ist()
{
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(curren tNode->getNext());
currentNode->setNext( newNode );
lastCurrentNode = currentNode;
currentNode = newNode;
}
else
{
newNode->setNext(NULL );
headNode->setNext(newNod e);
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(LinkLi st list)
{
Node* savedCurrentNod e = list.currentNod e;
list.currentNod e = list.headNode;
for(int i = 1; list.next(); i++)
{
cout << "\n Element " << i << " of the list is " << list.get()<< endl;
}
list.currentNod e = savedCurrentNod e;
}
/* 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 List size is = " << list.size <<'\n';
return list;
return list.size;
//////////********** MEAN() ***********/////////
}
void LinkList ::mean()
{
if (headNode == Null)
cout<<"\n Undefined Value";
else
{
Node *temp = headNode;
int mean = temp->get();
int total;
//show content of node
while(temp !=Null)
{
if (mx<temp->get() )
total = total + temp->get;
//move into next node
temp = temp->getNext();
}
mean=total/list.size;
cout<<"\n Mean of List is "<<mean;
}
}
//////////********** MAX () ***********/////////
void LinkList ::max()
{
if(headNode == Null )
cout<< "\n Undefined Value";
else
{
//declaring the initialize temp
Node *temp = headNode;
int mx = temp->get();
//show content of node
while(temp !=Null)
{
if (mx<temp->get() )
mx = temp -> get () ;
//move into next node
temp = temp->getNext();
}
cout<<"/n The Maximum Element in List is"<< mx;
}
cout<<"/n"
}
}
///////////////////// LinkList.h
#include "Node.h"
/* The LinkList class declaration*/
class LinkList
{
public:
LinkList();
void add (int addObject);
int get();
bool next();
friend void traverse(LinkLi st list);
friend LinkList addNodes();
friend void mean(LinkList list);
friend void max(LinkList list);
private:
int size;
Node * headNode;
Node * currentNode;
Node * lastCurrentNode ;
};
/////////////////////// main.cpp
#include "LinkList.h "
int main()
{
LinkList list = addNodes();
traverse(list);
mean(LinkList list)
max(LinkList list)
system("pause") ;
return 0;
}
////////////////////// 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;
}
/////////////////////////////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;
};[/CODE]
#include "LinkList.h "
/* The LinkList class implementation*/
/* Constructor */
LinkList::LinkL ist()
{
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(curren tNode->getNext());
currentNode->setNext( newNode );
lastCurrentNode = currentNode;
currentNode = newNode;
}
else
{
newNode->setNext(NULL );
headNode->setNext(newNod e);
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(LinkLi st list)
{
Node* savedCurrentNod e = list.currentNod e;
list.currentNod e = list.headNode;
for(int i = 1; list.next(); i++)
{
cout << "\n Element " << i << " of the list is " << list.get()<< endl;
}
list.currentNod e = savedCurrentNod e;
}
/* 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 List size is = " << list.size <<'\n';
return list;
return list.size;
//////////********** MEAN() ***********/////////
}
void LinkList ::mean()
{
if (headNode == Null)
cout<<"\n Undefined Value";
else
{
Node *temp = headNode;
int mean = temp->get();
int total;
//show content of node
while(temp !=Null)
{
if (mx<temp->get() )
total = total + temp->get;
//move into next node
temp = temp->getNext();
}
mean=total/list.size;
cout<<"\n Mean of List is "<<mean;
}
}
//////////********** MAX () ***********/////////
void LinkList ::max()
{
if(headNode == Null )
cout<< "\n Undefined Value";
else
{
//declaring the initialize temp
Node *temp = headNode;
int mx = temp->get();
//show content of node
while(temp !=Null)
{
if (mx<temp->get() )
mx = temp -> get () ;
//move into next node
temp = temp->getNext();
}
cout<<"/n The Maximum Element in List is"<< mx;
}
cout<<"/n"
}
}
///////////////////// LinkList.h
#include "Node.h"
/* The LinkList class declaration*/
class LinkList
{
public:
LinkList();
void add (int addObject);
int get();
bool next();
friend void traverse(LinkLi st list);
friend LinkList addNodes();
friend void mean(LinkList list);
friend void max(LinkList list);
private:
int size;
Node * headNode;
Node * currentNode;
Node * lastCurrentNode ;
};
/////////////////////// main.cpp
#include "LinkList.h "
int main()
{
LinkList list = addNodes();
traverse(list);
mean(LinkList list)
max(LinkList list)
system("pause") ;
return 0;
}
////////////////////// 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;
}
/////////////////////////////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;
};[/CODE]
Comment