code not showing any output. I think i have implemented the correct logic can someone plz correct my code
code link : https://ide.codingblocks.com/s/98844
Circular linked list error
From what I understood, your code is not showing any output because your linked list is not getting formed. Your code is entering an infinite loop.
When the first element is read, a new node say N1 is created using the push function. Its ‘next’ is put equal to ‘head’, which is null in the beginning. The code enters ‘else’ block and this node now links to itself. N1 is also the head. When the next element is read, a new node N2 is created and its ‘next’ points to N1. Now, ‘head’ is not null, so code enters ‘if’ block, and temp is equal to head(N1), which points to itself. As a result, the breaking condition of while loop is never satisfied and the code enters an infinite loop. Moreover, since you’re adding every new node behind the head, your LL will be formed in reverse.
So you’ll have to work more on your logic, always do a dry run with linked list questions. Also, (temp.next != null) would be an incorrect terminating condition for a circular linked list as no element would be pointing to null. And as asked in the question, you have not written any code for detection and breaking of loop in the linked list, so do that too!
thank you anukriti for the help now i have updated my code with 2 functions first one is to detect whether there is a loop or not and second function is to remove the loop yet when i am running my code it is still showing the linked list with the same input.
code link : https://ide.codingblocks.com/s/99055
https://ide.codingblocks.com/s/102313
I have made the required changes to your code, you can see them. The problem with your code was that you were not forming a circular linked list. You were forming a normal linked list with duplicate nodes, so there was no loop being formed. So for input 1 2 3 4 5 2 3 -1, your output was 1 2 3 4 5 2 3 because 5 was not pointing back to 2, but a new node 2 was being created and attached to 5.