Skip to content

Instantly share code, notes, and snippets.

@aciceri
Created April 22, 2016 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aciceri/c89ed80d1d016f8916a1fb41f7e0ae2b to your computer and use it in GitHub Desktop.
Save aciceri/c89ed80d1d016f8916a1fb41f7e0ae2b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
struct pila_nodo {
char valore;
struct pila_nodo * prossimo;
};
typedef struct pila_nodo elemento;
elemento * pop(elemento * testa);
elemento * push(elemento * testa);
void visualizza(elemento * testa);
int main(void) {
char scelta;
elemento * pila = NULL;
do {
printf("---Cosa vuoi fare?---\n");
printf("1-Pop\n");
printf("2-Push\n");
printf("3-Visualizza\n");
printf("0-Esci\n");
printf(">>> ");
scanf(" %c", &scelta);
switch(scelta) {
case '1':
pila = pop(pila);
break;
case '2':
pila = push(pila);
break;
case '3':
visualizza(pila);
}
} while(scelta != '0');
return 0;
}
elemento * pop(elemento * testa) {
if (testa == NULL) {
printf("La pila e' vuota!\n");
return NULL;
}
else {
printf("%c\n", testa->valore);
testa = testa->prossimo;
return testa;
}
}
elemento * push(elemento * testa) {
char valore;
elemento * el = (elemento *) malloc(sizeof(elemento));
printf("Inserisci elemento: ");
scanf(" %c", &valore);
el->valore = valore;
el->prossimo = testa;
return el;
}
void visualizza(elemento * testa) {
if (testa == NULL)
printf("La pila e' vuota!\n");
else {
while (testa != NULL) {
printf("%c ", testa->valore);
testa = testa->prossimo;
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment