Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <cs50.h>
- typedef struct node // define our own custom struct for each node in the linked list
- {
- int number;
- struct node *next;
- }
- node;
- node* create_node(int); // create node handles the malloc line, checking if it's not null and returns the completed node
- void add_node(node *list, node *n); // And add node loops through the list until it reaches the end and adds the new node to that point
- void print_list(node *list);
- void free_memory(node *list);
- int main(void)
- {
- int input = get_int("Please enter how many nodes you would like to create in the linked list? \n");
- node *list = NULL; // create the list pointer variable, that will anticipate to be pointed to something of a "node" datatype, initalize it to NULL asits not pointing to anything
- node *n = malloc(sizeof(node)); // malloc() "node 2" in our memory + point out *n pointer to this node
- if (n != NULL) // saftey check to ensure n is not pointing to a NULL value in our memory
- {
- n->number = 0; // set the number field of the node our "*n" is currently pointing to: 2
- n->next = NULL; // set the next field of the node our "*n" is currently pointing to: NULL
- }
- list = n; // point our "*list" pointer to the same node our "*n" pointer is pointing to (at the start of this program list will be pointing to the head of the linked list)
- for (int i = 1; i < input; i++) // start counting i at 1 since we declared the first node as "0"
- {
- n = create_node(i); // use creat_node to create a new node + also take the pointer to a node this function returns + assign it back to n
- add_node(list, n);
- }
- print_list(list);
- free_memory(list);
- return 0;
- }
- node* create_node(int i) // our function to generate a node with the number we got from "i' in main()" "node 0", then "node 1" , then "node 2" etc
- {
- node *n = malloc(sizeof(node)); // a new n pointer that will malloc "node i" in memory + point our "*n" pointer to this node
- if (n != NULL) // saftey check to ensure n is not pointing to a NULL value in our memory
- {
- n->number = i; // set the number field of the node our "*n_2" is currently pointing to: whatever "i" is (this draws a line in our linked list)
- n->next = NULL; // set the next field of the node our "*n" is currently pointing to: NULL
- }
- return n;
- }
- void add_node(node *list, node *n) // takes in paramaters for the 1. "list" pointer (that is anticipating to point to a node) and the "n" pointer (to point to the "n" node itself)
- {
- node *tmp = list; // create a temp pointer, that will anticipate to be poiinted to something of a "node" datatype, and point it to where *tmp is pointing to, drawing a link
- while (tmp->next != NULL) // every time when looping through the linked list, if the current node tmp is sitting on isn't NULL
- {
- tmp = tmp->next; // then migrate the temp pointer to the next node in the linked list
- }
- tmp->next = n; // then, set tmp->next field in this current node to = n, to point to where *n is pointing to, drawing another link between the temp node and node n after it.
- }
- void print_list(node *list) // takes in paramater for the list pointer (that is anticipating to point to a node)
- {
- printf("Numbers in linked list:\n");
- node *tmp = list; // make a temp pointer that will point to the head of the linked list
- if (list == NULL) // if the list pointer variable pointing to the head of thelinked list is NULL
- {
- printf("The linked list is empty\n");
- }
- else
- {
- while (tmp != NULL) // keep performing this while loop until temp is not longer pointing to a node after in the linked list
- {
- printf("%d\n", tmp->number); // print out the current nodes.number where tmp is pointing to
- tmp = tmp->next; // then after this, update temp to point to the next node in the linked list to perform this action again
- }
- }
- }
- void free_memory(node *list)
- {
- node* tmp = list;
- while (list != NULL)
- {
- tmp = list;
- list = list->next;
- free(tmp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement