Consider the following C program that creates and traverses a singly linked list. What will be the output?
Consider the following C program that creates and traverses a singly linked list. What will be the output?
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
struct Node *head, *second, *third;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 5;
head->next = second;
second->data = 10;
second->next = third;
third->data = 15;
third->next = NULL;
struct Node *ptr = head;
while (ptr != NULL) {
printf("%d ", ptr->data);
ptr = ptr->next->next;
}
return 0;
}
Show answerHide answer
Correct answer: (D) Segmentation fault (runtime error)