So, I have a class that looks like... this...
- Code: Select all
class cLinkedStrings
{
public:
cLinkedStrings ();
bool readString (ifstream * f);
void add (cLinkedStrings ** phead);
cLinkedStrings * next;
void printString();
const cLinkedStrings operator +(const cLinkedStrings& CLS2) const;
const cLinkedStrings operator =(const cLinkedStrings& CLS2) const;
bool operator ==(const cLinkedStrings& CLS2) const;
bool operator <(const cLinkedStrings& CLS2) const;
bool operator <=(const cLinkedStrings& CLS2) const;
bool operator >(const cLinkedStrings& CLS2) const;
bool operator >=(const cLinkedStrings& CLS2) const;
bool operator !=(const cLinkedStrings& CLS2) const;
void append(cLinkedStrings ** head);
void insert(cLinkedStrings ** head);
private:
string s;
};
note cLinkedStrings * next, the overloaded operators, insert, and the private string s.
next is the pointer to the next object in the list.
The overloaded operators work on strings in the object, exactly as the string class uses them.
s is the string of the object.
insert is my headache. See, I'm supposed to write a function that inserts a new node into a linked list alphabetically (read: alphanumerically).
The best I have come up with is this.
- Code: Select all
void cLinkedStrings::insert(cLinkedStrings ** point)
{
cLinkedStrings * pt = *point;
while ((pt != NULL) && !(*this <= *pt))
{
pt = pt->next;
}
if ((pt == NULL) || !(*this <= *pt))
{
next = NULL;
*point = this;
}
else if ((*this <= *pt) && (pt != NULL))
{
next = *point;
*point = this;
}
}
Now, I get my nodes by reading individual lines from a file. This file looks like this.
- Code: Select all
a
c
f
b
k
j
z
y
m
d
Yes, just single characters on each line. Now, when I run the program, it only adds 4 nodes to this list, in the following order: z, y, m, d
The last 4 letters alone. Argh... any ideas on what's wrong with my insert method?