E' che mi sembrava brutto scrivere direttamente il codice....e quindi ho provato a scriverlo a parole....cmq...ecco il codice...con 3 file..: specifica, implementazione,utente!
codice:
//File di specifica
#ifndef CODA_H
#define CODA_H
typedef struct _ESAME{
int codice;
const char * descrizione;
struct _ESAME *next;
}ESAME;
class Coda{
public:
Coda();
bool clear();
bool empty();
void push(const ESAME el);
bool pop(ESAME& el);
bool top(ESAME& el);
//Coda(const Coda& codaToCopy);
void stampa();
private:
ESAME* createEsame(int codice,const char* descrizione);
ESAME *first, *last;
};
#endif //CODA_H
codice:
//File di implementazione
#include "Coda.h"
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
using namespace std;
Coda::Coda(){
last=first=0;
}
//Coda::Coda (const Coda& codaToCopy){
bool Coda::clear(){
first=0;
ESAME *node=last;
last=0;
ESAME *next;
while(node!=0){
next= node->next;
delete node;
node=next;
}
}
bool Coda::empty(){
return last==0;
}
void Coda::push(const ESAME el){
ESAME* nuovorecord= createEsame(el.codice,el.descrizione);
if(empty()){
last=nuovorecord;
first=nuovorecord;
}
else{
nuovorecord->next=last;
last=nuovorecord;
}
}
void Coda::stampa(){
if(!empty()){
ESAME *temp=last;
while(temp){
cout << "\n";
cout<< temp->codice;
cout<< "\n";
cout<< temp->descrizione;
cout<< "\n";
temp=temp->next;
}
}
else
cout<<"\n"<<"La lista è vuota"<<"\n";
}
bool Coda::pop(ESAME& el){
if(!empty()){
if(last=&el){
ESAME* temp=last->next;
delete last;
last=temp;
}
else{
ESAME* temp1=last;
while(temp1->next!=&el){
temp1=temp1->next;
}
ESAME* temp2=⪙
temp1->next=temp2->next;
temp2->next=0;
delete temp2;
}
return true;
}
else
return false;
}
bool Coda::top(ESAME &el){
if(!empty()){
ESAME* temp=last;
while(temp->next){
temp=temp->next;
}
el.codice=temp->codice;
el.descrizione=temp->descrizione;
}
else
return false;
}
ESAME* Coda::createEsame(int cod,const char* desc){
ESAME* nuovoesame = new ESAME;
nuovoesame->codice = cod;
nuovoesame->descrizione = desc;
nuovoesame->next=0;
return nuovoesame;
}
codice:
//File di utilizzo
#include <iostream>
#include <stdlib.h>
#include "Coda.h"
#include <string>
#include <cstring>
using namespace std;
int main(){
Coda p1;
ESAME str;
str.codice=4081;
str.descrizione="Analisi I";
p1.push(str);
str.codice=2000;
str.descrizione="Analisi II";
p1.push(str);
str.codice=3928;
str.descrizione="Fisica";
p1.push(str);
str.codice=1011;
str.descrizione="Geometria";
p1.push(str);
p1.stampa();
p1.top(str);
cout<<"\n" << "Elemento di testa" <<"\n" << str.codice <<"\n"<< str.descrizione<<"\n";
Coda p2;
p2=p1;
p1.clear();
p1.stampa();
cout<<"\n\nprima della stampa p2\n";
p2.stampa();
system("PAUSE");
return EXIT_SUCCESS;
}
Grazie!!!