Salve a tutti, volevo chiedervi un parere su un codice che ho scritto che dovrebbe inserire degli elementi in una lista ordinata. Vi spiego brevemente il problema: in partenza ho una lista ordinata di smartphone in ordine alfabetico per produttore (es. Apple, Samsung...) e ogni produttore è ordinato in ordine decrescente per prezzo del cellulare. Devo scrivere una funzione che gestisce l'inserimento di nuovi dati. (Non posso usare il tipo string, ma solo array di caratteri.)
CODICE:
#include <iostream>
#include <cstring>
using namespace std
struct smartphone{
char modello[30];
char produttore[30];
int prezzo;
smartphone *next;
};
typedef smartphone *ptr_smart;
int minorprezzo(ptr_smart p, int costo){
int numero=0;
char prod[30]='\0';
while (p!=NULL) {
if ((!strcmp(prod, p->produttore)) && (p->prezzo<costo)){
numero=numero+1;
strcpy(prod,p->produttore);}
p=p->next;
}
return (numero);
}
void aggiungi(ptr_smart p, char mod[30], char prod[30], int prez){
ptr_smart head=p;
ptr_smart p_old;
ptr_smart tmp=p;
while (p!=NULL) {
if ((strcmp(prod, p->produttore) && (prez<p->prezzo) && (prez>(p->next)->prezzo) && (strcmp(prod, (p->next)->produttore)) {
tmp=p;
p = new smartphone;
strcpy(p->modello, mod);
strcpy(p->produttore, prod);
p->prezzo=prez;
p->next=tmp->next;
tmp->next=p;
}
else if ((strcmp(prod, head->produttore) && (head->prezzo<prez)) {
tmp=head;
head = new smartphone;
strcpy(head->modello, mod);
strcpy(head->produttore, prod);
head->prezzo=prez;
head->next=tmp;
}
else if ((strcmp(prod, p->produttore) && (!strcmp(prod, p_old->produttore) && (prez>p->prezzo)){
tmp=p;
p = new smartphone;
strcpy(p->modello, mod);
strcpy(p->produttore, prod);
p->prezzo=prez;
p_old->next=p;
p->next=tmp;
}
else if ((strcmp(prod, p->produttore) && (!strcmp(prod, (p->next)->produttore) && (prez<p->prezzo)) {
tmp=p;
p = new smartphone;
strcpy(p->modello, mod);
strcpy(p->produttore, prod);
p->prezzo=prez;
p->next=tmp->next;
tmp->next=p;
}
else if ((strcmp(prod, p->produttore) && (p->next==NULL) && (prez<p->prezzo)){
tmp=p;
p = new smartphone;
strcpy(p->modello, mod);
strcpy(p->produttore, prod);
p->prezzo=prez;
p->next=NULL;
tmp->next=p;
}
p_old=p;
p=p->next;
}
}
/////////////////////////////////////////////////////////////////////////////
Grazie in anticipo per l'aiuto.