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

 C Programming:
[pastacode lang=”c” manual=”struct%20node%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20node%20*next%3B%0A%7D%3B” message=”” highlight=”” provider=”manual”/]

Explain the functionality of following C functions.

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

[pastacode lang=”c” manual=”void%20fun1(struct%20node*%20head)%0A%7B%0A%20%20if(head%20%3D%3D%20NULL)%0A%20%20%20%20return%3B%0A%20%20%0A%20%20fun1(head-%3Enext)%3B%0A%20%20printf(%22%25d%20%20%22%2C%20head-%3Edata)%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

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 ?

[pastacode lang=”c” manual=”void%20fun2(struct%20node*%20head)%0A%7B%0A%20%20if(head%3D%3D%20NULL)%0A%20%20%20%20return%3B%0A%20%20printf(%22%25d%20%20%22%2C%20head-%3Edata)%3B%20%0A%20%0A%20%20if(head-%3Enext%20!%3D%20NULL%20)%0A%20%20%20%20fun2(head-%3Enext-%3Enext)%3B%0A%20%20printf(%22%25d%20%20%22%2C%20head-%3Edata)%3B%20%20%20%0A%7D” message=”” highlight=”” provider=”manual”/]

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 Programming:

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20A%20linked%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20node%20*next%3B%0A%7D%3B%0A%20%0A%20%0A%2F*%20Prints%20a%20linked%20list%20in%20reverse%20manner%20*%2F%0Avoid%20fun1(struct%20node*%20head)%0A%7B%0A%20%20if(head%20%3D%3D%20NULL)%0A%20%20%20%20return%3B%0A%20%0A%20%20fun1(head-%3Enext)%3B%0A%20%20printf(%22%25d%20%20%22%2C%20head-%3Edata)%3B%0A%7D%0A%20%0A%2F*%20prints%20alternate%20nodes%20of%20a%20Linked%20List%2C%20first%20%0A%20%20from%20head%20to%20end%2C%20and%20then%20from%20end%20to%20head.%20*%2F%0Avoid%20fun2(struct%20node*%20start)%0A%7B%0A%20%20if(start%20%3D%3D%20NULL)%0A%20%20%20%20return%3B%0A%20%20printf(%22%25d%20%20%22%2C%20start-%3Edata)%3B%20%0A%20%0A%20%20if(start-%3Enext%20!%3D%20NULL%20)%0A%20%20%20%20fun2(start-%3Enext-%3Enext)%3B%0A%20%20printf(%22%25d%20%20%22%2C%20start-%3Edata)%3B%0A%7D%0A%20%0A%2F*%20UTILITY%20FUNCTIONS%20TO%20TEST%20fun1()%20and%20fun2()%20*%2F%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%0A%20%20of%20a%20list%20and%20an%20int%2C%20push%20a%20new%20node%20on%20the%20front%0A%20%20of%20the%20list.%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%2F*%20allocate%20node%20*%2F%0A%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%20%0A%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%20%0A%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%20%0A%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%20%0A%2F*%20Drier%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20struct%20node*%20head%20%3D%20NULL%3B%0A%20%0A%20%20%2F*%20Using%20push()%20to%20construct%20below%20list%0A%20%20%20%201-%3E2-%3E3-%3E4-%3E5%20%20*%2F%0A%20%20push(%26head%2C%205)%3B%0A%20%20push(%26head%2C%204)%3B%0A%20%20push(%26head%2C%203)%3B%0A%20%20push(%26head%2C%202)%3B%0A%20%20push(%26head%2C%201)%3B%20%20%20%0A%20%20%0A%20%20printf(%22%5Cn%20Output%20of%20fun1()%20for%20list%201-%3E2-%3E3-%3E4-%3E5%20%5Cn%22)%3B%0A%20%20fun1(head)%3B%0A%20%0A%20%20printf(%22%5Cn%20Output%20of%20fun2()%20for%20list%201-%3E2-%3E3-%3E4-%3E5%20%5Cn%22)%3B%20%0A%20%20fun2(head)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]