Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define FALSE 0
- #define TRUE 1
- typedef int tipoelem;
- typedef struct elem {
- tipoelem info;
- struct elem* next;
- } elist;
- typedef elist *plist;
- // restituisce la lista vuota
- // XXX mi sembra del tutto inutile ...
- plist init() {
- plist temp;
- temp = NULL;
- return temp;
- }
- // verifica se una lista è vuota
- int is_empty(plist list) {
- if (!list) return TRUE;
- else return FALSE;
- }
- // aggiunge un elemento in testa alla lista e restituisce la lista risultato.
- plist cons(plist list, tipoelem info) {
- plist temp;
- temp = (plist)malloc(sizeof(elist));
- temp->info = info;
- temp->next = list;
- return temp;
- }
- // restituisce il primo elemento della lista
- tipoelem car(plist list) {
- return list->info;
- }
- // toglie il primo elemento della lista e restituisce la lista risultato.
- plist cdr(plist list) {
- plist temp;
- if (is_empty(list)) return list;
- temp = list;
- list = list->next;
- free(temp);
- return list;
- }
- void stampa(plist list) {
- printf("(");
- while (list->next) {
- printf("%d, ", list->info);
- list = list->next;
- }
- printf("%d)\n", list->info);
- }
- void main() {
- int scelta;
- int continua = TRUE;
- tipoelem info;
- plist list;
- list = init();
- while (continua) {
- // printf("--------Menu--------\n");
- printf("1) Cons, ");
- printf("2) Car, ");
- printf("3) Cdr, ");
- printf("4) Is Empty, ");
- printf("5) Stampa, ");
- printf("6) Esci\n");
- printf("Scelta: ");
- scanf("%d", &scelta);
- switch (scelta) {
- case 1:
- printf("Elemento: ");
- scanf("%d", &info);
- list = cons(list, info);
- break;
- case 2:
- if (is_empty(list)) printf("Vuota!\n");
- else printf("1° elemento: %d\n", car(list));
- break;
- case 3:
- list = cdr(list);
- break;
- case 4:
- if (is_empty(list)) printf("Vuota!\n");
- else printf("Piena!\n");
- break;
- case 5:
- stampa(list);
- break;
- case 6:
- continua = FALSE;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment