Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

30
Mar

Reversing a single linked list using stack

Related Blog Items

Here is the routine for reversing single linked list using a stack, for reversing linked list using pointers, see the earlier post. For reversing linked list recursively visit this post. This routine takes a single linked list as argument and reverse it and returns back the head node to the reversed list.


struct Node *reverse(struct Node* node)
{
  struct Node *tmpnode, *headnode;

  Stack *stack = CreateStack();

  while(node)
  {
    Push(stack, node);
    node = node->next;
  }

  headnode = Pop(stack);
  tmpnode = node = headnode;

  while(tmpnode)
  {
    tmpnode = Pop(stack);
    node->next = tmpnode;
    node = node->next;
  }

  DestroyStack(stack);
  return headnode;
}

comments welcome…

Popularity: 43%

You need to log on to convert this article into PDF


Related Blog Items

1 Comment

  • raad  said:

    hello , i been trying to check some of the articles here and alot dont work .. like the reverse using stack and this one! .. please note.


Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image