SIFE
SIFE

Reputation: 5695

Inserting a node in the front of linked list

I have a linked list, now I want to walk through it and each time I advance in it, I move the current node to the beginning of list like this:

4 5 3 2 7
5 4 3 2 7
3 5 4 2 7
2 3 5 4 7
7 2 3 5 4

The code:

#include <stdio.h>
#include <stdlib.h>

typedef struct _node {
    int p;
    struct _node *next;
} node;

main(int argc, char **argv)
{
    int i, n;
    node *nod = NULL;
    node *nod2 = NULL;
    node *nod_tmp = NULL;

    node *nod_next = NULL;
    node *nod_before = NULL;

    printf("Enter n: ");
    scanf("%d", &n);
    for(i = 0; i < n; ++i)
    {
        nod_tmp = (node *)malloc(sizeof(node));
        scanf("%d", &nod_tmp->p);
        if (i == 0)
        {
            nod = nod2 = nod_tmp;
        }
        nod2->next = nod_tmp;
        nod2 = nod_tmp;
    }

    nod_tmp = nod;
    while (nod_tmp->next != NULL)
    {
        nod_before = nod_tmp; // save current node
        nod_next = nod_tmp->next->next; // save next node
        nod_before->next = nod_next; // link the previous with the next one
        nod_tmp->next = nod; // point the current node to the beginning of list
    }

    while(nod != NULL)
    {   
        printf("%d\n", nod->p);
        nod = nod->next;
    }
    return 0;
}   

Upvotes: 2

Views: 761

Answers (4)

MOHAMED
MOHAMED

Reputation: 43518

#include <stdio.h>
#include <stdlib.h>

typedef struct _node {
    int p;
    struct _node *next;
} node;

main(int argc, char **argv)
{
    int i, n;
    node *nod = NULL;
    node *nod2 = NULL; //header of the linked list
    node *nod_tmp = NULL;

    printf("Enter n: ");
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        nod_tmp = (node *)malloc(sizeof(node));
        scanf("%d", &nod_tmp->p);
        nod_tmp->next = nod2;
        nod2 = nod_tmp;
    }

    nod = nod2;

    printf("Linked list order before reversing:  ");
    while(nod != NULL)
    {   
        printf("%d  ", nod->p);
        nod = nod->next;
    }
    printf("\n");

    nod = nod2;
    while (nod!=NULL && nod->next != NULL)
    {
        nod_tmp = nod2;
        nod2 = nod->next;
        nod->next = nod2->next;
        nod2->next = nod_tmp;
    }

    nod = nod2;

    printf("Linked list order after reversing:  ");
    while(nod != NULL)
    {   
        printf("%d ", nod->p);
        nod = nod->next;
    }
    printf("\n");
    return 0;
}   

Upvotes: 0

hmatar
hmatar

Reputation: 2419

nod_tmp = nod->next;
nod_before= nod;
while (nod_tmp != NULL)
{
     nod_before->next = nod_tmp->next;
     nod_tmp->next = nod;
     nod = nod_tmp;
     nod_tmp = nod_before->next;
}

Change your second loop to this loop above. Note that node_tmp points to the second node after the head and nod_before points to the first node. This way you avoid infinite loop.

Upvotes: 2

WhozCraig
WhozCraig

Reputation: 66194

Think seriously about what the while-loop reversing your list is actually doing. For example, assuming we had this list:

1 --> 2 --> 3 -->null

Lets walk your while-loop code:

nod_tmp = nod; // nod and nod_tmp now point to 1
while (nod_tmp->next != NULL)
{
    nod_before = nod_tmp; // nod_before, nod_tmp, and nod all point to 1
    nod_next = nod_tmp->next->next; // nod_next points to 3
    nod_before->next = nod_next; // 1-->3 
    nod_tmp->next = nod; // 1-->1
}

The first node (still referenced by nod_tmp) now points to itself. This will not only cause an infinite spin on your while-condition, it also leaks the rest of the memory in your list.

Upvotes: 2

beder
beder

Reputation: 1074

Inifinite loop right here:

while (nod_tmp->next != NULL)
    {
        nod_before = nod_tmp; // save current node
        nod_next = nod_tmp->next->next; // save next node
        nod_before->next = nod_next; // link the previous with the next one
        nod_tmp->next = nod; // point the current node to the beginning of list
    }

Upvotes: 0

Related Questions