{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">struct node<br\/>{<br\/>  int data;<br\/>  struct node *next;<br\/>};<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">void fun1(struct node* head)<br\/>{<br\/>  if(head == NULL)<br\/>    return;<br\/>  <br\/>  fun1(head-&gt;next);<br\/>  printf(&quot;%d  &quot;, head-&gt;data);<br\/>}<\/code><\/pre> <\/div>\n<p>fun1() prints the given Linked List in reverse manner. For Linked List 1-&gt;2-&gt;3-&gt;4-&gt;5, fun1() prints 5-&gt;4-&gt;3-&gt;2-&gt;1.<\/p>\n<p><strong>2. What does the following function do for a given Linked List ?<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">void fun2(struct node* head)<br\/>{<br\/>  if(head== NULL)<br\/>    return;<br\/>  printf(&quot;%d  &quot;, head-&gt;data); <br\/> <br\/>  if(head-&gt;next != NULL )<br\/>    fun2(head-&gt;next-&gt;next);<br\/>  printf(&quot;%d  &quot;, head-&gt;data);   <br\/>}<\/code><\/pre> <\/div>\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-&gt;2-&gt;3-&gt;4-&gt;5, fun2() prints 1 3 5 5 3 1. For Linked List 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* A linked list node *\/<br\/>struct node<br\/>{<br\/>  int data;<br\/>  struct node *next;<br\/>};<br\/> <br\/> <br\/>\/* Prints a linked list in reverse manner *\/<br\/>void fun1(struct node* head)<br\/>{<br\/>  if(head == NULL)<br\/>    return;<br\/> <br\/>  fun1(head-&gt;next);<br\/>  printf(&quot;%d  &quot;, head-&gt;data);<br\/>}<br\/> <br\/>\/* prints alternate nodes of a Linked List, first <br\/>  from head to end, and then from end to head. *\/<br\/>void fun2(struct node* start)<br\/>{<br\/>  if(start == NULL)<br\/>    return;<br\/>  printf(&quot;%d  &quot;, start-&gt;data); <br\/> <br\/>  if(start-&gt;next != NULL )<br\/>    fun2(start-&gt;next-&gt;next);<br\/>  printf(&quot;%d  &quot;, start-&gt;data);<br\/>}<br\/> <br\/>\/* UTILITY FUNCTIONS TO TEST fun1() and fun2() *\/<br\/>\/* Given a reference (pointer to pointer) to the head<br\/>  of a list and an int, push a new node on the front<br\/>  of the list. *\/<br\/>void push(struct node** head_ref, int new_data)<br\/>{<br\/>  \/* allocate node *\/<br\/>  struct node* new_node =<br\/>          (struct node*) malloc(sizeof(struct node));<br\/>  <br\/>  \/* put in the data  *\/<br\/>  new_node-&gt;data  = new_data;<br\/>  <br\/>  \/* link the old list off the new node *\/<br\/>  new_node-&gt;next = (*head_ref);<br\/>  <br\/>  \/* move the head to point to the new node *\/<br\/>  (*head_ref)    = new_node;<br\/>}<br\/>  <br\/>\/* Drier program to test above functions *\/<br\/>int main()<br\/>{<br\/>  \/* Start with the empty list *\/<br\/>  struct node* head = NULL;<br\/> <br\/>  \/* Using push() to construct below list<br\/>    1-&gt;2-&gt;3-&gt;4-&gt;5  *\/<br\/>  push(&amp;head, 5);<br\/>  push(&amp;head, 4);<br\/>  push(&amp;head, 3);<br\/>  push(&amp;head, 2);<br\/>  push(&amp;head, 1);   <br\/>  <br\/>  printf(&quot;\\n Output of fun1() for list 1-&gt;2-&gt;3-&gt;4-&gt;5 \\n&quot;);<br\/>  fun1(head);<br\/> <br\/>  printf(&quot;\\n Output of fun2() for list 1-&gt;2-&gt;3-&gt;4-&gt;5 \\n&quot;); <br\/>  fun2(head);<br\/>         <br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\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}]}}