linked list reverse and memoryData Structures -- Linked List -- Reverse
in the some of your funntions when returning the memory becomes null
head->value=ok
head->next becomes null after function exit
==============
void ReverseList(LISTP* head)
{
LISTP *temp,*current,*result;
temp=NULL;
result=NULL;
current=head;
while(current!=NULL)
{
temp=current->next;
current->next=result;
result=current;
current=temp;
}
head=result;
}
===================
if I print hte list inside the function it is ok
void display_list(LISTP* listp)
{
LISTP* temp=listp;
while(temp->next!=NULL)
{
printf("%dand",temp->data);
temp=temp->next;
}
}
======================
but if I exit the function the next pointer becomes null , I suppoise the solution is using static pointer allocated outside the function ...
any way my idea
==========
void reverselist(LISTP* listp)
{
LISTP* next = (LISTP*)malloc(sizeof(LISTP));
LISTP* next_of_next = (LISTP*)malloc(sizeof(LISTP));
next=listp->next;
if(next!=NULL)
next_of_next = next->next;
listp->next = NULL;
while(next_of_next!=NULL)
{
next->next=listp;
listp=next;
next=next_of_next;
next_of_next=next_of_next->next;
}
display_list(listp);
//free(next); ??? is nneeded
//free(next_of_next); is needed}
-reply by michael
Keywords: reverse linked list