hi everybody! thank you for viewing my post in order to help me i've created to linked list below start_ptr and begin_ptr. i just want to join the 2 linked lists in any order and i want to display the results the selected code below (in bold) is the one that should be modified to get the appropriate result thank u
#include < iostream.h>
struct node
{
int age;
node *nxt;
};
node *start_ptr= NULL;
node *current;
int choice = 7;
struct node1
{
int age1;
node1 *nxt;
};
node1 *begin_ptr= NULL;
node1 *current1;
void add_node1()
{ node *temp, *temp2; // Temporary pointers
// Reserve space for new node and fill it with data
temp = new node;
cout << endl<<"Please enter number: ";
cin >> temp->age;
temp->nxt = NULL;
// Set up link to this node
if (start_ptr == NULL)
{ start_ptr = temp;
current = start_ptr;
}
else
{ temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->nxt != NULL)
{ temp2 = temp2->nxt;
// Move to next link in chain
}
temp2->nxt = temp;
}
}
void display_node1()
{ node *temp;
temp = start_ptr;
cout << endl;
if (temp == NULL)
cout << "The list is empty!" << endl;
else
{ while (temp != NULL)
{ // Display details for what temp points to
cout << "Age : " << temp->age << endl;
cout << endl;
temp = temp->nxt;
}
cout << "End of list!" << endl;
}
}
void add_node2()
{ node1 *temp, *temp2; // Temporary pointers
// Reserve space for new node and fill it with data
temp = new node1;
cout <<endl<< "Please enter number: ";
cin >> temp->age1;
temp->nxt = NULL;
// Set up link to this node
if (begin_ptr == NULL)
{ begin_ptr = temp;
current1 = begin_ptr;
}
else
{ temp2 = begin_ptr;
// We know this is not NULL - list not empty!
while (temp2->nxt != NULL)
{ temp2 = temp2->nxt;
// Move to next link in chain
}
temp2->nxt = temp;
}
}
void display_node2()
{ node1 *temp;
temp = begin_ptr;
cout << endl;
if (temp == NULL)
cout << "The list is empty!" << endl;
else
{ while (temp != NULL)
{ // Display details for what temp points to
cout << "Age : " << temp->age1 << endl;
cout << endl;
temp = temp->nxt;
}
cout << "End of list!" << endl;
}
}
[B]
void merge_list(struct node1 *begin_ptr ,struct node *start_ptr)
{
if (start_ptr == NULL || begin_ptr == NULL)
return; // In case of null pointers, do nothing
while (begin_ptr->nxt != NULL)
begin_ptr = begin_ptr->nxt;
begin_ptr->nxt = start_ptr;
}[/B]
void main()
{
start_ptr = NULL;
begin_ptr = NULL;
do
{
cout << "***************************"<<endl;
cout << "1.Add Records to list #1"<<endl<<endl;
cout << "2.Display list #1" <<endl<<endl;
cout << "3.Add Records to list #2"<<endl<<endl;
cout << "4.Display list #2" <<endl<<endl;
cout << "5.Merge list #1 and list #2"<<endl<<endl;
cout << "6.Display Merged list" <<endl<<endl;
cout << "7.Quit"<<endl<<endl;
cout << endl;
cout << " >>>";
cin>> choice;
switch(choice)
{
case 1: add_node1();break;
case 2: display_node1();break;
case 3: add_node2();break;
case 4: display_node2();break
case 5: merge_list();break;
case 6: display_list();
}
}
while (choice != 7);
}


Sign In
Create Account

Back to top









