Knowee
Questions
Features
Study Tools

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

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

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:

  1. The start node is 1, it's not NULL, so it prints 1. The next node is 2, it's not NULL, so it calls itself with the node 3.

  2. Now the start node is 3, it's not NULL, so it prints 3. The next node is 4, it's not NULL, so it calls itself with the node 5.

  3. Now the start node is 5, it's not NULL, so it prints 5. The next node is 6, it's not NULL, so it calls itself with NULL (since there's no node two steps ahead).

  4. Now the start node is NULL, so it returns without doing anything.

  5. After the recursive call, it prints the data of the start node 5 again.

  6. It goes back to the previous call, prints the data of the start node 3 again.

  7. It goes back to the first call, prints the data of the start node 1 again.

So the output of the function for the given linked list is 1 3 5 5 3 1.

This problem has been solved

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

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.