Knowee
Questions
Features
Study Tools

void myFun(Node* head) {Node* current = head;while (current != nullptr) {if((current->data)%2==0)std::cout << current->data << " "; current = current->next;}std::cout << std::endl; }Output of above code in linked list

Question

void myFun(Node* head) {Node* current = head;while (current != nullptr) {if((current->data)%2==0)std::cout << current->data << " "; current = current->next;}std::cout << std::endl; }Output of above code in linked list

🧐 Not the exact question you are looking for?Go ask a question

Solution

The provided code is a function in C++ that traverses a linked list and prints out the data of each node that contains an even number.

Here's a step-by-step explanation:

  1. The function myFun is defined with a single parameter head which is a pointer to the first node of a linked list.

  2. A new pointer current is initialized to point to the head of the linked list.

  3. A while loop is started which continues until current becomes nullptr, i.e., until we have traversed all nodes in the linked list.

  4. Inside the loop, an if condition checks whether the data in the current node is even. This is done by taking the modulo of the data with 2 (current->data % 2) and checking if it equals 0.

  5. If the data is even, it is printed to the standard output followed by a space.

  6. The current pointer is then moved to the next node in the list (current = current->next).

  7. Once the loop has traversed all nodes, a newline character is printed to the standard output

This problem has been solved

Similar Questions

A linked list whose last node points back to both the head node and the previous node instead of containing the null pointer ____________

What is the output of the following function for a single linked list with the values1 -> 2 -> 3 -> 4 -> 5-> 6? start is pointing to the first node in the list.void fun(struct node *start){if (start == NULL) return;printf(“%d”, start->data);if (start-> next != NULL){fun (start-> next -> next);printf(“%d”, start->data);}}

In a singly linked list, the last node references to : a) Head b) NULL c) Next node d) None of the above

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its attribute, insert this node at the desired position and return the head node.A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.Example refers to the first node in the list Insert a node at position with . The new list is Function Description Complete the function insertNodeAtPosition in the editor below. It must return a reference to the head node of your finished list.insertNodeAtPosition has the following parameters:head: a SinglyLinkedListNode pointer to the head of the listdata: an integer value to insert as data in your new nodeposition: an integer position to insert the new node, zero based indexingReturnsSinglyLinkedListNode pointer: a reference to the head of the revised listInput FormatThe first line contains an integer , the number of elements in the linked list.Each of the next lines contains an integer SinglyLinkedListNode[i].data.The next line contains an integer , the data of the node that is to be inserted.The last line contains an integer .Constraints, where is the element of the linked list..Sample Input31613712Sample Output16 13 1 7ExplanationThe initial linked list is . Insert at the position which currently has in it. The updated linked list is .

In a doubly linked list, what’s the “head” of a linked list?It’s the node with the pointer to the previous node equals to NULLIt’s the node with the pointer to the next node equals to NULLI don't know

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.