0
I’ve been trying to make a double circular list for hours. I’ve tried to fix all the bugs but got a 'Segmentation fault (core dumped) error that I have no idea what it is. Can someone please help me?
Below follows my code:
//Arquivo circDuplList.c
#include <stdio.h>
#include <stdlib.h>
#include "cdlist.h"
struct cdlst{
int info;
cdLst* ant;
cdLst* prox;
};
/*Create an empty circ dupl list*/
cdLst* create_empty(void)
{
return NULL;
}
/*Insert a node to the front of the circular list*/
cdLst* insert_front(cdLst* cdl, int info)
{
cdLst* p = (cdLst*)malloc(sizeof(cdLst));
p->info = info;
p->prox = cdl;
if(cdl != NULL){
cdLst* aux = cdl;
while(aux->prox != cdl){
aux = aux->prox;
}
cdl->ant = p;
p->ant = aux;
aux->prox = p;
}else{
p->prox = p->ant;
p->ant = p->prox;
}
cdl = p;
return cdl;
}
/*Insert a node to the end of the circular list*/
cdLst* insert_end(cdLst* cdl, int info)
{
cdLst* n = (cdLst*) malloc(sizeof(cdLst));
n->info = info;
if(cdl != NULL){
cdLst* p = cdl;
while(p->prox != NULL){
p = p->prox;
}
p->prox = n;
n->prox = cdl;
n->ant = p;
cdl->ant = n;
}
else{
n->ant = n->prox = NULL;
return n;
}
return cdl;
}
/*Remove a node from the list*/
void retira(cdLst* cdl,int info)
{
if(cdl == NULL){
printf("The list is empty");
exit(1);
}
cdLst* p = cdl;
while(p->prox != cdl && p->info != info)
p = p->prox;
if(p->prox == cdl && p->info != info){
exit(1);
}
if(p == cdl){
cdl = cdl->prox;
cdl->ant = p->ant;
}
p->ant->prox = p->prox;
free(p);
}
/*Print the list*/
void cdl_print(cdLst* cdl)
{
cdLst* p = NULL;
printf("\n%d",p->info);
p = p->prox;
while(p != cdl)
printf("\n%d",p->info);
}
/*Print the list in the reverse order*/
//void cdl_print_rev(cdLst* cdl);
/*Verify if the list is empty*/
int empty(cdLst* cdl)
{
return cdl == NULL;
}
/*Free memory of the list*/
//void cdl_free();
//Arquivo main.c
#include <stdio.h>
#include <stdlib.h>
#include "cdlist.h"
int main(void){
cdLst* l = create_empty();
int v = empty(l);
printf("%d",v);
l = insert_front(l,3);
v = empty(l);
printf("%d",v);
l = insert_front(l,2);
l = insert_front(l,3);
l = insert_front(l,8);
l = insert_front(l,65);
l = insert_front(l,3);
cdl_print(l);
}