Fixed it, but need advice
Code:
#include <iostream>
class myNodeList
{
private:
struct myNode
{
char value;
myNode * next;
};
myNode * first;
myNode * index;
public:
myNodeList() : first(0),index(0){}
~myNodeList()
{
myNode * finger;
finger=first;
while(!(finger==0))
{
myNode * nullnext;
nullnext=finger->next;
delete finger;
finger = nullnext;
}
}
void addNode(char value)
{
myNode * newNode = new myNode();
newNode->value = value;
newNode->next = 0;
if (first==0){first=newNode;}
else
{
myNode * finger;
finger=first;
while(!(finger->next==0)){finger=finger->next;}
finger->next = newNode;
}
}
void eachNode(void)
{
if (index==0){index=first;}
else {index=index->next;}
}
char eachNode_getValue(void)
{
return index->value;
}
void printNodeList(void)
{
myNode * finger;
finger=first;
while(!(finger==0)){printf("%c",finger->value);finger=finger->next;};
}
int nodeCount(void)
{
myNode * finger;
int count=0;
finger=first;
while(!(finger==0)){++count;finger=finger->next;}
return count;
}
};
int main(void)
{
myNodeList nodez;
nodez.addNode('y');
nodez.addNode('a');
nodez.addNode('y');
nodez.addNode('!');
nodez.printNodeList();
printf("\n");
for (int index=0;index<nodez.nodeCount();++index)
{
nodez.eachNode();
printf("%c.",nodez.eachNode_getValue());
}
printf("\n");
system("PAUSE");
}
Works just peachy.
Thanks for all the advice. The problem came because the FOR loop wasn't evaluating the eachNode along with the index, it was ignoring the part after the comma.
However, I would like to know if there is a way I can squeeze the call to eachNode() up into the third bracket in FOR. It just looks cleaner to me, don't ask :)