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 1 3 51 2 3 51 3 5 5 3 1
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 1 3 51 2 3 51 3 5 5 3 1
Solution
The output of the function for the given linked list 1->2->3->4->5->6 will be 1 4 6 6 4 1.
Here's how:
-
The function first checks if the start node is NULL. If it is, the function returns. If it isn't, it prints the data of the start node.
-
Then, it checks if the next node of the start node is not NULL. If it isn't, it recursively calls the function with the node two steps ahead of the current start node.
-
After the recursive call, it prints the data of the start node again.
So, for the given linked list, the function will work as follows:
-
It will print 1 (the data of the first node), then move two steps ahead to node with data 3.
-
It will print 4 (the data of the node two steps ahead of node 3), then move two steps ahead to node with data 6.
-
It will print 6 (the data of the node two steps ahead of node 4), then try to move two steps ahead but there are no more nodes, so it returns.
-
After returning from the recursive call, it will print 6 and 4 again (the data of the nodes it visited in the recursive call).
-
Finally, it will print 1 again (the data of the first node it visited).
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.