0

How to input size of the list for this code?

#include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node *next; } node; node *newNode(int data) { node *new_node = (node *) malloc(sizeof(node)); new_node->data = data; new_node->next = NULL; return new_node; } node *insert_node(node *root, int data) { if (root == NULL) return newNode(data); else { node *cur; cur = insert_node(root->next, data); root->next = cur; } return root; } void print(node *np) { if (np) { printf("(%d)", np->data); print(np->next); } } int main() { int T = 5; node *root = NULL; while (T-- > 0) { int r = rand() % 100; root = insert_node(root, r); } print(root); printf("\n"); return 0; }

14th Oct 2021, 1:40 PM
Jessa Mae V Gonzales
Jessa Mae V Gonzales - avatar
1 Odpowiedź
0
In main(), instead of int T = 5; You can input T from the console by using scanf(): int T; scanf("%d", &T);
14th Oct 2021, 2:28 PM
Brian
Brian - avatar