/* Solo tengo un error
:mad: HELP! HELP! HELP! .
Alguien que me ayude con eso.
*/
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
using namespace std;
struct datos {
int num;
};
struct node {
datos data;
node *left;
node *right;
};
node *newList();
void printListAsc(node *current);
void printNode(node *current);
int size(node * list);
int generador(int min, int max);
void aniadir_ordenador(node **lista, node data);
int main() {
node *head = newList();
node *current = head;
node myData;
for (int i = 0; i < 100; i++) {
myData.data.num = generador(1, 1000);
aniadir_ordenador(current, myData); /* SOLO TENGO UN ERROR AQUI */
}
printListAsc(head);
return 0;
}
int size(node * list) {
node *head = list;
node *current = list;
int cont = 0;
while (head != (current = current->right)) {
cont = cont + 1;
}
return cont;
}
void aniadir_ordenador(node **lista, node data) {
node *aux = NULL;
node *ant = NULL;
node *nuevo_dato = NULL;
nuevo_dato = new node;
nuevo_dato->right = NULL;
nuevo_dato->left = NULL;
if (*lista == NULL) { //Si la lista esta vacia.
*lista = nuevo_dato;
}//De lo contrario agrego el nodo ordenado.
aux = *lista;
while (aux != NULL) {
ant = aux;
aux = aux->right;
}
if (ant == NULL) {//un solo elemento.
nuevo_dato->right = aux;
nuevo_dato->left = aux->left;
aux->left = nuevo_dato;
*lista = nuevo_dato;
}
if (aux == NULL) { //Ultimo elemento de la lista.
ant->right = nuevo_dato;
nuevo_dato->left = ant;
nuevo_dato->right = aux;
}
//Si no es porque esta en el medio!
nuevo_dato->right = aux;
aux->left = nuevo_dato;
nuevo_dato->left = ant;
ant->right = nuevo_dato;
}
node * newList() {
node *nuevo;
nuevo = new node;
nuevo->data.num = -1;
nuevo->right = nuevo;
nuevo->left = nuevo;
return nuevo;
}
void printListAsc(node *head) {
node *current = head;
while (current->right != head) {
current = current->right;
printNode(current);
}
}
void printNode(node *current) {
cout << current->data.num << endl;
}
int generador(int max, int min) {
return rand() % (max - min + 1) + 2;
}