C++: Linked Lists

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

C++: Linked Lists

Post by JS Lemming »

CURSES!!! I needed to have a way of iterating through class instances and beable to add an instance to the begining of the list and beable to remove an instance anywhere in the list...

I got this far... and it does compile.

Code: Select all

#include <stdio.h>
#include <iostream.h>

//Blood - contains data for blood particles
class Blood
{
    public:
        //class members
        float x, y;
        float xvel, yvel;
        int life;

        //Pointer to last and next node (cell)
        Blood* pLast;
        Blood* pNext;
};

//Pointer to the first particle in the list
Blood* pHead = 0;

//CreateBlood - adds a new blood particle to the chain
void CreateBlood(int x, int y, int angle, int intensity)
{
    //create a new instance of Blood
    Blood* newnode = new Blood;
    newnode->x = x; //+ rand(-3,3)
    newnode->y = y;
    newnode->life = 5;
    //You get the picture, just convert the blitz source

    //make the new node point to the head node (first one)
    newnode->pNext = pHead;
    cout << "pHead: " << pHead << "\n";

    //since we are adding the new node to the beginning of the
    //list, we set the last node pointer to 0, since there is
    //no last node to point to.
    newnode->pLast = 0;

    //cout << "Got this far!\n";

    //now we set the next node's pLast pointer to the current node
    pHead->pLast = newnode;

    //make the head pointer point to the new node
    pHead = newnode;

    return;
}

void UpdateBlood()
{
    //We need to iterate through all the items in the blood
    //list. So, start with a pointer pointing at the beginning
    //of the linked list.
    Blood* pCurrent = pHead;

    //Iterate through the list until we find the last object
    //in the list. This will be the one with the null next pointer.
    while(pCurrent->pNext != 0)
    {
        //do all the stuff needed to update this particle
        pCurrent->x = pCurrent->x + pCurrent->xvel;

        pCurrent->life = pCurrent->life - 1;
        //and so on... (this includes drawing the particle)

        //TEST... display the value of the life
        cout << pCurrent->life << "\n";

        /*
        //If the life of the particle has expired, remove it
        //from the list.
        if(pCurrent->life < 0)
        {
            //Save the pointer to the next node
            Blood* next = pCurrent->pNext;
            //link the last node to the next node
            pCurrent->pLast->pNext = next;
            //Delete the current node
            delete pCurrent;
            //make the next node the current node
            pCurrent = next;

        }
        */
    }
    return;
}

int number = 0;

int main(int argc, char *argv[])
{

    //Test the linked list
    while(1)
    {
        cin >> number;
        if(number < 0) break;
        for(int i=1; i<=number; i++)
        {
            CreateBlood(5,5,0,0);
        }

        //break;

        UpdateBlood();



    }


    return 0;
}
But it crashes at line 43:

Code: Select all

pHead->pLast = newnode;
and probably other places as well if it ever got that far through the code.

If you know whats wrong with this or if you know an EASIER way of doing this kinda thing.... please inform me.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

C++: Linked Lists

Post by Falco Girgis »

I've noticed that everywhere that you used that Rnd() monstrosity in your BlitzPlus version is now just Rand().

Let's not forget that the power of rand() is infinate here. It returns a double, right. I think you can just cast it to a float. Also, I noticed that you were giving rand() parameters. It's power needs no parameters. So like if -3 is the minimum and +3 is the maximum, and you want a float, you'd do this:

Code: Select all

variable = (float)rand()%-6 + 3;
OMFG! t3h power of Rand()!
:bow: :worship:

EDIT:
Actually, what I did up there was illegal. Who can tell me why? Yeah, that's right, the modulus operator only works with ints. When you make it a float you might get shot for trying to mod it.

So thanks to Tvspelsfreak, you can do this to get a random float between -3 and 3:

Code: Select all

variable = (rand()%600-300)/100.00
Last edited by Falco Girgis on Sun Oct 03, 2004 8:42 pm, edited 2 times in total.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

JS Lemming wrote:CURSES!!! I needed to have a way of iterating through class instances and beable to add an instance to the begining of the list and beable to remove an instance anywhere in the list...
Vectors... veeeectooorrrsss..... See the sticky in this forummmm.....
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

MarauderIIC is t3h vect0rz whore~!!!11one
Post Reply