#include <stdio.h> #include <stdlib.h> struct node { int data; struct node* next; }; int main() { int n, i; struct node *head = NULL, *new_node, *current_node; printf("Enter the number of nodes: "); scanf("%d", &n); for (i = 1; i <= n; i++) { new_node = (struct node*) malloc(sizeof(struct node)); printf("Enter the data for node %d: ", i); scanf("%d", &new_node->data); if (head == NULL) { head = new_node; current_node = head; } else { current_node->next = new_node; current_node = new_node; } } current_node = head; printf("The linked list is:\n"); while (current_node != NULL) { printf("%d -> ", current_node->data); current_node = current_node->next; } printf("NULL\n"); current_node = head; while (current_node != NULL) { struct node* temp = current_node; current_node = current_node->next; free(temp); } return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter