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 20:36] – [NODE POINTER] walleynotes:data:fall2023:projects:ttb1 [2023/09/16 20:50] (current) – [REMOVING A NODE FROM THE LIST] jwieland
Line 20: Line 20:
   * One Pointer that always points towards the very start of the list. We'll call it "start" for these examples.   * 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."   * 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. What is meant by malloc a node? while it is when you dynamically allocate memory and create a new node in a list.
Line 44: Line 50:
 <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> <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 59: 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.1694810217.txt.gz · Last modified: 2023/09/15 20:36 by walley