User Tools

Site Tools


notes:data:fall2023:projects:ttb1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
notes:data:fall2023:projects:ttb1 [2023/09/15 19:32] – [NODE STRUCT] wgates1notes:data:fall2023:projects:ttb1 [2023/09/16 20:50] (current) – [REMOVING A NODE FROM THE LIST] jwieland
Line 3: Line 3:
 =====NODE STRUCT===== =====NODE STRUCT=====
 What is a node struct?  What is a node struct? 
-A node struct is used to represent an element, in our case, a brick, within a list. +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.
  
-An example of structure for a node is as follows:+Here's an example of our brick structure with the node struct:
 <code C> <code C>
 struct BrickNode struct BrickNode
Line 14: Line 14:
     BrickNode* next;   // Pointer to the next brick node     BrickNode* next;   // Pointer to the next brick node
 }; };
-</code>+</code> 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===== =====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:
 +<code C>
 +BrickNode* start;
 +BrickNode* temp;
 +</code>
 =====MALLOC A NODE===== =====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:
 +<code C>
 +// Creates a new node for each brick
 +BrickNode* newBrick = (BrickNode*)malloc(sizeof(BrickNode));
 +</code>
 +
 +Here is a breakdown of what is happening on the left side of the equal sign:
 +<code C> BrickNode* newBrick </code>
 +This declares a pointer variable called newBrick which is of the type BrickNode.
 +
  
 =====LINKING NODES TOGETHER===== =====LINKING NODES TOGETHER=====
 +Format for linking nodes together:
 +<code C>
 +newBrick->next = BrickList;
 +</code>
  
 +This line of code is linking the new brick node into the list. 
 +here's how it works:
 +<code C>newBrick</code>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 <code c>->next = BrickList</code>
 =====TRAVERSING LIST===== =====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:
 +<code C>temp = temp->next;</code>
 +<color #ed1c24>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!</color>
 =====INSERTING NODE INTO LIST===== =====INSERTING NODE INTO LIST=====
  
Line 38: Line 69:
 =====REMOVING A NODE FROM 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.
 +
 +<code c>
 +  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;
 +  }
 +  }
 +</code>
 +
 +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==== ====AT THE START OF LIST====
  
notes/data/fall2023/projects/ttb1.1694806330.txt.gz · Last modified: 2023/09/15 19:32 by wgates1