I have to check if a singly linked list forms a palindrome of characters. Each node keeps a character.
I can't use a double linked list. With the code I wrote I'm getting this on a few tests -1073741819 (0xC0000005)
I also get a warning : control reaches end of non-void function. I don't understand what I'm doing wrong...
bool palindrome(node *head)
{ node*last=head->next->next; //last gets this value so we can enter the
while loop
node* first=head;
while(first->next!=last && first!=last)
{ last=first;
while(last->next)
last=last->next; //getting the last value in the list
if(last->info!=first->info)
returnfalse; //end function if the list is not a
palindrome
else
{
node* first_del=first; //if first and last are equal, I
first=first_del->next; //delete both of them so I can check
delete first_del; //if the next elements in the list make
//up a palindrome
node*del=last;
last=NULL;
delete del;
}
}
if(first->next==last && first->next->info==last->info)
returntrue;
elseif(first==last)
returntrue;
}
//if all nodes to the middle of the list were deleted, and the value/s in the middle are still equal, the list is a palindrome.
control reaches end of non-void function
- a function which has a type (like int foo() ) did not contain a return statement or the return statement is inside a condition and the other path is missing one, etc.
being lazy, I have to note that:
-insert at head of a list is simple. It happens to reverse the input naturally.
- you already have the input string.
- if you were to, I dunno, iterate the list forwards comparing against the string's letters, it would tell you the answer using the above 2 bullets.
- you can also just insert 1/2 the chars then check what is left against what you put in, again using the insert at top reversal idea.
I don't care about the list as I only have to make this function work, but I can see why it's a bad practice. The list must have at least 3 nodes so that's not a problem either.
What I thought I was doing at line 23 was that after I deleted the last node, the last but one would be the "new last" node and the function would keep going. I'm going to try recursion too. Thanks a lot!