home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,904 of 33,346   
   James K. Lowden to alexo   
   Re: insert [sorted] a node in a linked l   
   09 Mar 13 23:48:55   
   
   From: jklowden@speakeasy.net   
      
   On Fri,  8 Mar 2013 21:27:57 CST   
   alexo  wrote:   
      
   > > You could write just   
   > >   
   > > 	Node *prior = head, *pnode = new Node(value);   
   > >   
   > > 	for( Node *p= head;   
   > > 		p && p->getValue() < value;   
   > > 		prior = p, p = p->next );   
   > >   
   > > 	insert(pnode, prior);   
   > >   
   >   
   > your code works for a very short list but fails with an input as   
   > given in my main() function.   
      
   Fails because of the size of the list?  Do you know some way to find   
   the insertion point without iterating over the elements?   
      
   > Moreover head is a Head and not a Node even if both inherit from   
   > List_Element.   
      
   The fact that Head is a special case --- and not a Node -- is another   
   example of a special case.  Fundamentally you need two classes: the   
   list and the node, where node is parameterized on datatype.  More than   
   that is a sign you're working too hard.  :-)   
      
   --jkl   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca