Hi,
I want to use linked list in the linked list. In the following code i would like to introduce new public member as species which has four to five members. How to modify the code to add new member species? In the class TREE, I would like to add species as member in public domain, which will give the species number. For each species, there will be four parameters as species height, species age, crown size, etc, for which again i would like to use list.
Can anybody will help me to introduce the species member in the program?
Mahesh
// Standard Template Library example using a class.
#include <iostream>
#include <list>
using namespace std;
// The List STL template requires overloading operators =, == and <.
class TREE
{
friend ostream &operator<<(ostream &, const TREE &);
public:
int Tno;
int x;
int y;
int z;
float height;
TREE();
TREE(const TREE &);
~TREE(){};
TREE &operator=(const TREE &rhs);
int operator==(const TREE &rhs) const;
int operator<(const TREE &rhs) const;
};
TREE::TREE() // Constructor
{
Tno = 0;
x = 0;
y = 0;
z = 0;
height = 0;
}
TREE::TREE(const TREE ©in) // Copy constructor to handle pass by value.
{
Tno = copyin.Tno;
x = copyin.x;
y = copyin.y;
z = copyin.z;
height = copyin.height;
}
ostream &operator<<(ostream &output, const TREE &tree)
{
output << tree.Tno << ' ' << tree.x << ' ' << tree.y <<' '<< tree.z << ' '<< tree.height <<endl;
return output;
}
TREE& TREE::operator=(const TREE &rhs)
{
this->Tno = rhs.Tno;
this->x = rhs.x;
this->y = rhs.y;
this->z = rhs.z;
this->height = rhs.height;
return *this;
}
int TREE::operator==(const TREE &rhs) const
{
if( this->Tno != rhs.Tno) return 0;
if( this->x != rhs.x) return 0;
if( this->y != rhs.y) return 0;
if( this->z != rhs.z) return 0;
if( this->height != rhs.height) return 0;
return 1;
}
// This function is required for built-in STL list functions like sort
int TREE::operator<(const TREE &rhs) const
{
if( this->Tno == rhs.Tno && this->x == rhs.x && this->y==rhs.y && this->z==rhs.z && this->height < rhs.height) return 1;
if( this->Tno == rhs.Tno && this->x == rhs.x && this->y==rhs.y && this->z < rhs.z) return 1;
if( this->Tno == rhs.Tno && this->x == rhs.x && this->y < rhs.y) return 1;
if( this->Tno == rhs.Tno && this->x < rhs.x) return 1;
if( this->Tno < rhs.Tno) return 1;
return 0;
}
main()
{
list<TREE> L;
TREE T1 ;
T1.Tno=1;
T1.x=2;
T1.y=6;
T1.z=0;
T1.height=67;
L.push_back(T1); // Insert a new element at the end
T1.Tno=10;
T1.x=15;
L.push_back(T1); // Object passed by value. Uses default member-wise
T1.Tno=5; // copy constructor
T1.z=13;
L.push_back(T1);
T1.Tno=7;
T1.x=30;
T1.y=70;
T1.z=70;
L.push_back(T1);
list<TREE>::iterator i;
for(i=L.begin(); i != L.end(); ++i) cout << (*i).x << " "; // print member
cout << endl;
for(i=L.begin(); i != L.end(); ++i) cout << *i << " "; // print all
cout << endl;
cout << "Sorted: " << endl;
L.sort();
for(i=L.begin(); i != L.end(); ++i) cout << *i << " "; // print all
cout << endl;
return 0;
}