Link to home
Start Free TrialLog in
Avatar of krazeegook
krazeegook

asked on

output a list<> within a struct

I have a list within a struct like so:
typedef struct{
  int parent;     //node number
  int tDist;  

  typedef struct{
    int destNode;    //destination node
    int cDist;    
  }listNode;

  listNode lN;

  list< listNode > aList;

} heapNode;

I have initiated them like so:

  /***create adjacency list***/
  for(int j=0;j<N[0];j++){
    heapNode hN;
    hN.parent = 0;     //parent = null
    hN.tDist = 999999; //infinity value
    adjList.push_back(hN);
  }
  //sets up the list within each node
  for(int j=2;j<N.size();j=j+3){
    //N[j],N[j+1],N[j+2] are just bunch of ints
    adjList[N[j]].lN.destNode = N[j+1];
    adjList[N[j]].lN.cDist = N[j+2];
    adjList[N[j]].l.push_back(adjList[j].lN);
  }

and I'm trying to print out integers in struct elements of "aList". I am failing miserably and gets some wierd values as output.  How should I go about this?
(I would also need the int values within the listNode structs for assignment or comparison later)

THanks
ASKER CERTIFIED SOLUTION
Avatar of Mafalda
Mafalda

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mafalda
Mafalda

sorry for the typing mistakes
i recommend using the STL's for_each<>().

create a functional object that prints a single node, and run for_each with it (easier to debug)
Avatar of krazeegook

ASKER

Thanks Mafalda,
I rearranged so that inner struct is declared before outter struct and it works fine =)