User Tools

Site Tools


notes:data:fall2023:projects:ttb1

This is an old revision of the document!


TTB1

NODE STRUCT

What is a node struct? A node struct is used to represent an element, in our case, a brick, within a list. The brick structure is mostly unchanged since ttb0, however there is now the addition of the next pointer, which makes it a node struct.

Here's an example of our brick structure with the node struct:

struct BrickNode
{
    bool Active;
    int X, Y;          // Position on screen
    int Width, Height; // Size of hitbox for brick
    BrickNode* next;   // Pointer to the next brick node
};

The purpose of the next pointer is to be the connective tissue that holds our list together. Without it, we will not be able to access the following items in the list.

NODE POINTER

MALLOC A NODE

What is meant by malloc a node? while it is when you dynamically allocate memory and create a new node in a list.

here is an example of what it could possibly look like:

// Creates a new node for each brick
BrickNode* newBrick = (BrickNode*)malloc(sizeof(BrickNode));

Here is a breakdown of what is happening on the left side of the equal sign:

 BrickNode* newBrick 

This declares a pointer variable called newBrick which is of the type BrickNode.

LINKING NODES TOGETHER

Format for linking nodes together:

newBrick->next = BrickList;

This line of code is linking the new brick node into the list. here's how it works:

newBrick

which is a node for each brick, is set to point to the current head of the list essentially putting a new node at the beginning of the list. This is done with

->next = BrickList

TRAVERSING LIST

INSERTING NODE INTO LIST

AT START OF LIST

IN THE MIDDLE OF THE LIST

APPENDING NODE INTO LIST

AT END OF LIST

IN THE MIDDLE OF THE LIST

REMOVING A NODE FROM THE LIST

AT THE START OF LIST

IN THE MIDDLE OF THE LIST

AT END OF LIST

notes/data/fall2023/projects/ttb1.1694809959.txt.gz · Last modified: 2023/09/15 20:32 by walley