Thread: Doubly linked list add in order

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Exclamation Doubly linked list add in order

    I'm trying to create a doubly linked list that will add in order. I'm having trouble creating the AddInOrder method. Are there any web sources that any of you know of that could help me? or perhaps a part of my code that is not correct....or completely wrong .

    Code:
    void DList::AddAtHead(string& rsNewData) {
    	DListNode* plnNew = new DListNode(rsNewData);
    	plnNew-> plnNext = plnHead;
    	if (plnHead != NULL) 
    		plnHead-> plnPrev= plnNew;
    	else plnTail = plnNew;
    	plnHead= plnNew;
    	++nNumItems ;
    }
    
    void DList::AddInOrder (string & rsNewData) {
    	DListNode* plnNew = new DListNode(rsNewData);
    	//If it's the first item
    	if (nNumItems == 0) { 
    		AddAtHead(rsNewData);
    	}
    	else
    		//If it's smaller than the first - Add at start
    		if (rsNewData <= plnHead->sWord) { 
    			AddAtHead(rsNewData);
    		}
    		//If it's greater than the first - Loop through
    		else {
    			DListNode * plnCurrent = plnHead;
    			plnCurrent = plnHead->plnNext;
    			while (plnCurrent != plnHead) {
    				if (plnCurrent->sWord < plnNew->sWord) 
    					plnCurrent = plnCurrent->plnNext;
    				else
    					break;
    			}
    			plnNew->plnNext = plnCurrent;
    			plnNew->plnPrev = plnCurrent->plnPrev;
    			plnCurrent->plnPrev = plnNew;
    			(plnNew->plnPrev)->plnNext = plnNew;
    		}
    }
    Thanks for any help you can give,
    Dan
    Last edited by Dan17; 10-23-2006 at 01:41 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you keep indenting like that then you'll end up with very long lines. Usually else ifs are indented like so:
    Code:
    if(x) {
        /* ... */
    }
    else if(y) {
        /* ... */
    }
    else if(z) {
        /* ... */
    }
    else {
    
    }
    What exactly is wrong with your code? Does it crash? Does it insert items in the wrong places? Does it insert items at all?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    System-7
    Join Date
    Nov 2005
    Posts
    65
    When I input an example list:
    Tree
    First
    Zen

    the first two have no problem since they go into the AddToHead method. But when it tries to add Zen to the end of the list....it crashes with an exception in xstring. My .cpp file is now included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM