Jun 22, 2024
malloc function in Cstruct node {
int data;
struct node *next;
};
struct node *head;
head = NULL; // Initially, the list is empty
malloc to allocate memory for nodes dynamically.struct node *new_node;
new_node = (struct node*) malloc(sizeof(struct node));
int data and 4 for pointer).scanf("%d", &new_node->data); // Insert data
new_node->next = NULL; // Initially, next is NULL
temp pointer to traverse (do not modify head).temp = head;
while(temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
while loop:
int choice = 1;
while(choice) {
// Code to create and link new node
printf("Do you want to continue? (1/0)");
scanf("%d", &choice);
}
int count = 0;
temp = head;
while(temp != NULL) {
count++;
temp = temp->next;
}
printf("Number of nodes: %d", count);