3. What will be the output of the following code snippet after inserting a node with value 5 at the end of the doubly linked list 1 <-> 3 <-> 4?
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void insertEnd(struct Node** head, int val) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
void printList(struct Node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
}
int main() {
struct Node* head = malloc(sizeof(struct Node));
head->data = 1; head->prev = NULL;
struct Node* second = malloc(sizeof(struct Node));
second->data = 3; second->prev = head; head->next = second;
struct Node* third = malloc(sizeof(struct Node));
third->data = 4; third->prev = second; second->next = third; third->next = NULL;
insertEnd(&head, 5);
printList(head);
return 0;
}