Tuesday, March 30, 2010
reverse the linked list using recursive method
Algorithm:
Here the algorithm is, pointing p and q to the next elements in every iteration. Once we reached the end, then start pointing to back, i.e.., q->next = p;
//structure
struct Node
{
int data;
struct Node* next;
};
//global variables...
struct node* start = NULL;
struct node* p = NULL;
struct node* q = NULL;
//assusme that we have added some elements into the List, and start
//is pointing to first element in the list
reverseList(start);
void reverseList(struct Node* node)
{
if (node->next == NULL)
break;
p = node->next;
q = p->next;
if (q->next != NULL)
{
reverseList(q);
}
else
{
start->next = NULL;
start = q;
}
q->next = p;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment