PI-ITBA / 2024_02

Consultas 2C 2024
4 stars 0 forks source link

EJ15 TP11 #166

Open JoaquinCofino opened 1 day ago

JoaquinCofino commented 1 day ago

Hola, buenas tardes, queria consultar ahora este ejercicio, pareceria que hay un error en la funcion get, pero no logro identificar el error. Desde ya muchisimas gracias.

#include "moveToFrontADT.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static int compareStruct (elemType e1, elemType e2) {
    return e1.code - e2.code;
}

typedef int (*cpm) (elemType, elemType);

typedef struct tList{
    elemType elem;
    struct tList * tail;
}tList;

typedef struct moveToFrontCDT{
    tList* first;
    size_t dim;
    tList* current;
    cpm fun;
}moveToFrontCDT;

moveToFrontADT newMoveToFront(int (*compare) (elemType e1, elemType e2)){
    moveToFrontADT toReturn = malloc(sizeof(moveToFrontCDT));
    toReturn->current = NULL;
    toReturn->dim = 0;
    toReturn->first = NULL;
    toReturn->fun = compare;
    return toReturn;
}

void freeList(tList* list) {
    while(list != NULL){
        tList* next = list->tail;
        free(list);
        list = next;
    }
}

void freeMoveToFront(moveToFrontADT m){
    if(m == NULL){
        return;
    }
    freeList(m->first); 
    free(m);
}

static tList * addREC(tList * list, elemType elem, cpm fun, int* flag){
    if(list == NULL){
        tList * toReturn = malloc(sizeof(tList));
        toReturn->elem = elem;
        toReturn->tail = NULL;
        return toReturn;
    }
    if(fun(list->elem, elem) == 0){
        tList * siguiente = list->tail;
        free(list);
        (*flag)++;
        return siguiente;
    }
    list->tail = addREC(list->tail, elem, fun, flag);
    return list;
}

/*
static tList * addREC(tList * list, elemType elem) {
    if(list == NULL){
        tList * toReturn = malloc(sizeof(tList));
        toReturn->elem = elem;
        toReturn->tail = NULL;
        return toReturn;
    }
    list->tail = addREC(list->tail, elem);
    return list;
}
*/

unsigned int add(moveToFrontADT moveToFront, elemType elem) {
    int flag = 0; //1 si ya estaba
    moveToFront->first = addREC(moveToFront->first, elem, moveToFront->fun, &flag);
    if(!flag){
        moveToFront->dim++;
    }
    if(flag){
        tList * nuevoNodo = malloc(sizeof(tList));
        nuevoNodo->elem = elem;
        nuevoNodo->tail = moveToFront->first;
        moveToFront->first = nuevoNodo;
    }/*else{
        moveToFront->dim++;
    }*/
    return !flag;
}

unsigned int size(moveToFrontADT moveToFront){
    return moveToFront->dim;
 }

void toBegin(moveToFrontADT moveToFront){
    moveToFront->current = moveToFront->first;
}

int hasNext(moveToFrontADT moveToFront){
    return moveToFront != NULL && moveToFront->current != NULL;
}

elemType next(moveToFrontADT moveToFront){
    assert(hasNext(moveToFront));
    elemType current = moveToFront->current->elem;
    moveToFront->current = moveToFront->current->tail;
    return current;
}

static tList * getAYUDAAAAA(tList * list, elemType elem, cpm fun, int* flag){
    if(list == NULL){
        return list;
    }
    if(fun(list->elem, elem) == 0){
        tList * siguiente = list->tail;
        free(list);
        (*flag)++;
        return siguiente;
    }
    list->tail = getAYUDAAAAA(list->tail, elem, fun, flag);
    return list;
} 

elemType * get(moveToFrontADT moveToFront, elemType elem){
    int flag = 0;
    moveToFront->first = getAYUDAAAAA(moveToFront->first, elem, moveToFront->fun, &flag);
    if(flag){
        tList * nuevoNodo = malloc(sizeof(tList));
        nuevoNodo->elem = elem;
        nuevoNodo->tail = moveToFront->first;
        moveToFront->first = nuevoNodo;
    }else{
        return NULL;
    }
    elemType * toreturn = malloc(sizeof(elemType));
    toreturn->code = moveToFront->first->elem.code;
    strcpy(toreturn->name, moveToFront->first->elem.name);
    return toreturn;
}
marcelogarberoglio commented 23 hours ago

La función getAYUDAAAAA está eliminando el nodo de la lista. Está bien que lo "desenganche"pero no tendría que hacer el free sino dejarlo en un parámetro de salida, de esa forma get puede moverlo para que esté al principio de la lista Lo que vos estás haciendo es dejar el elem que te pasan. Fijate que en el test estamos usando a própsito un struct donde completamos el campo que determinan si son iguales pero no el otro.