The program goes into an infinite loop. I can't really seem to understand why. The logic is simple. My implementation as far as i can see is pretty accurate. Any help would be appreciated.
Your while loop will run until ptr is invalid. Where inside that while loop does the validity of ptr change? Nowhere. So when will the ptr value become invalid? Never. So when will the while loop stop? Never.