Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node
- {
- char *data;
- struct node *next;
- }
- node;
- int main (void)
- {
- node *list = NULL;
- // adding the first node
- node *n = malloc(sizeof(node));
- n->data = "First Node";
- n->next = NULL;
- list = n;
- // adding the second node
- n = malloc(sizeof(node));
- n->data = "Second Node";
- n->next = list;
- list = n;
- // adding the third node
- n = malloc(sizeof(node));
- n->data = "Third Node";
- n->next = list;
- list = n;
- // printing the linked list data
- node *current_node = list;
- while (current_node != NULL)
- {
- printf("Current node data: %s\n", current_node->data);
- current_node = current_node->next;
- }
- // freeing the memory after done with list
- current_node = list;
- while (current_node != NULL)
- {
- node *next_node = current_node->next;
- free(current_node);
- current_node = next_node;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement