fundamentals comp1511 - programming · looping through a linked list loop by using the next pointer...
TRANSCRIPT
![Page 1: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/1.jpg)
COMP1511 - Programming Fundamentals
Term 3, 2019 - Lecture 15
![Page 2: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/2.jpg)
What did we learn last week?Memory
● Allocating memory for use beyond the scope of functions
Multiple File Projects
● C files, H files and including our own files
Linked Lists
● structs, pointers and malloc all together!
![Page 3: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/3.jpg)
What are we learning today?Linked Lists
● Continuing our work from last week● Insertion and Removal from Linked Lists● Cleaning up memory from a linked list● Finishing our Battle Royale example
![Page 4: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/4.jpg)
Recap - Memory AllocationKeeping variables available to us
● malloc() and sizeof() and also free()● These functions allow us to declare variables as pieces of memory● The memory is allocated to us and we access it via a pointer● Unlike normal variables, these are never cleaned up by {curly brackets}● We can keep using them until we don't need them● We can free() them explicitly
![Page 5: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/5.jpg)
Pointers and Structsstruct node *
A pointer that can aim at memory
struct node
A piece of memory that can store a node
Node in memory
Usually allocated by malloc()
A memory address that can aim at a node
Node in memory
struct node *n = malloc(sizeof(struct node));
![Page 6: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/6.jpg)
Recap - Multi-file ProjectsSeparating our code
● Header files (*.h)● Contain function declarations, but no running code● Implementation files (*.c)● Contain implementations of the Header's functions● We #include header files● This means the file that includes a header can't see the implementation, it
trusts that it will be given certain functionality
![Page 7: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/7.jpg)
Recap - Linked ListsA chain of identical structs to hold information
● Pointers to the same type of struct so they can be chained together
● Some kind of information stored in the struct
struct node { struct node *next; int data;}
![Page 8: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/8.jpg)
A Linked List
A program's memory (not to scale)
Node
Next
Data
Node
Next
Data
Node
Next
Data
Node
Next
DataA pointer to the first node
NULL
![Page 9: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/9.jpg)
Looping through a Linked ListLoop by using the next pointer
● We can jump to the next node by following the current node's next pointer
● We know we're at the end if the next pointer is NULL
// Loop through a list of nodes, printing out their datavoid printData(struct node *n) { while (n != NULL) { printf("%d\n", n->data); n = n->next; }}
![Page 10: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/10.jpg)
Battle Royale - our unfinished exampleUsing a Linked List to track the players in a game
● We started by adding players to the game● We were able to loop through and print the players in the game● We might want to control the order of the list, so we need to be able to
insert at a particular position● We also want to be able to find and remove players from the list if they're
knocked out of the round
![Page 11: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/11.jpg)
Where are we up to?What functionality do we have so far?
● createPlayer() makes a node● printPlayers() can loop through a list and print it out● We have code in our main that can build a list in a specific order● What if we want an ordered list?
![Page 12: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/12.jpg)
Inserting Nodes into a Linked ListLinked Lists allow you to insert nodes in between other nodes
● We can do this by simply aiming next pointers to the right places● We find two linked nodes that we want to put a node between● We take the next of the first node and point it at our new node● We take the next of the new node and point it at the second node
This is much less complicated with diagrams . . .
![Page 13: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/13.jpg)
Our Linked ListBefore we've tried to insert anything
A program's memory (not to scale)
First Node
Next
2
Second Node
Next
1NULLhead
![Page 14: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/14.jpg)
Create a nodeA new node is made, it's not connected to anything yet
A program's memory (not to scale)
First Node
Next
2
Second Node
Next
1NULLhead
New Node
Next
2
![Page 15: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/15.jpg)
Connect the new node to the second nodeAlter the next pointer on the New Node
A program's memory (not to scale)
First Node
Next
2
Second Node
Next
1NULLhead
New Node
Next
2
Copy First Node's Next
![Page 16: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/16.jpg)
Connect the first node to the new nodeAlter the next pointer on the First Node
A program's memory (not to scale)
First Node
Next
2
Second Node
Next
1NULLhead
New Node
Next
2
Set First Node's Next to New Node's address
![Page 17: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/17.jpg)
Code for insertion
// Create and insert a new node into a list after a given listNodestruct node *insert(struct node* listNode, char newName[]) { struct node *n = createNode(newName, NULL); if (listNode == NULL) { // List is empty, n becomes the only element in the list listNode = n; n->next = NULL; } else { n->next = listNode->next; listNode->next = n; } return listNode;}
![Page 18: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/18.jpg)
Inserting NodesWe can use insertion to have greater control of where nodes are put in a list
int main(void) { // create the list of players struct node *head = createNode("Marc", NULL); insert("AndrewB", head); insert("Tom", head); insert("Aang", head); insert("Sokka", head); printPlayers(head);
return 0;}
![Page 19: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/19.jpg)
Break TimeHomework - it's not real homework, just things that can inspire you
● AlphaGo Documentary (on Netflix)● I, Robot Short Stories (Isaac Asimov)● Snow Crash and The Cryptonomicon Novels (Neal Stephenson)● Human Resource Machine Game (on Steam, iOS and Android)● Space Alert Board Game (Vlaada Chvatil)
![Page 20: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/20.jpg)
Insertion with some conditionsWe can now insert into any position in a Linked List
● We can read the data in a node and decide whether we want to insert before or after it
● Let's insert our elements into our list based on alphabetical order● We're going to use a string.h function, strcmp() for this● strcmp() compares two strings, and returns
○ 0 if they're equal○ negative if the first has a lower ascii value than the second○ positive if the first has a higher ascii value than the second
![Page 21: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/21.jpg)
Finding a where to insertWe're going to loop through the list
● This loop assumes the list is already in alphabetical order● Each time we loop, we're going to keep track of where we've been● We'll test the name of each node using strcmp() ● We stop looping once we find the first name that's "higher" than ours● Then we insert before that node
![Page 22: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/22.jpg)
Finding the insertion point
Node
Next
A
Node
Next
C
Node
Next
E
Node
Next
SA pointer to the first node
NULL
D
Attempting to insert a node with data: "D" into a sorted list while maintaining the alphabetical order
loop stops here
Insert between these two nodes
loop starts here
![Page 23: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/23.jpg)
Inserting into a list Alphabeticallystruct node *insertAlphabetical(char newName[], struct node* head) { struct node *previous = NULL; struct node *n = head; // Loop through the list and find the right place for the new name while (n != NULL && strcmp(newName, n->name) > 0) { previous = n; n = n->next; } struct node *insertionPoint = insert(newName, previous); if (previous == NULL) { // we inserted at the start of the list insertionPoint->next = n; return insertionPoint; } else { return head; }}
![Page 24: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/24.jpg)
Removing a nodeIf we want to remove a specific node
● We need to look through the list and see if a node matches the one we want to remove
● To remove, we'll use next pointers to connect the list around the node● Then, we'll free the node itself that we don't need anymore
![Page 25: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/25.jpg)
Removing a nodeIf we want to remove the Second Node
A program's memory (not to scale)
First Node
Next
3
Third Node
Next
1NULLhead
Second Node
Next
2
![Page 26: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/26.jpg)
Skipping the nodeAlter the First Node's next to bypass the node we're removing
A program's memory (not to scale)
First Node
Next
3
Third Node
Next
1NULLhead
Second Node
Next
2
First Node bypasses Second Node
![Page 27: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/27.jpg)
Freeing the nodeFree the memory from the now bypassed node
A program's memory (not to scale)
First Node
Next
3
Third Node
Next
1NULLhead
Second Node
Next
2
Free this node
![Page 28: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/28.jpg)
Finding the node
struct node *removeNode(char name[], struct node* head) { struct node *previous = NULL; struct node *n = head; // Keep looping until we find the matching name while (n != NULL && strcmp(name, n->name) != 0) { previous = n; n = n->next; } if (n != NULL) { // if n isn't NULL, we found the right node
Loop until you find the right match
![Page 29: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/29.jpg)
Removing the node
if (n != NULL) { // if n isn't NULL, we found the right node if (previous == NULL) { // it's the first node head = n->next; } else { previous->next = n->next; } free(n);
} return head;}
Having found the node, remove it from the list
![Page 30: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/30.jpg)
The Battle Royale GameIn a Battle Royale, people are removed from the game one at a time until only one person is left. They are the winner
● We can create a list of players● We can make sure it's in a nice alphabetical order● We can remove a single player from the list● Now we need to remove players one at a time● When there's only one left, they are the winner!
![Page 31: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/31.jpg)
Game codeOnce our list is created, we can loop through the game
● We print out the player list (we might want to modify that function!)● Our user will tell us who was knocked out
// A game loop that runs until only one player is left while (printPlayers(head) > 1) { printf("Who just got knocked out?\n"); char koName[MAX_NAME_LENGTH]; fgets(koName, MAX_NAME_LENGTH, stdin); koName[strlen(koName) - 1] = '\0'; head = removeNode(koName, head); printf("----------\n"); } printf("The winner is: %s\n", head->name);
![Page 32: Fundamentals COMP1511 - Programming · Looping through a Linked List Loop by using the next pointer We can jump to the next node by following the current node's next pointer We know](https://reader034.vdocument.in/reader034/viewer/2022052612/5f0e30857e708231d43e09f1/html5/thumbnails/32.jpg)
What did we learn today?Linked Lists
● Inserting nodes at a specific location● Inserting nodes into an ordered list● Finding nodes using a while loop● Removing nodes