#include "tabla_frec.hpp" void inicializar(Tabla& t){ t.numElementos = 0; } bool anyadir(Tabla& t, int n){ //encontrar n en tabla int pos = 0; bool encontrado = false; while (!encontrado && pos < t.numElementos){ if (t.elementos[pos].numero == n){ encontrado = true; } else{ pos = pos + 1; } } if (!encontrado){ pos = - 1; } bool error = false; //si está n en tabla, recolocar if (pos != -1) { t.elementos[pos].frec = t.elementos[pos].frec + 1; while (pos > 0 && t.elementos[pos].frec > t.elementos[pos-1].frec) { Tabla::Frecuencia aux = t.elementos[pos]; t.elementos[pos] = t.elementos[pos-1]; t.elementos[pos-1] = aux; pos--; } } else { //si no está, introducir al final si se puede if (t.numElementos < MAX_NUM_DATOS) { t.elementos[t.numElementos].numero = n; t.elementos[t.numElementos].frec = 1; t.numElementos = t.numElementos + 1; } else { error = true; } } return error; } int total(const Tabla& t) { return t.numElementos; } int infoEnt(const Tabla& t, int n) { if (1 <= n && n <= t.numElementos) { return t.elementos[n-1].numero; } else { return 0; } } int infoFrec(const Tabla& t, int n) { if (1 <= n && n <= t.numElementos) { return t.elementos[n-1].frec; } else { return 0; } }