Help on circular list!

This is a discussion on Help on circular list! within the C Programming forums, part of the General Programming Boards category; Pleas i need help to fix a function which seems to be wrong! it must delete a node of a ...

  1. #1
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60

    Help on circular list!

    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

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    where in the code is plan_elems declared / defined?? and maybe use that criteria for the last remaining instead of the last two.
    Last edited by itCbitC; 01-30-2009 at 01:20 PM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    the other one seems to be not connected to itself!
    I don't see anything obvious in your code, what are the concrete observation leading to your problem? another function is failing? As requested in the previous post, additional code may help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21