In: Computer Science
When I run this, gdb gives me a segmentation fault at the for loop. It says I'm dereferencing curr with curr->data when curr is null. It doesn't even seem to enter the body of the loop sometimes. I am testing it on a linked list that ends with a strange element. Could you help me get this one done? Thanks so much! Here is the instruction and code:
LewList *splitStrange();
This function should split the list in two, adding the elements with "strange" values into a new linked list, and keeping only those elements with "normal" (i.e. not strange) values in the list object on which this function is called. The function should return a pointer to the new (heap allocated) list with the strange elements in it. If the original list is empty, or if there are no strange elements, the function should return a pointer to a new empty LewList (not a pointer to nullptr).
We define "strange" for integers as "odd". (Look it up... odd is a synonym for strange!). See Strange.h for function templates defining a strange() function implementing this. Your implementation of splitStrange() should call strange() to determine whether to keep a particular element.
Sample Behavior
If the original list contains integers 3, 4, 18, 2, 9, 1, 10, 22, and 233, then this function should return a pointer to a new LewList containing integers 3, 9, 1, and 233 (in any order). And the original list should be altered to contain only integers 4, 18, 2, 10, and 22 (in any order).
// splitStrange()
template <typename ElementType>
LewList<ElementType>
*LewList<ElementType>::splitStrange()
{
LewList<ElementType> *rlist = new
LewList<ElementType>;
ElementType strangeval;
if(num_elements == 0)
{
return rlist;
}
Node*curr = list_head;
Node*temp = nullptr;
Node*prev = list_head;
while(curr != nullptr)
{
if(strange(curr->nullptr))
{
strangeval =
curr->data;
for(curr =
list_head; curr->data != strangeval; curr = curr->next)
{
prev = curr;
std::cout<<curr->data;
}
prev->next =
curr->next;
temp =
curr;
curr =
curr->next;
delete
temp;
num_elements--;
rlist->insert(strangeval);
}
else
{
curr =
curr->next;
}
}
return rlist;
}
//Strange.h
template <typename ElementType> bool strange(const ElementType &element) { return true; } bool strange(const int &element) { return (element % 2); }
There are some changes you need to make. I am using comment here to mark your mistake and make desired changes in your code.
LewList<ElementType> *LewList<ElementType>::splitStrange()
{
LewList<ElementType> *rlist = new LewList<ElementType>;
ElementType strangeval;
if(num_elements == 0)
{
return rlist;
}
Node*curr = list_head;
Node*temp = nullptr;
Node*prev = list_head;
while(curr != nullptr)
{
if(strange(curr->data))/// Strange check for int value
{
strangeval = curr->data;
for(curr = list_head; curr->data != strangeval; curr = curr->next)
{
prev = curr;
std::cout<<curr->data;
}
prev->next = curr->next;
temp = curr;
curr = curr->next;
delete temp; ///As asked in question you should not delete this but leave remaining list
num_elements--;
rlist->insert(strangeval);
}
else
{
curr = curr->next;
}
}
return rlist;
}
I hope now this will work fine. If you could provide structure of list, I could test for output.