I hope that you are very well aware about Linked-List. Here we are talking about reversing a Linked-List which means that first node should become last node of a Linked-List and Vice-versa...
void Reverse_Linked_List(void)
{
// When there is no Node in Linked-List
if(List_head==0)
return;
//When there is only one Node in the Linked-List
if(List_head->next==0)
return;
//When there are only two Nodes in the Linked-List
if(List_head->next==List_tail)
{
List_head->next = 0;
List_tail->next = List_head;
}
else
{
List_node* pre = List_head;
List_node* cur = List_head->next;
List_node* curnext = List_cur->next;
List_head->next = 0;
List_cur-> next = List_head;
for(; curnext!=0; )
{
List_cur->next = pre;
pre = cur;
cur = curnext;
curnext = curnext->next;
}
curnext->next = cur;
}
}
void Reverse_Linked_List(void)
{
// When there is no Node in Linked-List
if(List_head==0)
return;
//When there is only one Node in the Linked-List
if(List_head->next==0)
return;
//When there are only two Nodes in the Linked-List
if(List_head->next==List_tail)
{
List_head->next = 0;
List_tail->next = List_head;
}
else
{
List_node* pre = List_head;
List_node* cur = List_head->next;
List_node* curnext = List_cur->next;
List_head->next = 0;
List_cur-> next = List_head;
for(; curnext!=0; )
{
List_cur->next = pre;
pre = cur;
cur = curnext;
curnext = curnext->next;
}
curnext->next = cur;
}
}
No comments:
Post a Comment