Starting the Linked List
To build a linked list we need to start with a struct that can be used to manage the list itself. This will contain a pointer to the start of the list, and another pointer that refers to the end of the list. The pointer to the start can be used when we need to access the nodes of the list, this tells us where to start. The pointer to the last node can be used when we want to add a new node to the end of the list.
When we start a new list, the start and end pointers both need to point to nothing (NULL
) as shown in the following image. With the linked list, the NULL
indicates there is no node at this location.
To build this we will need to create a struct for the node, and a struct for the linked list. Once again, this is something that Copilot has seen many times. So it can help us get this going.
Here are the comments I used as prompts for Copilot, and the code after I cleaned it up a litte.
Creating a new linked list
With this in place Copilot picked up that I probably wanted a new_linked_list
function next. The suggestion even added the comments. If not, then you can add prompts to get it going in the right direction.