This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:data:fall2023:projects:ttb1 [2023/09/15 20:03] – [MALLOC A NODE] wgates1 | notes: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 a structure | + | Here's an example of our brick structure |
<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 | ||
}; | }; | ||
- | </ | + | </ |
=====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 " | ||
+ | * One Pointer that points towards whatever is the last node currently in the list. We'll call it " | ||
+ | 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; | ||
+ | </ | ||
=====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 27: | Line 35: | ||
</ | </ | ||
- | Here is a breakdown of what is happening | + | Here is a breakdown of what is happening |
<code C> BrickNode* newBrick </ | <code C> BrickNode* newBrick </ | ||
This declares a pointer variable called newBrick which is of the type BrickNode. | This declares a pointer variable called newBrick which is of the type BrickNode. | ||
Line 42: | Line 50: | ||
<code C> | <code C> | ||
=====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 '' | ||
+ | If your list is set up properly, ``temp-> | ||
+ | <code C>temp = temp-> | ||
+ | <color # | ||
=====INSERTING NODE INTO LIST===== | =====INSERTING NODE INTO LIST===== | ||
Line 57: | 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, | ||
+ | Node* current =*start-of-list; | ||
+ | Node* previous= NULL; | ||
+ | while(current!=NULL){ | ||
+ | if (current_node-> | ||
+ | if(previous==NULL){ | ||
+ | //node we want to delete from the list is the first node in the list | ||
+ | *start-of-list =current-> | ||
+ | } else{ | ||
+ | //set the previous node's pointer to the current node's pointer | ||
+ | } | ||
+ | free(current) | ||
+ | return; | ||
+ | } | ||
+ | //itterate to next brick | ||
+ | previous= current; | ||
+ | current=current-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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==== | ||