This is homework. I am not asking for someone to write this code for me, just to help me understand how to solve my problem.
I have to take two lists or integers inputted by the user, sorted in ascending order and combine them using pass-by-reference to return a pointer to the head of a new list that contains all the values from the first list, now sorted in ascending order together. Ex: List A contains 3,5,6,10; List B contains 1,4,5,7,10,11. The resultant list should output 1,3,4,5,5,6,7,10,10,11.
I am very new at lists, and don't quite understand them fully but what I want to do for the merge_list function is this (this is kind of pseudocode, as I am trying to convert it to real code. This is what I need the most help on):
int list1counter;
int list2counter
while (list1counter < list1size)
{
if(list1value>list2value)
{
if (list1value<list2value_at_next_position)
{
insert_here(list1value);
list1counter++;
}
else
{
list2counter++;
}
else
{
if(top_of_list)
{
head_insertl2(list1value);
}
else
{
cout << "error; value processed improperly";
return 0;
}
}
}
I defined my pointers like so:
typedef s1* s1p;
s1p *iter;
I started using an iterator to cycle through but I'm not sure how it will work since I have to cycle through two lists basically simultaneously. The iterator currently looks like for(iter = head1; iter != NULL; iter = iter->link)
.
My insert function:
int insert_here(s1p after_me, int numins)
{
s1p temporary;
temporary = new Node;
temporary->data = numins;
temporary->link = after_me->link;
after_me->link = temporary;
}
Basically I'm not sure how to use the iterator to cycle through both lists and compare the values at both positions. If anyone could give some pointers (no pun intended), that would be great.