tyrion

listaprof.c

Dec 14th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define FALSE 0
  4. #define TRUE 1
  5.  
  6. typedef int tipoelem;
  7.  
  8. typedef struct elem {
  9.     tipoelem info;
  10.     struct elem* next;
  11. } elist;
  12.  
  13. typedef elist *plist;
  14.  
  15. // restituisce la lista vuota
  16. // XXX mi sembra del tutto inutile ...
  17. plist init() {
  18.     plist temp;
  19.     temp = NULL;
  20.     return temp;
  21. }
  22.  
  23. // verifica se una lista è vuota
  24. int is_empty(plist list) {
  25.     if (!list) return TRUE;
  26.     else return FALSE;
  27. }
  28.  
  29. // aggiunge un elemento in testa alla lista e restituisce la lista risultato.
  30. plist cons(plist list, tipoelem info) {
  31.     plist temp;
  32.     temp = (plist)malloc(sizeof(elist));
  33.     temp->info = info;
  34.     temp->next = list;
  35.     return temp;
  36. }
  37.  
  38. // restituisce il primo elemento della lista
  39. tipoelem car(plist list) {
  40.     return list->info;
  41. }
  42.  
  43. // toglie il primo elemento della lista e restituisce la lista risultato.
  44. plist cdr(plist list) {
  45.     plist temp;
  46.     if (is_empty(list)) return list;
  47.     temp = list;
  48.     list = list->next;
  49.     free(temp);
  50.     return list;
  51. }
  52.  
  53. void stampa(plist list) {
  54.     printf("(");
  55.     while (list->next) {
  56.         printf("%d, ", list->info);
  57.         list = list->next;
  58.     }
  59.     printf("%d)\n", list->info);
  60. }
  61.  
  62. void main() {
  63.     int scelta;
  64.     int continua = TRUE;
  65.     tipoelem info;
  66.     plist list;
  67.     list = init();
  68.  
  69.     while (continua) {
  70.         // printf("--------Menu--------\n");
  71.         printf("1) Cons, ");
  72.         printf("2) Car, ");
  73.         printf("3) Cdr, ");
  74.         printf("4) Is Empty, ");
  75.         printf("5) Stampa, ");
  76.         printf("6) Esci\n");
  77.         printf("Scelta: ");
  78.         scanf("%d", &scelta);
  79.        
  80.         switch (scelta) {
  81.             case 1:
  82.                 printf("Elemento: ");
  83.                 scanf("%d", &info);
  84.                 list = cons(list, info);
  85.                 break;
  86.             case 2:
  87.                 if (is_empty(list)) printf("Vuota!\n");
  88.                 else printf("1° elemento: %d\n", car(list));
  89.                 break;
  90.             case 3:
  91.                 list = cdr(list);
  92.                 break;
  93.             case 4:
  94.                 if (is_empty(list)) printf("Vuota!\n");
  95.                 else printf("Piena!\n");
  96.                 break;
  97.             case 5:
  98.                 stampa(list);
  99.                 break;
  100.             case 6:
  101.                 continua = FALSE;
  102.                 break;
  103.         }
  104.     }
  105. }
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment