This is the approach I used but it gives segmentation fault. Please go through the code below and let me know what is the mistake
Even after odd error
hello @prerna_gupta31
ur loop conditions are not correct.
u are always using while(pointer->next!=NULL) as stopping condition.
this will give error if pointer is itself NULL.
u should use while(pointer!=NULL) as stopping condition.
i m not sure about ur logic ,pls explain it so that i can check its working
even is the pointer that will point to point to the first node containing even number.( βitβ is the pointer that is used to traverse through the loop and prev points to the previous node.). We traverse through the linked list meanwhile if we find any node that stores odd number then prev->next = it->next and it->next=even
prev->next = it->next and it->next=even this is done to remove the odd node from itβs current position and and insert it before first even node
see the logic is correct but the way u r implementing is not correct.
this is what u r trying.
u are maintaining an odd list right? and even head pointer
whenever u r encountring odd then u r adding it to end of odd list right?( or u can say we are adding before the head of even list ).
did i get u correct?
Yes, this is exactly what I am trying to do
https://ide.codingblocks.com/s/365198 I tried a different approach this time. It gives some runtime error
check ur newly created thread , i replied over there
evenhead=temp; eventail=temp; what does these lines mean?
Sorry I mean these lines: else { oddtail->next=temp; oddtail=temp; }
when our evenhead is NULL that means we have not found any even element so far.
we whenever we ecounter very first even number , we will update our head and tail .
these two lines are doing same.
we are making temp as evenhead and because it is only node in the list so far we are making it tail as well hence the line eventail=temp.
first we are adding temp to end of odd list.
oddtail->next=temp .// this will add temp to the end of the list
and now becuase temp is the last node of odd list. we need to make it tail.
hence we r using this line
oddtail=temp