Home > Net >  How to put the 1st element of a list at the end?
How to put the 1st element of a list at the end?

Time:12-30

I have a problem with my linked list. I just want to put the first element to the end but I don't know how to do it. Thanks for your help

typedef struct Element Element_t;
struct Element
{
    int nbr;
    Element_t *next;
};

This is my function to put the first element to the end. But that just take the first element and I don't know how to put it at the end.

void my_ra(Element_t **l_a)
{
    Element_t *temp;

    if ((*l_a) && (*l_a)->next)
        *l_a = (*l_a)->next;
}

The first list is when I do nothing, the second is when I try to put the first element at the end.

1 2 9 4 5 $
2 9 4 5 $

CodePudding user response:

The function can be defined the following way

void my_ra(Element_t **l_a)
{
    if ( *l_a != NULL && ( *l_a )->next != NULL )
    {
        Element_t *tmp = *l_a;
        *l_a = ( *l_a )->next;
        
        tmp->next = NULL;

        while ( *l_a != NULL ) l_a = &( *l_a )->next;

        *l_a = tmp;
    }
}

This part of the function

while ( *l_a != NULL ) l_a = &( *l_a )->next;

*l_a = tmp;

can be substituted for this code snippet.

Element_t *current = *l_a;

while ( current->next != NULL ) current = current->next;

current->next = tmp;

In this case the function will look like

void my_ra(Element_t **l_a)
{
    if ( *l_a != NULL && ( *l_a )->next != NULL )
    {
        Element_t *tmp = *l_a;
        *l_a = ( *l_a )->next;
        
        tmp->next = NULL;

        Element_t *current = *l_a;

        while ( current->next != NULL ) current = current->next;

        current->next = tmp;
    }
}

Here is a demonstration program.

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

typedef struct Element Element_t;
struct Element
{
    int nbr;
    Element_t *next;
};

int push( Element_t **l_a, int nbr )
{
    Element_t *new_element = ( Element_t * )malloc( sizeof( *new_element ) );
    int success = new_element != NULL;

    if (success)
    {
        new_element->nbr = nbr;
        new_element->next = *l_a;
        *l_a = new_element;
    }

    return success;
}

void display( const Element_t *l_a )
{
    for (; l_a != NULL; l_a = l_a->next)
    {
        printf( "%d -> ", l_a->nbr );
    }

    puts( "null" );
}

void my_ra( Element_t **l_a )
{
    if (*l_a != NULL && ( *l_a )->next != NULL)
    {
        Element_t *tmp = *l_a;
        *l_a = ( *l_a )->next;

        tmp->next = NULL;

        while (*l_a != NULL) l_a = &( *l_a )->next;

        *l_a = tmp;
    }
}

int main( void )
{
    Element_t *l_a = NULL;

    enum { N = 10 };

    for (int i = N; i != 0; i--)
    {
        push( &l_a, i );
    }

    display( l_a );

    do
    {
        my_ra( &l_a );
        display( l_a );
    } while (l_a->nbr != 1);
}

The program output is

1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 1 -> null
3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 1 -> 2 -> null
4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 1 -> 2 -> 3 -> null
5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 1 -> 2 -> 3 -> 4 -> null
6 -> 7 -> 8 -> 9 -> 10 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
7 -> 8 -> 9 -> 10 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
8 -> 9 -> 10 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
9 -> 10 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
10 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null

CodePudding user response:

Thanks but if I want to make it just with a simple pointer like

Element_t *l_a

I try to do this but it doesn't work !

void my_ra(Element_t *list)
{
    Element_t *temp1 = list;
    Element_t *temp2 = NULL;

    while (list->next->next != NULL)
        list = list->next;
    temp2 = list->next;
    temp2->next = temp1->next;
    temp1->next = NULL;
    list->next = temp1;
}

that make me the same as before first list : 1 2 3 4 after compilation : 1

Thx for your help

  • Related