Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [C] Cancellazione elementi lista

    Vorrei sapere , se ho una lista di elementi fatti così :

    codice:
    typedef struct Structure {
         int Data;
         struct Structure *Next;
    }Element;
    E di queste strutture ne ho allocate 10,
    come faccio poi a svuotare l'intera lista ?

    Grazie a tutti

  2. #2
    Utente di HTML.it
    Registrato dal
    May 2008
    Messaggi
    475
    Le hai allocate come? E' una lista o un vettore?
    "Let him who has understanding reckon the number of the beast, for it is a human number.
    Its number is rw-rw-rw-."

  3. #3
    Originariamente inviato da Ippo343
    Le hai allocate come? E' una lista o un vettore?
    Le ho allocate con la malloc, è una lista...

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Devi deallocare lo spazio allocato ... usi la free.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  5. #5
    Basati su questo : (Fatto da me)

    codice:
    #include <stdio.h>
    #include <stdlib.h> 
    
    typedef struct Structure {
         int Data;
         struct Structure *Next;
     }Element;
    
    Element *AddElements ();
    
    Element *ViewElements (Element *List);
    
    int main ()
    {
         Element *List;
         AddElements ();
         system ("CLS");
         ViewElements (List);
         system ("PAUSE");
         return 0;
    }
    
    Element *AddElements ()
    {
         Element *List = NULL;
         Element *Pointer;
         int Elements;
         int i;
         printf ("Elementi della lista : ");
         scanf ("%i", & Elements);
         if (Elements == 0)
         {
             List = NULL;
         }else{
             List = (Element *)malloc(sizeof(Element));
             printf ("Inserisci il 1 elemento : ");
             scanf ("%i", & List -> Data);
             Pointer = List;
             for (i = 0 ; i < Elements - 1 ; i++)
             {
                 Pointer -> Next = (Element *)malloc(sizeof(Element));
                 Pointer = Pointer -> Next;
                 printf ("Inserisci il %i elemento : ", i + 2);
                 scanf ("%i", & Pointer -> Data);
             }
             Pointer -> Next = NULL;
         }
         return List;
    }
    
    Element *ViewElements (Element *List)
    {
         printf ("Lista :\n\n");
         while (List != NULL)
         {
             printf ("%i -----> ", List -> Data);
         }
         printf ("NULL\n\n");
     }
    Come faccio a svuotare questa benedetta lista ?

  6. #6
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Te l'ho già detto ... prendi tutti i benedetti puntatori diversi da NULL, dall'ultimo al primo, e li usi con la benedetta free per deallocare la memoria.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  7. #7

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.