As I was sitting around doing the usual code-thinking, I suddenly thought of a thing that sounded real cool. An array of linked lists. This was something I'd never tried or seen before, so I naturally tried to code it. It appeared to work fine, but not all is OK. I probably missed something really obvious, but I just can't figure it out (I'm not really that awake at the moment).
I know, I know. I should not just post every little problem i run into. Especially not without having done thorough research of my own, but I just couldn't hold out any longer...
So here goes:
Code:
#include <stdio.h>
#include <time.h>
#include <windows.h>
class list
{
public:
list(){i=rand()%6+1,nxt=NULL;}
void showi();
void newnode();
list *nxt;
private:
int i;
};
int main()
{
srand(time(NULL));
list a_list[3];
a_list[0].newnode();
a_list[0].newnode();
a_list[0].newnode();
a_list[2].newnode();
printf("list[0] vars: ");
a_list[0].showi();
printf("list[1] vars: ");
a_list[1].showi();
printf("list[2] vars: ");
a_list[2].showi();
return 0;
}
void list::showi()
{
list *t=this;
do
{
printf("%i ",t->i);
if(t->nxt!=NULL)
t=t->nxt;
}
while(t->nxt!=NULL);
}
void list::newnode()
{
list *t=this;
spawn=new list;
if(spawn==NULL)
return;
else
{
t->nxt=spawn;
t=t->nxt;
}
}
The problem seems to be in displaying the variables stored in each node, as I have stepped through each creation of a new node and that sems fine. But I'm not sure of even that (no wonder, since I'm not really sure if I'm dreaming all this. That's how sleepy i am). I just don't know what the problem is... I thank all replies very gratefully, as I don't think I deserve them...