/***************************************

            Elenchi concatenati
           last update 10/07/87
      AMIGA-Version by Frank Kremser
PC-Original-Version by Dr. Edgar Huckert
       (C) 1987  by Markt & Technik

****************************************

Programma di dimostrazione per elenchi concatenati

*************************************************/

#include <stdio.h>

typedef struct kette
{
  char *pzeile;
  struct kette *next;
} KETTE;
KETTE *start,*zuletzt;

extern char *malloc(),*free();

/* Posiziona un input nell'elenco concatenato */
/* imposta start e zuletzt                    */
void mache_eintrag(str)
char *str;
{
  KETTE *pstruct;
  char *cadr;
  /* preparazione di una matrice con dimensione esatta per str */
  cadr = malloc(strlen(str)+1);
  strcpy(cadr,str);
  /* quindi allocazione della struttura */
  pstruct =(struct KETTE *)malloc(sizeof(KETTE));
  /* Puntatore al contenuto */
  pstruct->pzeile = cadr;
  /* Puntatore al successivo */
  pstruct->next = NULL;
  if (start != NULL)
    zuletzt->next = pstruct;
  else start = pstruct;
  zuletzt = pstruct;
}   /* end mache_eintrag */

void main()
{
  char zeile[100];
  KETTE *hadr;

  /* Posizionam. - Fine = CR */
  start = zuletzt = NULL;
  while (1)
    {
     printf("\nProssima riga:");
     gets(zeile);
     if (strlen(zeile) == 0) break;
     mache_eintrag(zeile);
    }   /* while 1 */

  /* Ricerca - Fine = CR */
  while (1)
    {
     printf("\nStringa di ricerca:");
     gets(zeile);
     if (strlen(zeile) == 0) break;
     hadr = start;
     while (hadr != NULL)
       {
        if (strcmp(hadr->pzeile,zeile) == 0) break;
        hadr = hadr->next;
       }   /* while hadr != NULL */
     if (hadr != NULL) printf("\t\t\t --- Trovata");
     else              printf("\t\t\t --- non trovata");
    }   /* while 1 */

  /* Non e' assolutamente necessario liberare */
  /* lo spazio allocato , ma e' bene farlo    */
  hadr = start;
  while (hadr != NULL)
    {
     zuletzt = hadr->next;
     free(hadr->pzeile);
     free(hadr);
     hadr = zuletzt;
    }
}   /* end main */
