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