Hi Martyr2,
I worked on the insertion functions with this data structure this
afternoon and think that the insertions are working at this point. I
placed the prompt to enter more contacts into the list in the
BST_InsertContact function instead of where it was previously (in
BST_Insert) and I think that solved the problem. The display function
is now passed the head of the list (once the BST is traversed to find
the appropriate starting Node).
Can you take a quick glance at this and see if there's something I
forgot?
Thanks again for your help!
CODE
void LL::LL_Insert(Node *& treeNode)
{
char response = 'y';
while(response == 'y')
{
char temp[75];
Node * CurrPtr;
CurrPtr = new Node;
cin.get();
cout << "Enter contact's first name: " << endl;
cin.get(temp, 30, '\n');
CurrPtr->F_Name = new char[strlen(temp) + 1];
strcpy(CurrPtr->F_Name, temp);
cin.get();
cout << "Enter contact's last name: " << endl;
cin.get(temp, 30, '\n');
CurrPtr->L_Name = new char[strlen(temp) + 1];
strcpy(CurrPtr->L_Name, temp);
if (treeNode->Next == NULL)
{
cout << "creating new node...inserting as second node in this list..." << endl;
treeNode->Next = CurrPtr;
CurrPtr->Next = NULL;
CurrPtr->LChild = NULL;
CurrPtr->RChild = NULL;
}
else
{
cout << "first node of this list already points to a node...searching to insert..." << endl;
Node * currentPtr = treeNode;
Node * prevPtr = NULL;
while (currentPtr != NULL)
{
cout << "made it into the while loop...looping..." << endl;
prevPtr = currentPtr;
cout << "just set prevPtr = newPtr..." << endl;
currentPtr = currentPtr->Next;
cout << "just set newPtr = newPtr->Next..." << endl;
}
cout << "made it out of the while loop..." << endl;
CurrPtr->LChild = NULL;
CurrPtr->RChild = NULL;
if (prevPtr == NULL)
{
CurrPtr->Next = currentPtr;
treeNode->Next = CurrPtr;
}
else
{
CurrPtr->Next = currentPtr;
prevPtr->Next = CurrPtr;
}
}
cout << "Do you wish to add more contacts to this emergency account? (y-yes, n-no)";
cout << endl;
cin >> response;
}
}
I used the LL_Insert function above with these BST insert functions and I think the algorithm is now working.
CODE
void BST::BST_Insert()
{
Node * treePtr;
treePtr = new Node;
treePtr->LChild = NULL;
treePtr->RChild = NULL;
treePtr->Next = NULL;
bool Success;
char temp[75];
cin.get();
cout << "Enter contact's first name: " << endl;
cin.get(temp, 30, '\n');
treePtr->F_Name = new char[strlen(temp) + 1];
strcpy(treePtr->F_Name, temp);
cin.get();
cout << "Enter contact's last name: " << endl;
cin.get(temp, 30, '\n');
treePtr->L_Name = new char[strlen(temp) + 1];
strcpy(treePtr->L_Name, temp);
BST_InsertContact(Root, treePtr, Success);
}
void BST::BST_InsertContact(Node *& Root, Node *& treePtr, bool & Success)
{
char response = 'y';
if (Root == NULL)
{
Root = new Node;
Root->LChild = NULL;
Root->RChild = NULL;
Root->Next = NULL;
Root->F_Name = new char[strlen(treePtr->F_Name) + 1];
strcpy(Root->F_Name, treePtr->F_Name);
Root->L_Name = new char[strlen(treePtr->L_Name) + 1];
strcpy(Root->L_Name, treePtr->L_Name);
Success = bool(Root != NULL);
cout << "Do you wish to add more contacts to this emergency account's list? (y-yes, n-no)";
cout << endl;
cin >> response;
if (response == 'y')
LL::LL_Insert(Root);
}
else if (Root->L_Name < treePtr->L_Name)
BST_InsertContact(Root->LChild, treePtr, Success);
else
BST_InsertContact(Root->RChild, treePtr, Success);
}