Hi! Can anyone please tell me where my mistake in this is?
The information from the list has to be transferred to the tree. But only the first element is copied.

#include<iostream>
#include<string>

using namespace std;

struct Person{
       char first[30], last[30];
       };
       
struct aList { 
       char adr[30]; Person name[30]; int numPpl; aList *next;
            };
            
struct aTree { 
       char adr[30]; Person name[30]; int numPpl; aTree *adrL, *adrR;
            };
            
typedef aList * point;
typedef aTree * node;
node root;
point head;

void create(point &head)
{     
     point	Last, p;
     char	ch, cz;
     int i;
     
     cout <<" New address <y/n>: ";
     cin >> ch;
     while (ch == 'y') 
     {
      int j=1;
      p = new aList;
      cout << "Address: ";
      cin >> p->adr;
      cout << "Number of inhabitants: ";
      cin >> p->numPpl;     
      for (int i=0; i<p->numPpl; i++, j++)
      {
          cout << j << " Name: ";
          cin >> p->name[i].first;
          cout << j << " Last name: ";
          cin >> p->name[i].last;
      }
           
      p->next=NULL;
      if (head == NULL) head = p;
         else	Last->next = p;
      Last = p;
      
      cout << " New address <y/n>: ";
      cin >> ch;
     }
              
}

void print_List(point p)
{
     while (p)
     {
           int j=1;
           cout << endl << "Inhabitants of '" << p->adr << "' :" << endl << endl;

           for (int i=0; i<p->numPpl; i++, j++)
           {

               cout << "  "  << j << ". " << p->name[i].first << " " << p->name[i].last << endl;

           }
               
           p=p->next;
     }
}

void  Ins_List(point head)
{
      char adrs[30];
      point  p, pPrev, q;
      cout << " Enter address: ";
      cin >> adrs;
      p = head->next;
      pPrev = head;
      while (p && strcmp(p->adr, adrs) < 0) 
      {	
            pPrev = p;
            p = p->next;
      }
      if (p && strcmp(p->adr, adrs) == 0)
      {
         for (int i = 0, j = 1; i < q->numPpl; i++, j++)
         {
             cout << j << " Name: ";
             cin >> p->name[i].first;
             cout << j << " Last name: ";
             cin >> p->name[i].last;
         }
      }
      else
      {
          q=new aList;				
          strcpy(q->adr,adrs);
          
          cout << " Number of inhabitants: ";
          cin >> q->numPpl;  
          for (int i = 0, j = 1; i < q->numPpl; i++, j++)
          {
              cout << j << " Name: ";
              cin >> q->name[i].first;
              cout << j << " Last name: ";
              cin >> q->name[i].last;
          }
          q->next = p;
          pPrev->next = q;				
      }
}

void add_to_BST(node &n, point p)
{
     if (n==NULL)
      {
         n=new aTree;
         strcpy(n->adr, p->adr);
         for (int i=0; i<p->numPpl; i++)
         {
             strcpy(n->name[i].first, p->name[i].first);
             strcpy(n->name[i].last, p->name[i].last);
         }
             n->adrL=NULL;
             n->adrR=NULL;
             n->numPpl=p->numPpl;
      }
      
      else if (strcmp(n->adr, p->adr)>0)
              add_to_BST (n->adrL, p->next);
              
           else if (strcmp(n->adr, p->adr)<0)
                   add_to_BST (n->adrR, p->next);
                   
                else cout<<" Address exists! "<<endl;
}

void print_Tree(node n)
{

     if (n)
     {
        print_Tree(n->adrL);
        int j=1;
        cout << endl << "Inhabitants of '" << n->adr << "' :" << endl << endl;
        for (int i = 0; i < n->numPpl; i++, j++)
        {
            cout << "  "  << j << ". " << n->name[i].first << " " << n->name[i].last << endl;
        }
        
        print_Tree(n->adrR);
     }
}

int main()
{
    head=NULL;
    root=NULL;
    char adr[30];

    create(head);
    cout << endl << "print list:";
    print_List(head);
    add_to_BST(root, head);
    cout << endl << "print tree:";
    print_Tree(root);

    cout << endl << "end of program" << endl;
    system("PAUSE");
}

There are a couple of problems....I will answer this one....look at the function add_to_BST....ask yourself this question "What happens after you process the first list item?" That is, what happens after that? Don't you want to invoke the same function add_to_BST(n, p->next) only in the case that p->next is NOT NULL.

I got this to work (required slight change to the other two calls in the else part ...are you sure you don't want to send p instead of p->next.

Are the nodes supposed to be in some order?

I just want to add one note: I would not build a tree and traverse the list in one function...I would create a loop that takes the next item in the list to process and send it to "add to tree" function.

Thank you for the fast response. It's helpful I think now I will be able to make it work. I knew it was something simple that I'm missing.

Also they are in one function because the professor wants it like that.

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.