Pleas i need help to fix a function which seems to be wrong! it must delete a node of a circular list.
Code:
static int rem_plan_rec(char *aname, plan_t **agende, int left) {
plan_t *tmp;
int i=0;
ec_null1( *agende, EINVAL )
if (left==0) return 1;
if (strncmp((*agende)->aname, aname, LAGENDA+1)==0) {
if ((*agende)->many!=0) //in this case can't be deleted
return 2;
tmp=(*agende)->nxt;
(*agende)->nxt=NULL;
free((*agende));
*agende=tmp;
if (--plan_elems==1) <----- MY PROBLEM!
tmp->nxt=tmp;
else if (plan_elems>1) {
while (i++<plan_elems-1) tmp=tmp->nxt; /* fa si che la lista resti circolare */
tmp->nxt=(*agende);
}
return 0;
}
else {
agende=&((*agende)->nxt);
return rem_plan_rec(aname, agende, --left);
}
}
//the list
typedef struct plan_t {
/* Nome dell'agenda (=nome file) */
char aname[LAGENDA+1];
/* Puntatore alla lista degli eventi */
elem_t *events;
/* Numero di eventi totali */
unsigned int many;
/* Puntatore all'agenda successiva */
struct plan_t *nxt;
}
plan_t;
plan_elems is an integer value that indicates how many nodes the list has. I Use the parameter left to know when the list is finished cause it's circular.
My problem is, as i indicated in the code, that when there are only two nodes, and one of them is deleted, the other one seems to be not connected to itself!
Cheers