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 .
Thanks for any help you can give,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; } } }
Dan