[comp.lang.c] Linked-list problems in DBW_Render

prem@geomag.fsu.edu (Prem Subrahmanyam) (08/16/89)

   I have been trying to modify DBW 1.0 and have done quite well so far,
   however, I seem to be at an impass.  See, I've been trying to implement
   an algorithm that will use a sorted linked-list.  The old file read
   procedure would simply knit together the list by having a pointer called
   opp to point to the *next field of the last read in item and then say:

   allocate new object; (pseudocode, folx)
   *opp = (long *)(pointer to new object)
   make lots of assignments.
   opp = &(long *)(pointer to object)->next

   etc. etc. etc.

   next is defined as a pointer to a node.  the object can be one of many 
   types (sphere, parallelogram, triangle, etc.).

   Now, here's what I've tried to do.

   allocate new object;
   make lots of assignments;
   sort( *opp,(pointer to new object));

   opp does not change positions, but acts as a root for this particular
   segment of the list.  Sort does the following:

   sort( sortroot, np)
   node **sortroot;
   node *np;
       {
	  node *cur=*sortroot;  /* i.e. , make cur point to whatever the root is
				    pointing to */
	  node *prev;
	  if (!*sortroot) {
			      *sortroot = np;
			      np->next = NULL;
                          }   /* insert at the beginning of a non-empty list */
          else 
	    if (if *np should go before **sortroot)
	       /* i.e. item should go at beginning of non-empty list */
	       {  
		  np->next = *sortroot;
		  *sortroot = np;
               }
            else
	       {  while ((cur->next)&&(*cur should go before *np) {
			prev = cur;
			cur = cur->next;
                    }
                 if (we're at the end of the list) {
		      cur->next = np;
		      np->next = NULL;
		      }
                 else
		    {  
		       np->next = cur;
		       prev->next = np;
                    }
               }
       }

  bear in mind that the objects actually being sorted away are much more than 
  nodes, but contain within them the items in a node...something like

  typedef struct NODE {
      NODE *next;
      other stuff;
      } node;

  and a sphere object is like

  typedef struct {
       node *next;
       other stuff; /* other stuff in node */
       sphere thingies;
       }sphere; 

  I have tried everything short of pulling all my hair out to get this to
  work right.  I've tried type-casting every pointer in sort to (long *).
  EVERYTHING!!!!!  The strangest thing is that the debug procedures show
  everything to be in place in the list USING THE EXACT SAME ACCESS AS THE
  ACTUAL PROCEDURES DO, yet none of the procedures are able to locate and
  use the objects.  It's the case of the missing linked list that's really
  not missing, but is there.  Anyone out there worked a lot on modifying
  DBW who could point out what might be going wrong.  I modified dumpnode()
  to dump all the information contained within the objects, and believe me,
  there is not an item missing.  I would use the Lattice CodeProbe, but I
  only have 512K of memory, not enough to be usable.  PLEASE HELP!!!!!!

  as always, direct replies electro-mail to prem@geomag.gly.fsu.edu 
  and once again,....thanks for your support.