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);
    		}
    		else
    			//If it's larger than the last - Add at end
    			if (rsNewData >= plnTail->sWord) {
    				plnNew->plnPrev = plnCurrent->plnPrev;
    				plnNew->plnNext = NULL;
    			}
    			//If it's smaller than the last - Loop through
    			else {
    				DListNode * plnCurrent = plnTail;
    				while (plnCurrent != NULL) {
    					while (rsNewData < plnCurrent-> sWord) {
    						plnCurrent = plnCurrent->plnPrev;
    					}
    					plnNew->plnNext = plnCurrent->plnNext;
    					plnNew->plnPrev = plnCurrent;
    				}
    			}
    
    
    }
    Thanks for any help you can give,
    Dan

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Adding in an ordered doubly linked list.

    Google is your best friend.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    What manner of C code is
    Code:
    void DList::AddInOrder (string & rsNewData) {
    That smells greatly of C++.

  4. #4
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Oh whoops...wrong forum...sorry

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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