954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem about 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;
}
maheshkumar
Newbie Poster
3 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

>>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;
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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;
}
maheshkumar
Newbie Poster
3 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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

typedef list<TREE> LTREE;
list<LTREE> L;
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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;
maheshkumar
Newbie Poster
3 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You