printing Reverse of linked list without reversing it actually

1st Method: by a recursive routine or

2nd Method:using a stack.

1st:Recursive
go to the end then print the nodes, i.e. recursive call comes before printing the node…
void printreverse(node *node1)
{
if (node1)
{
printreverse(node1->link);
printf(“%d -> “,node1->data);
}
}

2nd method: using stack

Push all the nodes, pop one by one and print them

void printreverse(node* node1)
{
node *tmpnode;

Stack *stack = CreateStack();

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

tmpnode = Pop(stack);

while(tmpnode)
{
tmpnode = Pop(stack);
printf(“%d -> “, tmpnode->data);
}

DestroyStack(stack);
}

Advertisements

2 Responses to “printing Reverse of linked list without reversing it actually”

  1. promotional items Says:

    promotional items…

    […]printing Reverse of linked list without reversing it actually « TheWarrior[…]…

  2. celebs without makeup Says:

    I like the valuable information you supply in your articles.
    I’ll bookmark your blog and check once more here regularly. I am somewhat sure I’ll be told
    many new stuff right right here! Good luck for the next!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: