# Practice questions for Linked List and Recursion

Assume the structure of a Linked List node is as follows.

 `struct` `Node ` `{ ` `  ``int` `data; ` `  ``struct` `Node *next; ` `}; `

Explain the functionality of following C functions.

1. What does the following function do for a given Linked List?

 `void` `fun1(``struct` `Node* head) ` `{ ` `  ``if``(head == NULL) ` `    ``return``; ` `  `  `  ``fun1(head->next); ` `  ``printf``(``"%d  "``, head->data); ` `} `

fun1() prints the given Linked List in reverse manner. For Linked List 1->2->3->4->5, fun1() prints 5->4->3->2->1.

2. What does the following function do for a given Linked List ?

 `void` `fun2(``struct` `Node* head) ` `{ ` `  ``if``(head== NULL) ` `    ``return``; ` `  ``printf``(``"%d  "``, head->data);  ` ` `  `  ``if``(head->next != NULL ) ` `    ``fun2(head->next->next); ` `  ``printf``(``"%d  "``, head->data);    ` `} `

fun2() prints alternate nodes of the given Linked List, first from head to end, and then from end to head. If Linked List has even number of nodes, then fun2() skips the last node. For Linked List 1->2->3->4->5, fun2() prints 1 3 5 5 3 1. For Linked List 1->2->3->4->5->6, fun2() prints 1 3 5 5 3 1.

Below is a complete running program to test above functions.

## C++

 `#include ` `using` `namespace` `std; ` ` `  `/* A linked list node */` `class` `Node  ` `{  ` `    ``public``: ` `    ``int` `data;  ` `    ``Node *next;  ` `};  ` ` `  ` `  `/* Prints a linked list in reverse manner */` `void` `fun1(Node* head)  ` `{  ` `    ``if``(head == NULL)  ` `        ``return``;  ` `     `  `    ``fun1(head->next);  ` `    ``cout << head->data << ``" "``;  ` `}  ` ` `  `/* prints alternate nodes of a Linked List, first  ` `from head to end, and then from end to head. */` `void` `fun2(Node* start)  ` `{  ` `    ``if``(start == NULL)  ` `        ``return``;  ` `    ``cout<data<<``" "``;  ` `     `  `    ``if``(start->next != NULL )  ` `        ``fun2(start->next->next);  ` `    ``cout << start->data << ``" "``;  ` `}  ` ` `  `/* UTILITY FUNCTIONS TO TEST fun1() and fun2() */` `/* Given a reference (pointer to pointer) to the head  ` `of a list and an int, push a new node on the front  ` `of the list. */` `void` `push(Node** head_ref, ``int` `new_data)  ` `{  ` `    ``/* allocate node */` `    ``Node* new_node = ``new` `Node(); ` `     `  `    ``/* put in the data */` `    ``new_node->data = new_data;  ` `     `  `    ``/* link the old list off the new node */` `    ``new_node->next = (*head_ref);  ` `     `  `    ``/* move the head to point to the new node */` `    ``(*head_ref) = new_node;  ` `}  ` ` `  `/* Driver code */` `int` `main()  ` `{  ` `    ``/* Start with the empty list */` `    ``Node* head = NULL;  ` `     `  `    ``/* Using push() to construct below list  ` `        ``1->2->3->4->5 */` `    ``push(&head, 5);  ` `    ``push(&head, 4);  ` `    ``push(&head, 3);  ` `    ``push(&head, 2);  ` `    ``push(&head, 1);  ` `     `  `    ``cout<<````"Output of fun1() for list 1->2->3->4->5 "````;  ` `    ``fun1(head);  ` `     `  `    ``cout<<````" Output of fun2() for list 1->2->3->4->5 "````;  ` `    ``fun2(head);  ` ` `  `    ``return` `0;  ` `}  ` ` `  `// This code is contributed by rathbhupendra `

## C

 `#include ` `#include ` ` `  `/* A linked list node */` `struct` `Node ` `{ ` `  ``int` `data; ` `  ``struct` `Node *next; ` `}; ` ` `  ` `  `/* Prints a linked list in reverse manner */` `void` `fun1(``struct` `Node* head) ` `{ ` `  ``if``(head == NULL) ` `    ``return``; ` ` `  `  ``fun1(head->next); ` `  ``printf``(``"%d  "``, head->data); ` `} ` ` `  `/* prints alternate nodes of a Linked List, first  ` `  ``from head to end, and then from end to head. */` `void` `fun2(``struct` `Node* start) ` `{ ` `  ``if``(start == NULL) ` `    ``return``; ` `  ``printf``(``"%d  "``, start->data);  ` ` `  `  ``if``(start->next != NULL ) ` `    ``fun2(start->next->next); ` `  ``printf``(``"%d  "``, start->data); ` `} ` ` `  `/* UTILITY FUNCTIONS TO TEST fun1() and fun2() */` `/* Given a reference (pointer to pointer) to the head ` `  ``of a list and an int, push a new node on the front ` `  ``of the list. */` `void` `push(``struct` `Node** head_ref, ``int` `new_data) ` `{ ` `  ``/* allocate node */` `  ``struct` `Node* new_node = ` `          ``(``struct` `Node*) ``malloc``(``sizeof``(``struct` `Node)); ` `  `  `  ``/* put in the data  */` `  ``new_node->data  = new_data; ` `  `  `  ``/* link the old list off the new node */` `  ``new_node->next = (*head_ref); ` `  `  `  ``/* move the head to point to the new node */` `  ``(*head_ref)    = new_node; ` `} ` `  `  `/* Drier program to test above functions */` `int` `main() ` `{ ` `  ``/* Start with the empty list */` `  ``struct` `Node* head = NULL; ` ` `  `  ``/* Using push() to construct below list ` `    ``1->2->3->4->5  */` `  ``push(&head, 5); ` `  ``push(&head, 4); ` `  ``push(&head, 3); ` `  ``push(&head, 2); ` `  ``push(&head, 1);    ` `  `  `  ``printf``(````" Output of fun1() for list 1->2->3->4->5 "````); ` `  ``fun1(head); ` ` `  `  ``printf``(````" Output of fun2() for list 1->2->3->4->5 "````);  ` `  ``fun2(head); ` `         `  `  ``getchar``(); ` `  ``return` `0; ` `} `

Output:

``` Output of fun1() for list 1->2->3->4->5
5 4 3 2 1
Output of fun2() for list 1->2->3->4->5
1 3 5 5 3 1 ```

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.