vuoi
o PayPal
tutte le volte che vuoi
Semplice programma diviso in due che svolge la simulazione delle pile e dei template che vengo utilizati molto in programmazione.Il linguaggio addotatto è il c++.
node<MyType> *head; // Puntatore ai dati
int len; // La lunghezza della Pila
public:
Stack(); // Costruttore
~Stack(); // Distruttore
MyType Read(); // Restituisce l'elemento in testa alla Pila
void Push(const MyType &d); // Aggiunge un elemento in testa alla Pila
MyType Pop(); // Rimuove l'elemento in testa alla Pila (e lo
// restituisce)
int IsEmpty() // La pila e` vuota?
{ return(len == 0); }
};
// Costruttore per il tipo di dato Pila.
template<class MyType> Stack<MyType>::Stack()
{ len = 0; // La Pila e` vuota
head = NULL; // La testa non punta da nessuna parte
}
// Distruttore per il tipo di dato Pila.
template<class MyType> Stack<MyType>::~Stack()
{ while (len) {
Pop();
}
}
// Restituisce l'elemento in testa alla Pila
template<class MyType> MyType Stack<MyType>::Read()
{ if (IsEmpty()) {
cerr << "Errore: la Pila e` vuota!" << endl;
exit(-2);
}
return(head->data);
}
// Rimuove un elemento dalla testa della Pila
template<class MyType> MyType Stack<MyType>::Pop()
{ MyType tmp;
if (IsEmpty()) {
cerr << "Errore: la Pila e` vuota!" << endl;
exit(-2);
}
else {
tmp = head->data; // Copio il valore sulla testa della Pila
node<MyType> *tmpp = head; // Mi salvo il puntatore alla testa della
Pila head = head->next; // Sposto la testa della Pila
delete tmpp; // Cancello la vecchia testa della Pila
len--;
}
return(tmp); // Restituisco la vecchia testa della Pila