linked list problem...

This is a discussion on linked list problem... within the C++ Programming forums, part of the General Programming Boards category; ok i am having a linked list problem..here is code...and explanation is uner the code... Code: #include <iostream> using namespace ...

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    linked list problem...

    ok i am having a linked list problem..here is code...and explanation is uner the code...

    Code:
    #include <iostream>
    using namespace std;
    
    struct train
    {
    	int passengers;
    	train* pnext_car;
    };
    
    int main(void)
    {
    	train (*whole_train) = new train[30];
    
    	// link the trains together
    	for(int i = 0; i <= 30; i++)
    	{
    		// HOW?? to link together...
    	}
    
    
    	return 0;
    }
    how do i lin the train cars together...i tried
    *(whole_train)[i]->pnext_car = &(whole_train)[i+1];

    it doesnt work..please help
    nextus, the samurai warrior

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    nevermind

    nevermind..i played around and got it...
    (*(whole_train + i)).pnext_car = &(*(whole_train + (i + 1)));
    nextus, the samurai warrior

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: nevermind

    Originally posted by nextus
    nevermind..i played around and got it...
    (*(whole_train + i)).pnext_car = &(*(whole_train + (i + 1)));
    you can also just do

    (whole_train + i)->pnext_car = whole_train + i + 1;

    No sense in making it more complicated than it is

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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