All pastes #393468 Raw Edit

liste in liste

public c v1 · immutable
·published 2007-03-13 16:12 UTC
rendered paste body
#include <stdio.h>#include <string.h>#include <stdlib.h> /* adaugat pentru malloc */typedef struct snod1{        int nr_linie;        struct snod1 *leg_nrlinie;} nod1;typedef struct snod2{        char *cuvant;        struct snod2 *leg;        nod1 *a;} nod2, *list;void addL(list *lst, int i, char *word){     list nou, p;     nou=(nod2 *)malloc(sizeof(nod2));     nou->cuvant=(char *)malloc(100*sizeof(char)); /* sizeof char e mereu 1, poti scrie malloc(100) */     /* sau mai bine malloc(strlen(word)+1), ca sa aloci exact atat cat      * trebuie ;)       */     nou->a=(nod1 *)malloc(sizeof(nod1)); /* typecasting results of malloc is not necessary */     nou->a->nr_linie=i;     nou->a->leg_nrlinie=NULL;     strcpy(nou->cuvant, word);     nou->leg=NULL;     if (*lst==NULL)         *lst=nou;     else     {            p=*lst;            while (p->leg!=NULL)                  p=p->leg;            p->leg=nou;            printf("Added %s\n", nou->cuvant);      }}void printL(list lst){     list p=lst; /* uitasesi initializarea-- p era mereu NULL si nu intra in while */     nod1 *q;     while (p!=NULL)     {           printf("%s ", p->cuvant);           q=p->a;           while (q!=NULL)           {                 printf("%d ", q->nr_linie);                 q=q->leg_nrlinie;           }           printf("\n");           p=p->leg;     }}int main(){    FILE *f1;    int i=1;    char *k;    list lst=NULL;    char *linie;    linie=(char *) malloc(100*sizeof(char));      //k=(char *) malloc(100*sizeof(char));    f1=fopen("dictionar.txt", "rt");    while (fgets(linie,255,f1)!=NULL)    {               linie[strlen(linie)-1] = 0; /* eliminam \n-ul inestetic :P */          k=strtok(linie," ");          printf("Trying to add %s\n", k); /* my debug thingies */          addL(&lst,i,k);             while (k)             {                   k=strtok(NULL," ");                   if (k!=NULL) { /* ultimul apel returneaza NULL;                                      nu e prins in while, pentru ca dupa while                                     se mai face alt apel */                       printf("Trying to add %s\n", k);                        addL(&lst,i,k);                   }             }          i++; /* linia era incrementata gresit in while-ul interior */    }    printL(lst);    return 0;}