简体   繁体   中英

Insert an element into a linked list

If I have two structures defined as follow, how can I insert a new element in the list ? I tried something, but I don't know where is the mistake. The function for inserting a new element is called insertFirst. In main I have lst=insertFirst(lst,7); lst=insertFirst(lst,8); lst=insertFirst(lst,7); lst=insertFirst(lst,8); Thank you!

typedef int DATA;
    struct element {
    DATA cheie;
    struct element *urm;
};
typedef struct element Element, *ELEMENT;

struct lista {
    int nr; //nr elemente
    ELEMENT inceput;
    ELEMENT sfarsit;
};
typedef struct lista Lista, *LISTA;


LISTA insertFirst(LISTA l, DATA x)
{
    LISTA w;
    w = (LISTA)malloc(sizeof(Lista));
    if (w == NULL)return NULL;
    w->inceput=(LISTA)malloc(sizeof(Lista));
    w->inceput->cheie = x;
    w->inceput->urm = l;

    LISTA p = l;
    for (; p->inceput->urm != NULL; p = p->inceput->urm);

    w->sfarsit->cheie = p->inceput->cheie;
    w->sfarsit->urm = NULL;
    return w;
}

It seems you mean the following. That is you have a two-sided singly-linked list.

LISTA insertFirst(LISTA l, DATA x)
{
    ELEMENT w;
    w = (ELEMENT)malloc(sizeof(Element));

    if ( w )
    {
        w->cheie = x;
        w->urm = l->inceput;

        l->inceput = w;
        if ( !l->sfarsit ) l->sfarsit = w;

        ++l->nr;
    }

    return l;
}

Another way to define the function is the following

int insertFirst(LISTA l, DATA x)
{
    ELEMENT w;
    w = (ELEMENT)malloc(sizeof(Element));

    int success = w != NULL;

    if ( success )
    {
        w->cheie = x;
        w->urm = l->inceput;

        l->inceput = w;
        if ( !l->sfarsit ) l->sfarsit = w;

        ++l->nr;
    }

    return success;
}

And in main you should define the list like

Lista l = { 0, NULL, NULL };

Here is a demonstrative program

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

typedef int DATA;

struct element 
{
    DATA cheie;
    struct element *urm;
};
typedef struct element Element, *ELEMENT;

struct lista 
{
    int nr; //nr elemente
    ELEMENT inceput;
    ELEMENT sfarsit;
};
typedef struct lista Lista, *LISTA;

int insertFirst(LISTA l, DATA x)
{
    ELEMENT w;
    w = (ELEMENT)malloc(sizeof(Element));

    int success = w != NULL;

    if ( success )
    {
        w->cheie = x;
        w->urm = l->inceput;

        l->inceput = w;
        if ( !l->sfarsit ) l->sfarsit = w;

        ++l->nr;
    }

    return success;
}

void outputLista( LISTA l )
{
    printf( "%d: ", l->nr );

    for ( ELEMENT current = l->inceput; current; current = current->urm )
    {
        printf( "%d ", current->cheie );
    }
}

int main(void) 
{
    Lista l = { 0, NULL, NULL };

    insertFirst( &l, 7 );
    insertFirst( &l, 8 );

    outputLista( &l );
    putchar( '\n' );

    return 0;
}

Its output is

2: 8 7 

As for your function implementation then it does not make sense. You do not need to create a new list in the function. What you need is to create a new element and insert it before the current first element of the list.

The function that inserts a new element at the end of the list can look like

int insertLast(LISTA l, DATA x)
{
    ELEMENT w;
    w = (ELEMENT)malloc(sizeof(Element));

    int success = w != NULL;

    if ( success )
    {
        w->cheie = x;
        w->urm = NULL;

        if ( l->sfarsit )
        {
            l->sfarsit->urm = w;
        }
        else
        {
            l->inceput = w;
        }

        l->sfarsit = w;

        ++l->nr;
    }

    return success;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM