0

Necesito que el programa elimine un nodo de la lista(doblemente ligada) cuando cierta condición ocurre, sin embargo cuando esto sucede, al intentar acceder a los valores anteriores y posteriores del nodo marca el error de:

Segmentation Fault 11

Estuve probando el codigo y me di cuenta que este error se presenta justamente en la linea 114, en la funcion de borrar nodo:

void deleteNODO(LISTA* list,NODO*nod){
  printf("%s\n","eliminar nodo" );
  NODO* pointer = list->head;
    while (pointer!=NULL) {
      if (pointer == nod) {
        printf("%s\n","encontrado!");
        if (pointer == list->head) {
          printf("%s\n","lista vale 1" );
          list->head = pointer->next;
          list->tail = NULL;
        } else if (pointer == list->tail) {
          printf("%s\n","lista en cola" );
          list->tail =  pointer->prev;
          list->tail->next = NULL;
        } else{
          printf("%s\n","lista cuerpo" );

          NODO* previo = pointer->prev;
          printf("%s\n","1" );
          //PROBLEMA JUSTAMENTE AQUI
          previo->next = pointer->next;
          printf("%s\n","2" );
          pointer->next->prev = previo;
          printf("%s\n","3" );

        }
        list->size--;
        freeNodo(pointer);
      }
    }

}

y bueno si es necesario también dejo el resto del codigo:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "assert.h"

typedef struct{
  double *F; // vector n-dimensional
  int n; // dimension del vector F
} SOLUCION;

typedef struct nodo{
SOLUCION solucion;
  struct nodo *prev;
  struct nodo *next;
} NODO;

typedef struct{
  NODO *head;
  NODO *tail;
  int size;
} LISTA;
void cpySolucion(NODO*to , NODO*from){
  to->solucion.n = from->solucion.n;
  memcpy(to->solucion.F,from->solucion.F,sizeof(double)*to->solucion.n);

}
void initNodo(NODO* nod){
  int dim = nod->solucion.n;
  nod->solucion.F = (double*)malloc(sizeof(double)*dim);
  nod->next = NULL;
  nod->prev = NULL;
};
void freeNodo(NODO*nod){
  nod->next = NULL;
  nod->prev = NULL;
  free(nod->solucion.F);
  free(nod);
}
void cpyNodo(NODO* to, NODO* from){
  cpySolucion(to,from);
    to->next = from->next;
    to->prev = from->prev;
}

void addLista(LISTA* list, NODO* nodo){
  printf("%s\n","añadiendo a la lista" );
    NODO* newnodo;
    newnodo = (NODO*) malloc(sizeof(NODO));
  if (list->size == 0) {
    assert(list->head==NULL);
    assert(list->tail==NULL);
    initNodo(newnodo);
    cpyNodo(newnodo,nodo);
    list->head = newnodo;
    list->tail = newnodo;
    newnodo->next = NULL;
    newnodo->prev = NULL;
    list->size = list->size +1;
  } else {
    initNodo(newnodo);
    cpyNodo(newnodo,nodo);
    newnodo->prev = list->tail;
    list->tail = newnodo;
    newnodo->prev->next = newnodo;
    newnodo->next = NULL;
    list->size = list->size +1;
  }
}

NODO* getElementAtPosition(LISTA*list ,int pos){
  NODO* returnNodo = list->head;
  for (int i = 0; i < pos; i++) {
    returnNodo = returnNodo->next;
  }
  return returnNodo;
}
void deleteElementAtPosition(LISTA* list,int pos){
  NODO* elemento = getElementAtPosition(list,pos);

    if (list->size == 1) {
      list->head = NULL;
      list->tail = NULL;
    } else if (elemento == list->tail) {
      list->tail =  elemento->prev;
      list->tail->next = NULL;
    } else{
      NODO* previo = elemento->prev;
      elemento->prev->next = elemento->next;
      elemento->next->prev = previo;
    }
    list->size--;
    freeNodo(getElementAtPosition(list,pos));
}
void deleteNODO(LISTA* list,NODO*nod){
  printf("%s\n","eliminar nodo" );
  NODO* pointer = list->head;
    while (pointer!=NULL) {
      if (pointer == nod) {
        printf("%s\n","encontrado!");
        if (pointer == list->head) {
          printf("%s\n","lista vale 1" );
          list->head = pointer->next;
          list->tail = NULL;
        } else if (pointer == list->tail) {
          printf("%s\n","lista en cola" );
          list->tail =  pointer->prev;
          list->tail->next = NULL;
        } else{
          printf("%s\n","lista cuerpo" );

          NODO* previo = pointer->prev;
          printf("%s\n","1" );
          //PROBLEMA JUSTAMENTE AQUI
          previo->next = pointer->next;
          printf("%s\n","2" );
          pointer->next->prev = previo;
          printf("%s\n","3" );

        }
        list->size--;
        freeNodo(pointer);
      }
    }

}
int vectoresSonIgules(NODO*nod1 , NODO*nod2){
  int sonIguales = 1;
  for (int i = 0; i < nod1->solucion.n; i++) {
    if (nod1->solucion.F[i] != nod2->solucion.F[i]) {
      sonIguales = 0;
    }
  }
  return sonIguales;
}
int primerVectorEnNodoDominaSegundo(NODO*nod1,NODO*nod2){
  for (int i = 0; i < nod1->solucion.n; i++) {
    double currentNumber = nod1->solucion.F[i];
    for (int a = 0; a < nod1->solucion.n; a++) {
      if (currentNumber <= nod2->solucion.F[a]) {
        continue;
      } else{
        return 0;
      }
    }
  }
  int sonIguales = 1;
  for (int i = 0; i < nod1->solucion.n; i++) {
    if (nod1->solucion.F[i] != nod2->solucion.F[i]) {
      sonIguales = 0;
    }
  }
  if (sonIguales == 1) {
    return 0;
  } else{
    return 1;
  }
}
void initLista(LISTA* list) {
  list->size = 0;
  list->head = NULL;
  list->tail = NULL;
}
void actualizaSolucion(LISTA*list,NODO* nod){
  printf("%s\n","Actualizando solucion");
  if (list->size==0) {
    printf("%s\n","lista 0");
    addLista(list,nod);
  } else{
    int numero = list->size;
    for (int i = 0; i < numero; i++) {
      printf("%d\n",list->size);
      if (primerVectorEnNodoDominaSegundo(nod,getElementAtPosition(list,i)) == 1) {
        printf("%s\n","A DOMINA a B" );
        deleteNODO(list,getElementAtPosition(list,i));
      } else if (primerVectorEnNodoDominaSegundo(getElementAtPosition(list,i),nod) == 1) {
        printf("%s\n","B DOMINA a A" );
        return;
      } else if (vectoresSonIgules(nod,getElementAtPosition(list,i)) == 1) {
        printf("%s\n","son  igules" );
        return;
      }
    }
    addLista(list,nod);
  }
}

int main(int argc, char const *argv[]) {
  LISTA nuevaLista;
  initLista(&nuevaLista);
  int dimension;
  int cantidad;
  printf("%s\n","ingrese la cantidad de vectores");
  scanf("%d",&cantidad);
  printf("%s\n","ingrese la dimension" );
  scanf("%d",&dimension);

  double (*vectores)[dimension] = malloc(sizeof(double[cantidad][dimension]));
  int numero = 0;
  switch (dimension) {
    case 2:
      for (int b = 0; b < cantidad; b++) {
          scanf("%lf %lf",&vectores[b][0],&vectores[b][1]);
          numero++;
        }
    break;
    case 3:
    for (int b = 0; b < cantidad; b++) {
        scanf("%lf %lf %lf",&vectores[b][0],&vectores[b][1],&vectores[b][2]);
      }
    break;
    case 4:
    for (int b = 0; b < cantidad; b++) {
        scanf("%lf %lf %lf %lf",&vectores[b][0],&vectores[b][1],&vectores[b][2],&vectores[b][3]);
      }
    break;
    case 5:
    for (int b = 0; b < cantidad; b++) {
        scanf("%lf %lf %lf %lf %lf",&vectores[b][0],&vectores[b][1],&vectores[b][2],&vectores[b][3],&vectores[b][4]);
      }
    break;
    case 8:
    for (int b = 0; b < cantidad; b++) {
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&vectores[b][0],&vectores[b][1],&vectores[b][2],&vectores[b][3],&vectores[b][4],&vectores[b][5],&vectores[b][6],&vectores[b][7]);
      }
    break;
    case 10:
    for (int b = 0; b < cantidad; b++) {
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",&vectores[b][0],&vectores[b][1],&vectores[b][2],&vectores[b][3],&vectores[b][4],&vectores[b][5],&vectores[b][6],&vectores[b][7],&vectores[b][8],&vectores[b][9]);
      }
    break;
    default:
      printf("%s\n","POR FAVOR INGRESA UNA CANTIDAD VALIDA");
    }
    printf("%d\n",numero );

  for (int i = 0; i < cantidad; i++) {
    printf("%s %d\n","iteracion" ,i);
      NODO actualnodo;
      actualnodo.solucion.n = dimension;
      initNodo(&actualnodo);
      for (int a = 0; a < dimension; a++) {
        actualnodo.solucion.F[a] = vectores[i][a];
      }
      actualizaSolucion(&nuevaLista,&actualnodo);
    }
    for (int i = 0; i < nuevaLista.size; i++) {
      for (int a = 0; a < getElementAtPosition(&nuevaLista,i)->solucion.n; a++) {
        printf("%lf ",getElementAtPosition(&nuevaLista,i)->solucion.F[a]);
      }
      printf("%s\n","");

    }
  return 0;
}
3
  • No tengo ahora tiempo de depurar tu código, pero te indico que si obtienes un segfault es por intentar acceder a datos de un puntero nulo (o un puntero que no apunta a una zona válida). Puesto que la línea que te da problemas es previo->next = pointer->next; la causa puede ser que o bien pointer o bien previo sean NULL. Creo que podemos descartar pointer pues justo antes habías accedido a pointer->prev sin problemas, por lo que yo verificaría bien que previo no sea NULL. Ya que previo es el valor de pointer->prev verifica cuando construyes la lista que le asignas algo válido.
    – abulafia
    el 26 oct. 2018 a las 6:37
  • Hola y bienvenido a Stack Overflow en español. Nótese que esta pregunta tiene problemas de formato. Dale a edit para modificarla. Nos ayudará a todos para darte una solución más rápidamente. el 26 oct. 2018 a las 6:38
  • ¿Qué sistema operativo, compilador y versión de C estás utilizando? Esto nos ayudará a enfocar tu problema mucho más. También tienes páginas donde puedes compilar tu código online para que reproduzcamos tu problema mucho mejor. el 26 oct. 2018 a las 6:43

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.