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");
}