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.

Example:

Input:  1->10->30->14,  index = 2
Output: 30  
The node at index 2 is 30

Algorithm:

1. Initialize count = 0
2. Loop through the link list
     a. if count is equal to the passed index then return current
         node
     b. Increment count
     c. change current to point to next of the current.
[ad type=”banner”]

C Programming:

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20to%20find%20n’th%20node%20in%20linked%20list%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%23include%20%3Cassert.h%3E%0A%20%0A%2F*%20Link%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20next%3B%0A%7D%3B%0A%20%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%0A%20%20%20%20of%20a%20list%20and%20an%20int%2C%20push%20a%20new%20node%20on%20the%20front%0A%20%20%20%20of%20the%20list.%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%20%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%20%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Takes%20head%20pointer%20of%20the%20linked%20list%20and%20index%0A%20%20%20%20as%20arguments%20and%20return%20data%20at%20index*%2F%0Aint%20GetNth(struct%20node*%20head%2C%20int%20index)%0A%7B%0A%20%20%20%20struct%20node*%20current%20%3D%20head%3B%0A%20%20%20%20int%20count%20%3D%200%3B%20%2F*%20the%20index%20of%20the%20node%20we’re%20currently%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20looking%20at%20*%2F%0A%20%20%20%20while%20(current%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20if%20(count%20%3D%3D%20index)%0A%20%20%20%20%20%20%20%20%20%20return(current-%3Edata)%3B%0A%20%20%20%20%20%20%20count%2B%2B%3B%0A%20%20%20%20%20%20%20current%20%3D%20current-%3Enext%3B%0A%20%20%20%20%7D%0A%20%20%20%0A%20%20%20%20%2F*%20if%20we%20get%20to%20this%20line%2C%20the%20caller%20was%20asking%0A%20%20%20%20%20%20%20for%20a%20non-existent%20element%20so%20we%20assert%20fail%20*%2F%0A%20%20%20%20assert(0)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%7D%0A%20%0A%2F*%20Drier%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20%20%20struct%20node*%20head%20%3D%20NULL%3B%0A%20%20%20%0A%20%20%20%20%2F*%20Use%20push()%20to%20construct%20below%20list%0A%20%20%20%20%201-%3E12-%3E1-%3E4-%3E1%20%20*%2F%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%20%20%20push(%26head%2C%204)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%20%20%20push(%26head%2C%2012)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%20%20%0A%20%20%20%0A%20%20%20%20%2F*%20Check%20the%20count%20function%20*%2F%0A%20%20%20%20printf(%22Element%20at%20index%203%20is%20%25d%22%2C%20GetNth(head%2C%203))%3B%20%20%0A%20%20%20%20getchar()%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Element at index 3 is 4

Time Complexity: O(n)

[ad type=”banner”]