language C : help for linked list

hello !
are there somebody that can help me for to understand this code ?
ive examin early!
thanks


=====================================================================

#include
#include
#include


struct listaNodi {
char character;
struct listaNodi *nextPtr;
};
typedef struct listaNodi ListaNodi;
typedef ListaNodi *ListNodePtr;


void inserisci( ListNodePtr *sTesta, char value );
/* definisco un prototipo di funzione per inserire i caratteri */


int main( )
{
ListaNodiPtr Testa = NULL ;
char carattere;
int risposta ;

printf ("vuoi provare ?? digita 1 o 0 per uscire
" ) ;
scanf ("%d" , & risposta ) ;


while ( risposta == 1 )
{
inserisci( &Testa, carattere );
printf ("vuoi provare ancora ?? digita 1 o 0 per uscire
" ) ;
scanf ("%d" , & risposta ) ;
}

return 0;


}

void inserisci( ListNodePtr *sTesta, char value )
{
ListaNodiPtr Nuovo;
ListaNodiPtrCorrente;
Nuovo = malloc( sizeof( ListaNodi ) );

if ( Nuovo != NULL ) {
Nuovo->character = value;
Nuovo->nextPtr = NULL;
Precedente = NULL;
Corrente = *sTesta;

while (Corrente != NULL && value >Corrente->character ) {
Precedente =Corrente;
Corrente =Corrente->nextPtr;
}

if ( Precedente == NULL ) {
Nuovo->nextPtr = *sTesta;
*sTesta = Nuovo;
}
else {
Precedente->nextPtr = Nuovo;
Nuovo->nextPtr =Corrente;
}
}
else {
printf( "%c memoria non disponibile .
", value );
}

}



ive some quenstion ;
1)how the compilator find that struct if i dont creat it ?
how to create a new variable of this type ?
it's similar like this " char *Ptr = " value" " ; ??







why to write in that mode
*testa =( *Testa ) -> carattere ; ?



Thanks



Comments

  • : hello !
    : are there somebody that can help me for to understand this code ?
    : ive examin early!
    : thanks
    :
    :
    : =====================================================================
    : [code]
    : #include
    : #include
    : #include
    :
    : struct listaNodi
    : {
    : char character;
    : struct listaNodi *nextPtr;
    : };
    :
    : typedef struct listaNodi ListaNodi;
    : typedef ListaNodi *ListNodePtr;
    :
    :
    : void inserisci( ListNodePtr *sTesta, char value );
    : /* definisco un prototipo di funzione per inserire i caratteri */
    :
    :
    : int main( )
    : {
    : ListNod[color=Red]e[/color]Ptr Testa = NULL ;
    : char carattere;
    : int risposta ;
    :
    : printf ("vuoi provare ?? digita 1 o 0 per uscire
    " ) ;
    : scanf ("%d" , & risposta ) ;
    :
    : while ( risposta == 1 )
    : {
    : inserisci( &Testa, carattere [color=Red]/* <-- used without init */ [/color]);
    : printf ("vuoi provare ancora ?? digita 1 o 0 per uscire
    " ) ;
    : scanf ("%d" , & risposta ) ;
    : }
    :
    : return 0;
    : }
    :
    : void inserisci( ListNodePtr *sTesta, char value )
    : {
    : ListNod[color=Red]e[/color]Ptr Nuovo;
    : ListNod[color=Red]e[/color]Ptr Corrente;
    : [color=Red]ListNodePtr Precedente; // Missing variable[/color]
    : Nuovo = malloc( sizeof( ListaNodi ) );
    :
    : if ( Nuovo != NULL ) {
    : Nuovo->character = value;
    : Nuovo->nextPtr = NULL;
    : Precedente = NULL;
    : Corrente = *sTesta;
    :
    : [color=Green]// This loop searches for a linked list item
    : // which is a structure ListaNodi and this item must
    : // have its value of 'character' following the 'value'.
    : // In other words it will insert a new node in SORTED
    : // ORDER.[/color]
    : while (Corrente != NULL && value >Corrente->character ) {
    : Precedente =Corrente;
    : Corrente =Corrente->nextPtr;
    : }
    :
    : if ( Precedente == NULL ) {
    : [color=Green]// Inserting at the very beginning of the linked list[/color]
    : Nuovo->nextPtr = *sTesta;
    : *sTesta = Nuovo;
    : }
    : else {
    : [color=Green]// Inserting between nodes 'Precedente' and 'Nuovo'[/color]
    : Precedente->nextPtr = Nuovo;
    : Nuovo->nextPtr =Corrente;
    : }
    : }
    : else {
    : printf( "%c memoria non disponibile .
    ", value );
    : }
    : }
    : [/code]
    :
    : ive some quenstion ;
    : 1)how the compilator find that struct if i dont creat it ?
    : how to create a new variable of this type ?
    : it's similar like this " char *Ptr = " value" " ; ??
    :
    : [color=Blue]Which structure? I do not understand the question.[/color]
    :
    :
    : why to write in that mode
    : *testa =( *Testa ) -> carattere ; ?
    :
    : [color=Blue]I also do not understand the question here - there is no such code in that code above.[/color]
    :
    : Thanks
    :
    :
    :
    :
  • hello !
    thanks for help!

    i explain !

    usually you prepare structure in this mode :

    struct node
    {
    int x ;
    char x ;
    } ;

    and after you declaire

    struct node node ;

    but in that code it wasn't , and i ask myself ,
    how the compilator prepare each node ? how to find structure ?

    it's like when you declaire one string with pointer ?
    like char *xxx = "xxxx" ; ???


    thanks


  • [color=Blue]The very first node for a list in this case is declared in a main() function with this line:[/color]
    [code]
    ListNodePtr Testa = NULL;
    [/code][color=Blue]Then later, in a loop, this pointer is passed to inserisci() function and it is passed by address (like so: "&Testa"). Because of passing by address this value can be modified by a function. The new node for a list is created by function malloc(). New node is inserted BEFORE the Testa or BETWEEN some nodes after a search is done to find a node which will follow the new one.

    So, the answer is: every new node is created by a malloc() function. This function is used to allocate memory blocks from free memory reserves for a program. If no more memory available - it will return NULL value.[/color]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories