I was trying to convert my Linked List into a Circular Linked List (everything worked ok with the not circular one)... but then when I do the insert (inserta) operation, and test it... DOS prompt closes itself (crashes)... then when I debug it, it says I have an acces violation (segmentation fault)...
So... why?! Code:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef int tipoDato;
typedef struct tipoNodo{
tipoDato elem;
struct tipoNodo *sig;
}tipoNodo;
typedef tipoNodo *tipoLista;
typedef tipoNodo *tipoPos;
void inicializa (tipoLista *r){
*r=NULL;
}
int vacia (tipoLista *r){
return!*r;
}
tipoPos primero (tipoLista *r){
return *r;
}
tipoPos ultimo (tipoLista *r){
tipoPos aux;
if (vacia(r))
return NULL;
aux=*r;
while(aux->sig!=*r)
aux=aux->sig;
return aux;
}
tipoPos anterior (tipoPos pos, tipoLista *r){
tipoPos aux;
if(vacia(r)||pos==NULL)
return NULL;
aux=*r;
while(aux->sig!=pos && aux!=NULL)
aux=aux->sig;
return aux;
}
tipoPos siguiente(tipoPos pos, tipoLista *r){
if(vacia(r)||pos==NULL)
return NULL;
return pos->sig;
}
void inserta (tipoDato e, tipoPos pos, tipoLista *r){
tipoPos aux;
aux=(tipoPos)malloc(sizeof(tipoNodo));
if(aux==NULL){
printf("memoria del sistema insuficiente");
return;
}
aux->elem=e;
if (pos==NULL){
aux->sig=*r;
ultimo(r)->sig=aux;
*r=aux;
}
else{
aux->sig=pos->sig;
pos->sig=aux;
}
}
void elimina (tipoPos pos, tipoLista *r){
if(vacia(r)||pos==NULL){
printf("insuficiencia de datos");
return;
}
if (pos->sig=pos)
*r=NULL;
else if (pos==*r){
ultimo(r)->sig=(*r)->sig;
*r=(*r)->sig;
}
else
anterior(pos,r)->sig=pos->sig;
free(pos);
}
tipoPos localiza (tipoDato elem, tipoLista *r){
tipoPos aux;
aux=*r;
while (aux!=NULL&&aux->elem!=elem)
aux=aux->sig;
return aux;
}
tipoDato recupera (tipoPos pos, tipoLista *r){
if(vacia(r)||pos==NULL){
printf("insuficiencia de datos, el programa se terminara");
exit(1);
}
else
return pos->elem;
}
void imprime (tipoLista *r){
tipoPos aux;
aux=*r;
while (aux!=NULL){
printf("%d ",aux->elem);
aux=aux->sig;
}
}
void anula (tipoLista *r){
tipoPos aux;
while(*r!=NULL){
aux=*r;
*r=aux->sig;
free(aux);
}
}
int main(int argc, char *argv[]){
tipoLista r;
int i;
inicializa(&r);
if(vacia(&r))
printf("la lista esta vacia\n\n");
system("cls");
for(i=0;i<20;i++)
inserta(rand()%100,ultimo(&r),&r);
imprime(&r);
printf ("\n\n");
elimina(ultimo(&r),&r);
elimina(primero(&r),&r);
imprime(&r);
printf ("\n\n");
elimina(anterior(ultimo(&r),&r),&r);
elimina(siguiente(primero(&r),&r),&r);
imprime(&r);
printf ("\n\n");
for(i=0;i<50;i++){
if(localiza(i,&r)!=NULL)
elimina(localiza(i,&r),&r);
}
imprime(&r);
printf ("\n\n");
anula(&r);
if(vacia(&r))
printf("la lista esta vacia\n\n");
system("PAUSE");
return 0;
}
Thanks in advance...