Hey guys, I'm trying to write a double linked class, and am having some troubles on operations that I can do to it.
So far I have implemented the following:
-output contents of list from both the head and tail of list
-add at head of list
-AddinOrder (having some problems)
If it is the first element, I can add it at the head, if it's greater than the element at the tail, I can add it there, but when adding in the middle somewhere, I am getting an unexpected result. I am trying to test this by adding in order exclusively (i never call AddatHead from the switch) and then plugging in a series of strings (in order).
Now, when i try to add a string that's not the first, but before the last, I get the following: As soon as I display the addinorder string, it is displaying me the contents of the list over and over again.
I'm sure it's a logic error that I'm overlooking, i'm going to see what I can do in the meantime with it, perhaps someone might point me in the right direction...
here is my listnode declaration
Code:
class DListNode
{
friend class DList; // List needs to access private data
private:
string sWord;
DListNode* plnNext;
DListNode* plnPrev;
public:
DListNode() : sWord(""), plnNext(NULL), plnPrev(NULL) { }
DListNode(string &rsData ) : sWord(rsData ), plnNext(NULL), plnPrev(NULL) { }
};
and my list class:
Code:
class DList {
private:
int nNumItems;
DListNode* plnHead;
DListNode* plnTail;
public:
DList() { nNumItems= 0; plnHead = NULL; plnTail = NULL; }
void AddAtHead(string &rsNewData );
void OutputAllRecordsHead();
void OutputAllRecordsTail();
void AddInOrder (string & rsNewData);
};
Descoped methods of listclass:
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)
{
if (plnHead == NULL || rsNewData > plnHead->sWord)
{
AddAtHead(rsNewData);
return;
}
if(rsNewData > plnTail->sWord)
{
plnTail->plnNext = plnNew;
plnNew->plnNext = NULL;
plnNew->plnPrev = plnTail;
++nNumItems;
}
else
{
DListNode* plnCurr = plnHead;
DListNode* plnNew = new DListNode(rsNewData);
while(rsNewData < plnCurr->sWord)
plnCurr = plnCurr->plnNext;
plnNew->plnPrev = plnCurr;
plnCurr->plnNext = plnNew;
//now to assing the next value to plnNew
plnCurr = plnTail;
while(rsNewData > plnCurr->sWord)
plnCurr = plnCurr->plnPrev;
plnNew->plnNext = plnCurr;
plnCurr->plnPrev = plnNew;
++nNumItems;
}
}
void DList:: OutputAllRecordsHead() {
DListNode * plnCurrent = plnHead;
while (plnCurrent != NULL) {
cout << plnCurrent-> sWord << endl;
plnCurrent = plnCurrent->plnNext;
}
cout << "Total number of items : " << nNumItems << endl;
}
void DList:: OutputAllRecordsTail() {
DListNode * plnCurrent = plnTail;
while (plnCurrent != NULL) {
cout << plnCurrent-> sWord << endl;
plnCurrent = plnCurrent->plnPrev;
}
cout << "Total number of items : " << nNumItems << endl;
}
I have a feeling that by re-declaring plnCurr to point to tail, I might be mixing up a couple of things, but from my understanding, plnCurr is pointing to a data block which already has a pointer to it, so I am just accessing that data through plnCurr, change the values, and then I can move around with plnCurr and the changes at the data block that plnCurr was pointing to is kept.
Am I way off on this one? Any help is appreciated.