microinfo
29-05-2012, 11:17
#ifndef LISTA_H_
#define LISTA_H_
#include <iostream>
#include <assert.h>
#include <fstream>
#include <string.h>
#include "nodo.h"
#include "bolletta.h"
#include "fisso.h"
#include "mobile.h"
using namespace std;
//classe lista
template<class T>
class lista{
public:
lista(); //costruttore
lista(char*);
~lista(); //distruttore
bool isempty();
void print();
void instop(T &);
void insord(T &);
bool deltop(T &);
void elimina();
bool ric(char *);
private:
nodo<T> *firstptr;
nodo<T> *lastptr;
nodo<T> *getnew(T &);//alloca un nuovo nodo
};
//definizioni per la classe lista
template<class T>
lista<T>::lista():firstptr(0),lastptr(0){}
/*template<class T>
lista<T>::lista(char* nfile){
lista();
ifstream file;
file.open(nfile,ios::in|ios::binary);
if(file){
bolletta currentptr;
while(!file.eof()){
file>>currentptr;
instop(currentptr);
}
cout<<"fine";
file.close();
}
}*/
template<class T>
lista<T>::~lista(){
cout<<"Elinimo tutto: \n";
nodo<T> *currentptr=firstptr, *tempptr;
while (currentptr!=0){
tempptr=currentptr;
cout<<tempptr->data<<"\n";
currentptr=currentptr->nextptr;
delete tempptr;}
}
template<class T>
bool lista<T>::isempty(){ //ritorna true se la lista è vuota
return firstptr==0;
}
template<class T>
void lista<T>::instop(T &val){
nodo<T> *newptr=getnew(val);
if(isempty())
firstptr=lastptr=newptr;
else{
newptr->nextptr=firstptr;
firstptr=newptr;
}
}
template<class T>
void lista<T>::insord(T &val){
nodo<T> *newptr=getnew(val);
nodo<T> *p = firstptr;
nodo<T> *prec = firstptr;
if(isempty())
firstptr=lastptr=newptr;
else{
//ordinamento crescente per codice (di tipo stringa)
while ((p!=NULL)&&(strcmp(p->data->getcod(),newptr->data->getcod())>0))
//while ((p!=0)&&((p->data->getnumtel())>(newptr->data->getnumtel())))
{
prec = p;
p=p->nextptr;
}
if (p!=NULL)
{
if (strcmp(p->data->getcod(),newptr->data->getcod())==0)
// if ((p->data->getnumtel())==(newptr->data->getnumtel()))
throw ecc();
prec->nextptr = newptr;
newptr->nextptr = p;
}
else
{
prec->nextptr = newptr;
newptr->nextptr = NULL;
lastptr = newptr;
}
}
}
template<class T>
nodo<T> *lista<T>::getnew(T &val){
nodo<T> *ptr=new nodo<T>(val);
assert(ptr!=0);
return ptr;
}
template<class T>
void lista<T>::print(){
nodo<T> *currentptr=firstptr;
while(currentptr!=0){
//cout<<currentptr->data<<" ";
(currentptr->data)->datiutenza();//avvia la funziona datiutenza della classe al puntatore currentptr
currentptr=currentptr->nextptr;
cout<<endl;
}
cout<<"\n";
}
template<class T>
bool lista<T>::deltop(T &val){
nodo<T> *tempptr=firstptr;
firstptr=firstptr->nextptr;
val=tempptr->data;
delete tempptr;
return true;
}
template <class T>
bool lista<T>::ric(char * val){
nodo<T>* temptr = firstptr;
while (temptr) {
//if(temptr->data->getnumtel()==val)
if(strcmp(temptr->data->getcod(),val)==0)
return true;
temptr = temptr->nextptr;
}
return false;
}
#endif /* LISTA_H_ */
perchè la funzione inserisci ordinato va in loop quando eseguo l'iseriemnto di 3 4 oggetti?
lista<bolletta*>* l = new lista<bolletta*>;
fisso f("f",0.05,40,200.1,"via roma");
fisso f1("a",0.02,34,302.2,"via napoli");
mobile m("d",0.05,100,350,0.5);
mobile m1("e",0.05,20,100,0.5);
bolletta *fptr=&f ,*f1ptr=&f1, *mptr=&m, *m1ptr=&m1;
l->insord(fptr);
l->insord(m1ptr);
l->insord(f1ptr);
l->insord(mptr);
l->print();
#define LISTA_H_
#include <iostream>
#include <assert.h>
#include <fstream>
#include <string.h>
#include "nodo.h"
#include "bolletta.h"
#include "fisso.h"
#include "mobile.h"
using namespace std;
//classe lista
template<class T>
class lista{
public:
lista(); //costruttore
lista(char*);
~lista(); //distruttore
bool isempty();
void print();
void instop(T &);
void insord(T &);
bool deltop(T &);
void elimina();
bool ric(char *);
private:
nodo<T> *firstptr;
nodo<T> *lastptr;
nodo<T> *getnew(T &);//alloca un nuovo nodo
};
//definizioni per la classe lista
template<class T>
lista<T>::lista():firstptr(0),lastptr(0){}
/*template<class T>
lista<T>::lista(char* nfile){
lista();
ifstream file;
file.open(nfile,ios::in|ios::binary);
if(file){
bolletta currentptr;
while(!file.eof()){
file>>currentptr;
instop(currentptr);
}
cout<<"fine";
file.close();
}
}*/
template<class T>
lista<T>::~lista(){
cout<<"Elinimo tutto: \n";
nodo<T> *currentptr=firstptr, *tempptr;
while (currentptr!=0){
tempptr=currentptr;
cout<<tempptr->data<<"\n";
currentptr=currentptr->nextptr;
delete tempptr;}
}
template<class T>
bool lista<T>::isempty(){ //ritorna true se la lista è vuota
return firstptr==0;
}
template<class T>
void lista<T>::instop(T &val){
nodo<T> *newptr=getnew(val);
if(isempty())
firstptr=lastptr=newptr;
else{
newptr->nextptr=firstptr;
firstptr=newptr;
}
}
template<class T>
void lista<T>::insord(T &val){
nodo<T> *newptr=getnew(val);
nodo<T> *p = firstptr;
nodo<T> *prec = firstptr;
if(isempty())
firstptr=lastptr=newptr;
else{
//ordinamento crescente per codice (di tipo stringa)
while ((p!=NULL)&&(strcmp(p->data->getcod(),newptr->data->getcod())>0))
//while ((p!=0)&&((p->data->getnumtel())>(newptr->data->getnumtel())))
{
prec = p;
p=p->nextptr;
}
if (p!=NULL)
{
if (strcmp(p->data->getcod(),newptr->data->getcod())==0)
// if ((p->data->getnumtel())==(newptr->data->getnumtel()))
throw ecc();
prec->nextptr = newptr;
newptr->nextptr = p;
}
else
{
prec->nextptr = newptr;
newptr->nextptr = NULL;
lastptr = newptr;
}
}
}
template<class T>
nodo<T> *lista<T>::getnew(T &val){
nodo<T> *ptr=new nodo<T>(val);
assert(ptr!=0);
return ptr;
}
template<class T>
void lista<T>::print(){
nodo<T> *currentptr=firstptr;
while(currentptr!=0){
//cout<<currentptr->data<<" ";
(currentptr->data)->datiutenza();//avvia la funziona datiutenza della classe al puntatore currentptr
currentptr=currentptr->nextptr;
cout<<endl;
}
cout<<"\n";
}
template<class T>
bool lista<T>::deltop(T &val){
nodo<T> *tempptr=firstptr;
firstptr=firstptr->nextptr;
val=tempptr->data;
delete tempptr;
return true;
}
template <class T>
bool lista<T>::ric(char * val){
nodo<T>* temptr = firstptr;
while (temptr) {
//if(temptr->data->getnumtel()==val)
if(strcmp(temptr->data->getcod(),val)==0)
return true;
temptr = temptr->nextptr;
}
return false;
}
#endif /* LISTA_H_ */
perchè la funzione inserisci ordinato va in loop quando eseguo l'iseriemnto di 3 4 oggetti?
lista<bolletta*>* l = new lista<bolletta*>;
fisso f("f",0.05,40,200.1,"via roma");
fisso f1("a",0.02,34,302.2,"via napoli");
mobile m("d",0.05,100,350,0.5);
mobile m1("e",0.05,20,100,0.5);
bolletta *fptr=&f ,*f1ptr=&f1, *mptr=&m, *m1ptr=&m1;
l->insord(fptr);
l->insord(m1ptr);
l->insord(f1ptr);
l->insord(mptr);
l->print();