Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to reverse a singly linked list recursively? 1

Status
Not open for further replies.
What I would do is
- Have a function that would count the number of Nodes in the list.
- Have this number in the recursive function
- Have a loop that would move a temporary pointer to the last one in the list

// function call

reversingList(number,primaryPointer,newPointer,counter,0);

// function definition

void reversingList(int number,Node *primaryPointer,Node *newPointer,int counter);
{
static Node *temp = primaryPointer;
static Node array[number];

if ( number == 0 )
{
while ((*temp).GetNextNodePtr() != NULL )
{
(*temp) = array[counter];
temp = (*temp).GetNextNodePtr();
}

// Assigning to a new list
while ( counter != 0 )
{
temp = new Node(array[counter]);
newPointer = temp;
newPointer = (*newPointer).GetNextNodePtr();
counter --;
}
(*newPointer).SetNextNodePtr(NULL); // assign last ptr to NULL
}

if ( number != 0 )
{
counter++;
reversing(number-1,primaryPointer,counter);
}
} Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top