Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 15
  1. #1

    [C] help creazione lista

    Buongiorno a tutti ...

    Sono fermo da più di due giorni perche non riesco a creare una lista nel mio programmino in C.

    codice:
    while(fgets(s,254,FileIn))
      
            {                
                 if (strstr (s, par) != NULL)
                       {
                            printf ("%s\n", s);
                            y++;
                       
                       }
                 else 
                       
                            x++;
            }
    E' il solito codice ...

    Come faccio a inizializzare una lista, in modo che al posto di eseguire il PRINTF (%S\N", S) mi metta ogni elemento S, che in questo caso sono STRINGHE in una LISTA?

    Mi serve qualche delucidazione per partire perchè non ho mai affrontato prima d'ora le liste ...

    GRAZIE A TUTTI

    CIAO

  2. #2
    codice:
    typedef struct List List;
    
    void init(List *aux, char *el);
    void add(List *aux, char *el);
    
    struct List
    {
      char *el;
      List *next;
    };
    
    void init(List *aux, char *el)
    {
      assert(aux != NULL);
      strcpy(aux->el, el);
      aux->next = NULL;
    }
    
    void add(List *aux, char *el)
    {
      assert(aux != NULL);
      while(aux->next != NULL)
       aux = aux->next;
    
      List *tmp;
    
      strcpy(tmp->el, el);
      tmp->next = NULL;
    
      aux->next = tmp;
    }

    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  3. #3
    non riesco a capire il code che hai postato

    nel mio prg sono in un ciclo while dove mi spara a schermo un serie di S,

    queste S le voglio inserire in una lista ?!?

    come faccio a creare una lista composta da n elementi s?

    GRAZIE

    CIAO

  4. #4
    la lista è per definizione un ADT dinamico che sopperisce alla dichiarazione statica degli array(cioe puoi, fino a quando hai memoria per il prog, inserire elementi nella lista senza dichiararne la dimensione a compil-time).


    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  5. #5
    Mi riferivo al codice che hai postato ...

    il codice definisce solo la lista?

    in quale riga inserisco la mia S nella lista?

    Potresti aggiungere qualche commenitino

    GRAZIE MOLTO

    CIAO

  6. #6
    Non capisco l'inizio

    cosa intenti per *aux e *el?

  7. #7
    codice:
    typedef struct List List;
    
    void init(List *aux, char *el);
    void add(List *aux, char *el);
    
    struct List
    {
      char *el; // l'elmento
      List *next; // il prossimo elemento della lista
    };
    // inizializza la lista
    void init(List *aux, char *el)
    {
      assert(aux != NULL);
      strcpy(aux->el, el);
      aux->next = NULL;
    }
    // aggiunge un elemento
    void add(List *aux, char *el)
    {
      assert(aux != NULL);
      while(aux->next != NULL)
       aux = aux->next;
    
      List *tmp;
    
      strcpy(tmp->el, el);
      tmp->next = NULL;
    
      aux->next = tmp;
    }
    // .....
    // siamo nell'ipotetico main()
    //...
    
    List* head;
    init(head);
    //...
    //nel tuo while
    while(fgets(s,254,FileIn))
      
            {                
                 if (strstr (s, par) != NULL)
                       {
                            add(head, s);
                            y++;
                       
                       }
                 else 
                       
                            x++;
            }
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  8. #8
    codice:
    List* head;
     init(head);
    mi da errore di dichiarazione variabile o qualcosa del genere ...

    Attendo tue notizie

    Grazie

    CIAO

  9. #9
    codice:
    #include <stdio.h>
    #include <string.h>
    #include <assert.h> 
    #include <malloc.h> 
    
    struct List
    {
      char *el;
      struct List *next;
      struct List *prev;
    };
    
    void init(struct List *aux, char *el);
    void add(struct List *aux, char *el);
    
    void init(struct List *aux, char *el)
    {
    	assert(aux != NULL);
      strcpy(aux->el, el);
      aux->next = NULL;
      aux->prev = NULL;
    }
    
    void add(struct List *aux, char *el)
    {
    	assert(aux != NULL);
      while(aux->next != NULL)
       aux = aux->next;
    
      struct List *tmp = (struct List *)malloc(sizeof(struct List));
    
      strcpy(tmp->el, el);
      tmp->next = NULL;
      tmp->prev = aux;
    
      aux->next = tmp;
    }
    
    int main(int argc, char *argv[])
    {
    	struct List *head = (struct List *)malloc (sizeof(struct List));
    	init(head, "Luca");
    	add(head, "Lucas");
    	while(head->prev != NULL)
    	{
    		printf("%s", head->el);
    		head = head->next;
    	}
    	char c;
    	scanf("%s", &c);
      return 0;
    }
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  10. #10
    Utente di HTML.it L'avatar di anx721
    Registrato dal
    Apr 2003
    Messaggi
    2,352
    Luc@s, il tuo codice contiene un 'errore importante, overo il fatto che non allochi la memoria per il campo 'el' della struttura che deve contenere la stringa, ecco il codice corretto:


    codice:
    #include <stdio.h>
    #include <string.h>
    #include <assert.h> 
    #include <malloc.h> 
    
    struct List
    {
      char *el;
      struct List *next;
      struct List *prev;
    };
    
    void init(struct List *aux, char *el);
    void add(struct List *aux, char *el);
    
    void init(struct List *aux, char *el)
    {
      assert(aux != NULL);
      //Bisogna allocare la memoria prima di effettuare la copiatura
      aux -> el = (char *)malloc(100 * sizeof(char));
      strcpy(aux->el, el);
      aux->next = NULL;
      aux->prev = NULL;
    }
    
    void add(struct List *aux, char *el)
    {
      assert(aux != NULL);
      while(aux->next != NULL)
        aux = aux->next;
      struct List *tmp = (struct List *)malloc(sizeof(struct List));
      //Bisogna allocare la memoria prima di effettuare la copiatura
      tmp -> el = (char *)malloc(100 * sizeof(char));
      strcpy(tmp->el, el);
      tmp->next = NULL;
      tmp->prev = aux;
    
      aux->next = tmp;
    }
    
    int main(int argc, char *argv[])
    {
    	struct List *head = (struct List *)malloc (sizeof(struct List));
    	init(head, "Luca1");
    	add(head, "Luca2");
    	add(head, "Luca3");
    	while(head != NULL)
    	{
    		printf("%s\n", head->el);
    		head = head->next;
    	}
    	char c[10];
    	scanf("%s", c);
      return 0;
    }

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.