{"id":27618,"date":"2018-04-04T19:34:18","date_gmt":"2018-04-04T14:04:18","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27618"},"modified":"2018-04-04T19:34:18","modified_gmt":"2018-04-04T14:04:18","slug":"practice-questions-linked-list-recursion","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/practice-questions-linked-list-recursion\/","title":{"rendered":"Practice questions for Linked List and Recursion"},"content":{"rendered":"<p>Assume the structure of a Linked List node is as follows.<span id=\"more-6803\"><\/span><\/p>\n<div><strong>\u00a0C Programming:<\/strong><\/div>\n<div>\n[pastacode lang=\u201dc\u201d manual=\u201dstruct%20node%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20node%20*next%3B%0A%7D%3B\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Explain the functionality of following C functions.<\/p>\n<p><strong>1. What does the following function do for a given Linked List?<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201dvoid%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>fun1() prints the given Linked List in reverse manner. For Linked List 1->2->3->4->5, fun1() prints 5->4->3->2->1.<\/p>\n<p><strong>2. What does the following function do for a given Linked List ?<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201dvoid%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>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.<\/p>\n<p>Below is a complete running program to test above functions.<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Practice questions for Linked List and Recursion &#8211; Linked List &#8211; Assume the structure of a Linked List node is as follows.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81834,81838,81836,81832,81833,81839,81837,79580,81840,81835],"class_list":["post-27618","post","type-post","status-publish","format-standard","hentry","category-linked-list","category-singly-linked-list","tag-check-if-a-singly-linked-list-is-palindrome","tag-linked-list-exercises","tag-linked-list-interview-questions-and-answers-pdf","tag-linked-list-interview-questions-in-c","tag-linked-list-interview-questions-java","tag-linked-list-practice-problems-java","tag-linked-list-problems-and-solutions","tag-linked-list-problems-in-c","tag-linked-list-problems-pdf","tag-linked-list-questions-in-data-structure"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27618"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27618\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}