{"id":26979,"date":"2017-12-28T21:54:34","date_gmt":"2017-12-28T16:24:34","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26979"},"modified":"2017-12-28T21:54:34","modified_gmt":"2017-12-28T16:24:34","slug":"write-function-get-nth-node-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/write-function-get-nth-node-linked-list\/","title":{"rendered":"C Programming &#8211; Write a function to get Nth node in a Linked List"},"content":{"rendered":"<p>Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position.<br \/>\n<span id=\"more-88\"><\/span><\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre>Input:  1-&gt;10-&gt;30-&gt;14,  index = 2\r\nOutput: 30  \r\nThe node at index 2 is 30\r\n<\/pre>\n<div id=\"practice\">\n<p><strong>Algorithm:<\/strong><\/p>\n<pre>1. Initialize count = 0\r\n2. Loop through the link list\r\n     a. if count is equal to the passed index then return current\r\n         node\r\n     b. Increment count\r\n     c. change current to point to next of the current.\r\n<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p>C Programming:<\/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\">\/\/ C program to find n&#039;th node in linked list<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/>#include &lt;assert.h&gt;<br\/> <br\/>\/* Link list node *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node* next;<br\/>};<br\/> <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\/>\/* Takes head pointer of the linked list and index<br\/>    as arguments and return data at index*\/<br\/>int GetNth(struct node* head, int index)<br\/>{<br\/>    struct node* current = head;<br\/>    int count = 0; \/* the index of the node we&#039;re currently<br\/>                  looking at *\/<br\/>    while (current != NULL)<br\/>    {<br\/>       if (count == index)<br\/>          return(current-&gt;data);<br\/>       count++;<br\/>       current = current-&gt;next;<br\/>    }<br\/>   <br\/>    \/* if we get to this line, the caller was asking<br\/>       for a non-existent element so we assert fail *\/<br\/>    assert(0);              <br\/>}<br\/> <br\/>\/* Drier program to test above function*\/<br\/>int main()<br\/>{<br\/>    \/* Start with the empty list *\/<br\/>    struct node* head = NULL;<br\/>   <br\/>    \/* Use push() to construct below list<br\/>     1-&gt;12-&gt;1-&gt;4-&gt;1  *\/<br\/>    push(&amp;head, 1);<br\/>    push(&amp;head, 4);<br\/>    push(&amp;head, 1);<br\/>    push(&amp;head, 12);<br\/>    push(&amp;head, 1);  <br\/>   <br\/>    \/* Check the count function *\/<br\/>    printf(&quot;Element at index 3 is %d&quot;, GetNth(head, 3));  <br\/>    getchar();<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Element at index 3 is 4<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n)<\/p>\n[ad type=&#8221;banner&#8221;]\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>C Programming &#8211; Write a function to get Nth node in a Linked List &#8211; Linked List &#8211; Write a GetNth() function that takes a linked list and an integer index<\/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":[80286,80499,80490,80492,80489,80493,80495,80498,79571,80488,80491,80288,80494,80497,80285,80496],"class_list":["post-26979","post","type-post","status-publish","format-standard","hentry","category-linked-list","category-singly-linked-list","tag-algorithm-to-search-an-element-in-singly-linked-list","tag-c-program-to-search-for-an-element-in-the-linked-list-using-recursion","tag-delete-nth-element-in-linked-list","tag-find-middle-element-in-linked-list-java","tag-find-nth-element-in-linked-list-c","tag-find-nth-to-last-element-in-a-linked-list-recursion","tag-insert-a-node-at-a-specific-position-in-a-linked-list-c","tag-linear-search-using-linked-list-in-c","tag-linked-list-search-java","tag-nth-node-from-the-end-of-a-linked-list-java","tag-search-a-node-in-linked-list-in-c","tag-search-an-element-in-doubly-linked-list","tag-search-node-in-linked-list-c","tag-searching-a-linked-list-in-data-structure","tag-searching-in-linked-list-algorithm","tag-searching-in-linked-list-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26979","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=26979"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26979\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}