What is the output of following function for start pointing to first node of following linked list?1->2->3->4->5->6void fun(struct node* start){ if(start == NULL) return; printf("%d ", start->data); if(start->next != NULL ) fun(start->next->next); printf("%d ", start->data);}1 4 6 6 4 11 3 5 5 3 11 3 5 1 3 51 2 3 5
Question
What is the output of following function for start pointing to first node of following linked list?1->2->3->4->5->6void fun(struct node* start){ if(start == NULL) return; printf("%d ", start->data); if(start->next != NULL ) fun(start->next->next); printf("%d ", start->data);}1 4 6 6 4 11 3 5 5 3 11 3 5 1 3 51 2 3 5
Solution
The function fun is a recursive function that prints the data of the nodes in a linked list. It first checks if the start node is NULL, if it is, it returns without doing anything. If it's not NULL, it prints the data of the start node. Then it checks if the next node is not NULL, if it's not, it calls itself with the node two steps ahead. After the recursive call, it prints the data of the start node again.
The linked list given is 1->2->3->4->5->6.
Let's go through the function step by step:
-
The start node is
1, it's notNULL, so it prints1. The next node is2, it's notNULL, so it calls itself with the node3. -
Now the start node is
3, it's notNULL, so it prints3. The next node is4, it's notNULL, so it calls itself with the node5. -
Now the start node is
5, it's notNULL, so it prints5. The next node is6, it's notNULL, so it calls itself withNULL(since there's no node two steps ahead). -
Now the start node is
NULL, so it returns without doing anything. -
After the recursive call, it prints the data of the start node
5again. -
It goes back to the previous call, prints the data of the start node
3again. -
It goes back to the first call, prints the data of the start node
1again.
So the output of the function for the given linked list is 1 3 5 5 3 1.
Similar Questions
What will be the output of the following code snippet for the list 1->2->3->4->5->6?void solve(struct node* start){if(start == NULL)return;printf("%d ", start->data);if(start->next != NULL )solve(start->next->next);printf("%d ", start->data);}*2 points1 2 3 4 5 61 3 5 5 3 61 3 5 1 3 52 4 6 1 3 5
The following C function takes a single-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the function completes execution?struct node{ int value; struct node *next;};void rearrange(struct node *list){ struct node *p, * q; int temp; if ((!list) || !list->next) return; p = list; q = list->next; while(q) { temp = p->value; p->value = q->value; q->value = temp; p = q->next; q = p?p->next:0; }}1,2,3,4,5,6,72,1,4,3,6,5,72,3,4,5,6,7,11,3,2,5,4,7,6
How is the last node identified in a doubly linked list?It points to `None`.It has a special flag.It has a reference to the first node.It has a pointer to the previous node.
In a doubly linked list, what’s the “tail” of a linked list?It’s the node with the pointer to the previous node equal to NULLIt’s the node with the pointer to the next node equal to NULL
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
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.