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 &copyin)   // 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;
}

Recommended Answers

All 5 Replies

>>Can anybody will help me to introduce the species member in the program? I would like to add species as member in public domain

I'd do it like this:

class TREE
{  
   friend ostream &operator<<(ostream &, const TREE &);   
   public:     
      int Tno;     
      int x;     
      int y;     
      int z;     
      float height;
      string familyName;
      string genusName;
      string speciesName;
      string varietyName;
      int age;
      int crownSize;

Hi, This is not the solution. i want to use species as also linked list.

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 &copyin)   // 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;
}

I think I know what you want to do -- create a 2d linked list

typedef list<TREE> LTREE;
list<LTREE> L;

Hi,

No. Actually i have stroed tree data as a doubly linked list. species is one member of that data and it has structure i.e. many paramenters. i want to use again linked list to store the parameters of species.

I think I know what you want to do -- create a 2d linked list

typedef list<TREE> LTREE;
list<LTREE> L;

You are not being very clear about what you want. Is species part of TREE ? What is species? is it a structure, a class, or something else.

class species
{
  // some stuff here
};

class Something
{
   Species sp;
// other stuff
};

Anything like the above? If not, then please show what you are doing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.