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;
}