Les agradecería infinitamente si me ayudan a resolver este problema! Igual si encuentro la solución les aviso! Muchas Gracias de antemano!
Código:
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
struct datos {
string TITLE;
string ARTIST;
string COUNTRY;
string COMPANY;
float PRICE;
int YEAR;
};
struct node {
datos data;
node *left;
node *right;
};
string getTagName(string str);
string getTagContent(string str);
node * newList();
node * insert_right(node *list, datos nuevo_nodeo);
void printList(node* list);
void readXML(node *head);
void sortList(node **lista, int rule, bool dsc) ;
int main() {
node *head = newList();
node *current = head;
//LEYENDO MI ARCHIVO XML
readXML(head);
cout << "IMPRIMIENDO LISTA" << endl << endl;
printList(head);
// Todo funciona hasta que le incorpora la parte para ordenar y la aplicacion deja de funcionar en ese momento!!
sortList( &head, 1, true ); // Ordenando por precio, pero no me funciona y no encuentro el error!
return 0;
}
node * newList() {
node *nuevo;
nuevo = new node;
nuevo->data.ARTIST=-1;
nuevo->data.COMPANY=-1;
nuevo->data.COUNTRY=-1;
nuevo->data.PRICE=-1;
nuevo->data.TITLE=-1;
nuevo->data.YEAR=-1;
nuevo->right = nuevo;
nuevo->left = nuevo;
return nuevo;
}
string getTagName(string str) {
int posInit, posFin;
int delta;
posInit = str.find("<");
posFin = str.find(">");
delta = posFin - posInit;
return str.substr(posInit + 1, delta - 1);
}
string getTagContent(string str) {
int posInit, posFin;
int delta;
posInit = str.find(">");
posFin = str.find("</");
delta = posFin - posInit;
return str.substr(posInit + 1, delta - 1);
}
node * insert_right(node *list, datos nuevo_nodeo) {
node *nuevo;
nuevo = new node;
nuevo->data.TITLE = nuevo_nodeo.TITLE;
nuevo->data.ARTIST = nuevo_nodeo.ARTIST;
nuevo->data.COUNTRY = nuevo_nodeo.COUNTRY;
nuevo->data.COMPANY = nuevo_nodeo.COMPANY;
nuevo->data.PRICE = nuevo_nodeo.PRICE;
nuevo->data.YEAR = nuevo_nodeo.YEAR;
nuevo->left = list;
nuevo->right = list->right;
list->right = nuevo;
nuevo->right->left = nuevo;
return nuevo;
}
void printList(node* list) {
node *head = list;
node *current = list;
while (head != (current = current->right)) {
cout << "------------------------" << endl;
cout << current->data.TITLE << endl;
cout << current->data.ARTIST << endl;
cout << current->data.COUNTRY << endl;
cout << current->data.COMPANY << endl;
cout << current->data.PRICE << endl;
cout << current->data.YEAR << endl;
}
}
void readXML(node *current) {
datos nuevo;
string fileName = "cd_catalog.XML";
string line;
string tagName;
int posInit, posFin;
int delta;
int ban = 0;
cout << "-----archivo XML->" << fileName << "--------" << endl << endl << endl;
ifstream myFile((char*) fileName.c_str());
while (getline(myFile, line, '\n')) {
tagName = getTagName(line);
if (ban == 0) {
datos nuevo; //Para acceder a mi estructura facilmente!
}
if (tagName.compare("TITLE") == 0) {
nuevo.TITLE = getTagContent(line);
ban++;
} else if (tagName.compare("ARTIST") == 0) {
nuevo.ARTIST = getTagContent(line);
ban++;
} else if (tagName.compare("COUNTRY") == 0) {
nuevo.COUNTRY = getTagContent(line);
ban++;
} else if (tagName.compare("COMPANY") == 0) {
nuevo.COMPANY = getTagContent(line);
ban++;
} else if (tagName.compare("PRICE") == 0) {
nuevo.PRICE = atof(getTagContent(line).c_str());
ban++;
} else if (tagName.compare("YEAR") == 0) {
nuevo.YEAR = atoi(getTagContent(line).c_str());
ban++;
}
if (ban == 6) {
current = insert_right(current, nuevo);
ban = 0;
}
}
myFile.close();
}
void sortList(node **lista, int rule, bool dsc) {
// rule { 1 --> Price, 2 --> year, 3 --> Artis, 4 --> title }
node *lst = *lista;
node *sig;
datos temp;
//-- Boolean para validar situación de cambio ---//
bool valid;
do {
sig = lst->right;
while (sig->right != NULL) {
switch (rule) {
case 1:
valid = dsc ? (lst->data.PRICE > sig->data.PRICE) : (lst->data.PRICE < sig->data.PRICE);
break;
case 2:
valid = dsc ? (lst->data.YEAR > sig->data.YEAR) : (lst->data.YEAR < sig->data.YEAR);
break;
case 3:
valid = dsc ? (lst->data.ARTIST > sig->data.ARTIST) : (lst->data.ARTIST < sig->data.ARTIST);
break;
case 4:
valid = dsc ? (lst->data.TITLE > sig->data.TITLE) : (lst->data.TITLE < sig->data.TITLE);
break;
}
if (valid) {
temp = lst->data;
lst->data = sig->data;
sig->data = temp;
}
sig = sig->right;
}
lst = lst->right;
} while (lst->right != NULL);
}