PHP:
#include<stdio.h>
#include<stdlib.h>
#include "stack.h"
struct nodo{
int dato;
int h;
struct nodo *next;
};
int cont,h;
typedef struct nodo nodo;
nodo *testa = NULL;
nodo *tmp = NULL;
void push( int n){
struct nodo *nuovo;
nuovo = malloc(sizeof(struct nodo));
if(nuovo == NULL) exit(0);
nuovo -> dato = n;
nuovo -> next = testa;
testa = nuovo;
}
int pop(void){
tmp = testa;
h = tmp->dato;
testa = testa->next;
return h;
}
int top(void){
return testa->dato;
}
int is_empty(void){
if(testa == NULL) return 1;
else return 0;
}
void make_empty(void){
while(is_empty()== 1)
pop();
}
void stampa(void){
tmp=testa;
while(tmp !=NULL){
printf("| %d |\n",tmp->dato);
printf("|---|\n");
tmp=tmp->next;
}
}
PHP:
#include<stdio.h>
#include<stdlib.h>
#include "stack.h"
int main (){
char c;
int n,tmp;
while( ( c = getchar ()) != 'f' ){
switch(c){
case '1':
scanf("%d",&n);
push(n);
printf("inserito: %d",n);
break;
case '2':
tmp = pop();
printf("tolto: %d",tmp);
break;
case '3':
tmp = top();
printf("in testa: %d",tmp);
break;
case '4':
tmp = is_empty();
if(tmp==0)
printf("pila vuota");
else printf("pila non vuota");
break;
case '5':
make_empty();
printf("pila svuotata");
break;
case '6': stampa();
break;
} // end switch
}
return 0;
}