/ Published in: C++
                    
                                        
implementacija liste pomoću pokazivaÄa
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
#include<iostream>
#include<string>
using namespace std;
//#include "lista_pokazivaci.h"
#include "lista_polje.h"
bool prazno( list* L){
return FirstL(L) == EndL(L);
}
int Novi_unos(list *L){
int pocetno=1;
int sifra = 0;
if(prazno(L)) sifra = pocetno;
else sifra = RetrieveL(PreviousL(EndL(L),L),L).sifra+1;
zivotinje unos;
cout <<"Sifra: " << sifra << endl;
unos.sifra = sifra;
cout << "Naziv: "; cin>>unos.naziv;
cout << "Vrsta: "; cin>>unos.vrsta;
cout << "Cijena: "; cin >> unos.cijena;
if(unos.cijena < 0) return 0;
cout << "datum dostave)" << endl;
cout << "Dan: "; cin >> unos.datum.dan;
if(unos.datum.dan<1 || unos.datum.dan>31) return 0;
cout << "Mjesec: "; cin >> unos.datum.mj;
if(unos.datum.mj<1 || unos.datum.mj>12) return 0;
cout << "Godina: "; cin >> unos.datum.god;
if(unos.datum.god<1980) return 0;
InsertL(unos,EndL(L),L);
return 1;
}
void ispis(list *L){
elem end = EndL(L);
while(end != FirstL(L)){
zivotinje ispis = RetrieveL(PreviousL(end,L),L);
cout << "Sifra: " << ispis.sifra << endl;
cout << "Naziv: " << ispis.naziv << endl;
cout << "Vrsta: " << ispis.vrsta << endl;
cout << "Cijena: " << ispis.cijena << endl;
cout << "datum dostave: " << ispis.datum.dan<<"." <<ispis.datum.mj << "."
<<ispis.datum.god<<"."<<endl;
cout <<endl;
end = PreviousL(end,L);
}
}
void obrisi_prije( list* L){
if(prazno(L)){
cout << endl << "Nema životinja unesenih poslje" << endl;
return;
}
int broj = 0;
elem trenutno = FirstL(L);
struct{
int dan,mj,god;
}dost;
while(trenutno != EndL(L)){
zivotinje prem = RetrieveL(trenutno,L);
dost.dan = prem.datum.dan;
dost.mj = prem.datum.mj;
dost.god = prem.datum.god;
if((dost.god>2012) ||
((dost.god==2012)&&(dost.mj>9)) ||
((dost.god==2012)&&(dost.mj==9)) && (dost.dan>23)){
broj++;
cout << "Sifra: " << prem.sifra << endl;
cout << "Naziv: " << prem.naziv << endl;
cout << "Vrsta: " << prem.vrsta << endl;
cout << "Cijena: " << prem.cijena << endl;
cout << "datum dostave: " << prem.datum.dan<<"."
<<prem.datum.mj << "."
<<prem.datum.god<<"."<<endl;
cout << endl;
}
trenutno = NextL(trenutno,L);
}
cout << endl << "Broj pronadjenih zivotinja: " << broj << endl;
}
int Brisanje_naziv(list* L){
char naziv[30];
cin.ignore();
cout << "Unesite naziv: "; cin>>naziv;
if(prazno(L)) return 0;
elem trenutno = FirstL(L);
bool postoji = false;
while(trenutno != EndL(L)){
zivotinje ziv = RetrieveL(trenutno,L);
if(strcmp(ziv.naziv,naziv)==0){
postoji = true;
break;
}
trenutno = NextL(trenutno,L);
}
if(postoji){
DeleteL(trenutno,L);
}
else
return 0;
return 1;
}
int Brisanje_vrsta(list* L){
char vrsta[30];
cin.ignore();
cout << "Upisite vrstu: ";
cin>>vrsta;
elem trenutno = FirstL(L);
bool postoji = false;
while(trenutno != EndL(L)){
zivotinje ziv = RetrieveL(trenutno,L);
if(strcmp(ziv.vrsta,vrsta)==0){
postoji = true;
DeleteL(trenutno,L);
}
trenutno = NextL(trenutno,L);
}
if(!postoji)
return 0;
return 1;
}
void spajanje(zivotinje polje[], int prvi, int sredina, int drugi){
int i=prvi;
int j=sredina+1;
int k=0;
zivotinje *b=new zivotinje[drugi-prvi+1];
while(i<=sredina && j<=drugi){
if(polje[i].cijena>polje[j].cijena) b[k++]=polje[i++];
else if(polje[i].cijena<polje[j].cijena) b[k++]=polje[j++];
if(polje[i].cijena==polje[j].cijena){
if(strcmp(polje[i].naziv,polje[j].naziv)==1)
b[k++]=polje[i++];
else
b[k++]=polje[j++];
}
}
while(j<=drugi) b[k++]=polje[j++];
while(i<=sredina) b[k++]=polje[i++];
for(int c=0;c<=drugi-prvi;c++) polje[c+prvi]=b[c];
delete []b;
}
void Msort(zivotinje polje[], int prvi, int drugi){
if(prvi<drugi){
int sredina=(prvi+drugi)/2;
Msort(polje,prvi,sredina);
Msort(polje,sredina+1,drugi);
spajanje(polje,prvi,sredina,drugi);
}
}
void Msort(zivotinje polje[],int vel){
Msort(polje,0,vel-1);
}
void p6(list *gl){
if(FirstL(gl)!=EndL(gl)){
int broj=0;
elem poz=FirstL(gl);
while(false==(!true)){
broj++;
poz=NextL(poz,gl);
if(poz==EndL(gl))
break;
}
poz=FirstL(gl);
zivotinje *sort=new zivotinje[broj];
int mj=0;
while(false==(!true)){
sort[mj]=RetrieveL(poz,gl);
mj++;
poz=NextL(poz,gl);
if(poz==EndL(gl))
break;
}
Msort(sort,broj);
for(int i=0;i<broj;i++){
cout<<endl<<"Sifra: "<<sort[i].sifra;
cout<<endl<<"Cijena: "<<sort[i].cijena;
cout<<endl<<"Naziv: "<<sort[i].naziv;
cout<<endl<<"Vrsta: "<<sort[i].vrsta;
cout<<endl<<"datum dostave: "
<<sort[i].datum.dan<<"."
<<sort[i].datum.mj<<"."
<<sort[i].datum.god<<"."<<endl;
}
delete []sort;
}
else
cout<<endl<<"Prazna lista!"<<endl;
}
int main(){
list* L = NULL;
L = InitL(L);
int odabir = 0;
do{
cout << endl;
cout << "1. Dodaj zapis" << endl;
cout << "2. Ispisi listu" << endl;
cout << "3. Ispisi zivotinje dostavljene poslje 23. rujna 2012" << endl;
cout << "4. Brisi prema nazivu zivotinje" << endl;
cout << "5. Brisi prema vrsti zivotinje" << endl;
cout << "6. Silazno sortiranje" << endl;
cout << "9. Izlaz" << endl;
cout << "-----------------------------" << endl;
cin >> odabir;
system("cls");
switch(odabir){
case 1:
if(!Novi_unos(L)) cout << "Pogresno uneseni zivotinje!" << endl;
break;
case 2:
ispis(L);
break;
case 3:
obrisi_prije(L);
break;
case 4:
if(Brisanje_naziv(L)) cout << "Zivotinja obrisana." << endl;
else cout << "Pogresni naziv." << endl;
break;
case 5:
if(Brisanje_vrsta(L)) cout << "Vrsta obrisana!" << endl;
else cout << "Pogresna vrsta!" << endl;
break;
case 6:
p6(L);
break;
case 9:
break;
default:
cout << "Pogrešan unos!" << endl;
}
}while(odabir != 9);
DeleteAllL(L);
system("pause");
return 0;
}
URL: lista
Comments
 Subscribe to comments
                    Subscribe to comments
                
                