User Tools

Site Tools


notes:data:fall2023:projects:ttb1

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

In order to access our list in key areas, we need to create pointers that point towards specific brick structures in our list. For a singly-linked list, we need two pointers:

  • One Pointer that always points towards the very start of the list. We'll call it “start” for these examples.
  • One Pointer that points towards whatever is the last node currently in the list. We'll call it “temp.”

To create these pointers, we set them to the type of our brick structure. That way, after dereferencing the pointer, we'll be able to access any of the variables we assign inside our struct. The code looks like:

BrickNode* start;
BrickNode* temp;

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

In a singly-linked list, we can only move in one direction through the list — forward. To move through the list, we need to move our temp pointer to instead point to the next item within the list. This is where our next pointer within our BrickNode struct comes in handy.

If your list is set up properly, ``temp→next`` should be pointing towards another BrickNode structure or a NULL value (if you're at the end of the already-created list). Only traverse through the list if there's a known node to move to, otherwise you'll end up setting temp to NULL. And we don't want that. If all is good, the following command should move the pointer properly and you'll be looking at the next node:

temp = temp->next;

WARNING: Once you've moved the temp pointer forward, there is no way to move it backwards in a singly-linked list! You'll need to set it back to the start pointer at the start of the 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

One of the ways to delete a specific Node from a linked list is to create a function that searches for the node by its properties (e.g. x, and y) and then removes it. The psudo-code would look something like this.

  deleteNode(Node** start-of-list, int x, int y) {
  Node* current =*start-of-list;
  Node* previous= NULL;
  while(current!=NULL){
    if (current_node->x == x && current_node->y == y) {
      if(previous==NULL){
        //node we want to delete from the list is the first node in the list
        *start-of-list =current->next;
      } else{
        //set the previous node's pointer to the current node's pointer
      }
      free(current)
      return;
    }
    //itterate to next brick
    previous= current;
    current=current->next;
  }
  }

In essence we are itterating through the different nodes, and checking if they are the node we want to delete. Make sure to have a catch case for the first node in the list.

AT THE START OF LIST

IN THE MIDDLE OF THE LIST

AT END OF LIST

notes/data/fall2023/projects/ttb1.txt · Last modified: 2023/09/16 20:50 by jwieland