Can any one help me in reversing the single linked list and at the same time i want to print the reversed links.
Should be able to do this in a single loop....
struct item
{
struct item*next;
};
struct item *forward=get_list_to_revese();
struct item *reverse=NULL;
while (forward)
{
struct item *p=forward; /* pop first from forward */
forward=forward->next;
p->next=reverse; /* push onto reverse */
reverse=p;
printf("%p\n",p);
}
recursion works as well -
#include <stdlib.h>
typedef struct
list
{
int data;
struct list *next;
} list_t;
void rev(list_t *p)
{
if(p->next !=NULL) rev(p->next);
printf("%d\n", p->data);
}
If you need to store the reversed list:
void rev(list_t *p, list_t **reverse)
{
if(p->next !=NULL) rev(p->next);
printf("%d\n", p->data);
*reverse=p;
reverse++;
}