I wrote the code about merging two sorted lists. However,just the head1 running not the head2. For example, head1: 0 2 5 7 head2: 0 5 8 9. The output will be 0 2 5 7. Could anyone tell me why?
#include <iostream>
using namespace std;
// class node
class node {
private:
double num;
node *link;
public:
node() { }
node(double m, node *n) { num = m; link = n; }
node* getlink() { return link; }
double getdata() { return num; }
void setdata(double m) { num = m; }
void setlink(node* n) { link = n; }
};
typedef node* nodeptr;
void insertnode(nodeptr& head, double m);
void printlist(nodeptr head);
nodeptr mergelists(nodeptr& head1, nodeptr& head2);
void reverselist(nodeptr& head);
nodeptr search(nodeptr head, double searchterm);
void insert(nodeptr afterme, double newdata);
int main()
{
double input;
nodeptr head1 = NULL; // Pointer to the head of List #1
nodeptr head2 = NULL; // Pointer to the head of List #2
nodeptr temp;
// Part 1 - Create two sorted lists
cout << "-------------------------------------" << endl;
cout << "CREATE LIST #1: " << endl;
cout << "-------------------------------------" << endl;
do {
cout << "Enter value (0 to quit): ";
cin >> input;
// Insert the "input" value into the list
insertnode(head1, input);
} while (input != 0);
cout << "-------------------------------------" << endl;
cout << "CREATE LIST #2: " << endl;
cout << "-------------------------------------" << endl;
do {
cout << "Enter value (0 to quit): ";
cin >> input;
// Insert the "input" value into the list
insertnode(head2, input);
} while (input != 0);
// Part 1 - Print the lists to make sure that they are correct.
printlist(head1);
printlist(head2);
// Part 2 - Merge the two lists and display the new merged list
cout << "Merge lists: " << endl;
temp = mergelists(head1, head2);
printlist(temp);
// Part 3 - Reverse the merged list and then display it
return 0;
}
nodeptr search(nodeptr head, double searchterm)
{
nodeptr p = head;
nodeptr q = head->getlink();
if (p == NULL)
return NULL;
else
{
while (p != NULL)
{
q = p->getlink();
while (q != NULL && q->getdata() < searchterm)
{
p = p->getlink();
q = q->getlink();
}
return p;
}
}
}
void insertnode(nodeptr& head, double m)
{
// CASE 1 - List is empty
if (head == NULL)
{
head = new node(m, NULL);
}
// CASE 2 - List is not empty and new value is < 1st value
else if (m < head->getdata())
{
head = new node(m, head);
}
// CASE 3 - List is not empty and new value goes inside list
else
{
// search for correct location - notes on Search
nodeptr afterme = search(head,m);
// insert at this location -- see notes on insert inside list
nodeptr temp;
temp = new node(m, afterme->getlink());
afterme->setlink(temp);
}
}
void printlist(nodeptr head)
{
nodeptr p;
p = head;
while (p != NULL)
{
cout << p->getdata() << endl;
p = p->getlink();
}
}
// mergelist function -> wrong result
nodeptr mergelists(nodeptr& head1, nodeptr& head2)
{
if (head1 == NULL)
return head2;
if (head2 == NULL)
return head1;
nodeptr result = new node(0, NULL);
nodeptr head = result;
while (head1 != NULL&&head2 != NULL){
if (head1->getdata() <head2->getdata()){
result ->getlink() = head1;
head1 = head1 -> getlink();
}
else{
result->getlink() = head2;
head2 = head2->getlink();
}
result = result ->getlink();
}
if (head1 == NULL){
result ->getlink() = head2;
}
else{
result ->getlink() = head1;
}
return head->getlink();
}
Here is my output: