Just a question...

This is a discussion on Just a question... within the C++ Programming forums, part of the General Programming Boards category; As I was sitting around doing the usual code-thinking, I suddenly thought of a thing that sounded real cool. An ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    30

    Just a question...

    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...
    "You... Remarkably made it rain. Rain of blood." - Tomoe

  2. #2
    Ted
    Ted is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    If you would comment your code it would be easier to find your
    errors. How you ever got this code to run is beyond me.

    What is spawn and why is it undeclared?

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    30

    I think...

    ...the code ought to be fairly simple to follow. It's not like there is that much of it. Anyone who knows his way around a linked list should understand it quite easily (at least I think so, unless I'm completely off the track with this).

    Spawn was just a temporary pointer used to allocate new memory and check that it went OK. Oh... I see what you mean, must've forgotten to put that part in... Sorry. So here is what was supposed to be there:
    Code:
    void list::newnode()
    {
    	list *t=this;
    	list *spawn=new list;	//note the change
    	if(spawn==NULL)
    		return;
    	else
    	{
    		t->nxt=spawn;
    		t=t->nxt;
    	}
    }
    Hmm... actually I might have forgotten that in my real code as well... That may be the problem. Thanks for pointing it out. I'll just go and check out my code...

    [edit]Nope that wasn't the problem...[/edit]
    Last edited by ninja; 06-01-2002 at 03:56 AM.
    "You... Remarkably made it rain. Rain of blood." - Tomoe

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    30

    Damn!

    I must really have been awfully sleepy when I wrote that code! I just noticed that I don't even add the nodes to the end of the list! Works perfectly now, so no need for more comments, unless you've spotted some bad habbits or something. I always want to learn new stuff, so feel free to comment anything you want...
    "You... Remarkably made it rain. Rain of blood." - Tomoe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21