Corning Community College
CSCS2320 Data Structures
With a functioning breakout game, replace the malloc'ed array of structs with a singly-linked list of nodes, preserving the same overall functionality.
You will want to go here to edit and fill in the various sections of the document:
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.
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:
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;
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.
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
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!
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.
To be successful in this project, the following criteria (or their equivalent) must be met:
Let's say you have completed work on the project, and are ready to submit, you would do the following:
lab46:~/src/SEMESTER/DESIG/PROJECT$ submit DESIG PROJECT file1 file2 file3 ... fileN
You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.
I'll be evaluating the project based on the following criteria:
39:ttb1:final tally of results (39/39) *:ttb1:functional breakout game [13/13] *:ttb1:linked list implemented and used [26/26]