I have written a program which checks whether the entered list is a circular list or not. I am successfully able to make the circular list. But as soon as I call the check_circular fucntion my program hangs. Can anybody find the error please? And yes the programs hangs when the list is circular and I call check_circular function. The check_circular function executes properly if the list is not circular.
I am using gcc compiler!

#include<stdio.h>
#include<malloc.h>
int n;
struct node
{
    int data;
    struct node *next;
};
struct node *newnode,*temp,*temp2,*start=NULL,*x, *temp3, *temp4;

void create()
{
	
	int i;
	printf("Enter the no. of nodes you want to insert");
	scanf("%d", &n);
	
	for(i=0; i<n; i++)
	{
	    	newnode=malloc(sizeof(struct node));
		printf("\n Enter the data ");
		scanf("%d",&newnode->data);
		newnode->next=NULL;
		if(start==NULL)
		    start=newnode;
		else
		{
		    for(temp=start;temp->next!=NULL;temp=temp->next);        
		       
		    temp->next=newnode;
		}
	}
}

void display()
{
    if(start==NULL)
    {
	printf("\n Linklist is EMPTY ");
    }
    else
    {
	printf("The entered list is:");
	for(temp=start;temp!=NULL;temp=temp->next)
	{
	    
	    printf("\n %d",temp->data);
	}
    }
}

void make_circular()
{
    int i,pos;
    
    printf("\n Enter the node no at which you want to connect the list to make it circular (-1 to leave as it is) ");
    scanf("%d",&pos);
	    if(pos>n)
    {
	printf("Invalid position");
    }

    else if(pos==-1)
	return;
    
    else
	{
		
	   	for(temp=start;temp->next!=NULL;temp=temp->next);
		temp2=start;
		printf("After temp2");
		for(i=1; i<pos; i++)
			temp2=temp2->next;
		temp->next=temp2;
	 }   

}


void check_circular()
{

temp3=start;

while(temp3!=NULL)
{
	
	temp4=temp3->next;
	
	while(temp4!=NULL)
	{
		if(temp4==temp3)
		{
			printf("List is circular");
			return;
		}
		temp4=temp4->next;
	}
	temp3=temp3->next;
}printf("List is not circular");
}		

int main()
{
   	create();
	display();
	make_circular();
	
	check_circular();
	//display();
	
    	return 0;
}

Your code is hanging because of the for loop on line 70.
From the looks of it I dont think you need the loop, so might as well delete it

But from a conceptual point of view I cannot understand why loop runs on infinitely

loop in line 70 is perfectly fine. I am using it becoz I need to find the last node in the list. So temp is running till it reaches last node.

Which loop is running infinitely?

n yeah if I dont call the check_circular function at all then still the code is running fine!

loop in line 70 is perfectly fine. I am using it becoz I need to find the last node in the list. So temp is running till it reaches last node.

Which loop is running infinitely?

The loop in line 70 is running infinitely that is why your program hangs. Your loop finds the end of the list but I dont see the end being used anywhere else in the function

Line #75, connects the last node to the one specified (i.e. temp2) - why? Probably not what you want.

This is exactly what I want to do. I am asking the user to enter the no. of the node to which he wants to connect the last node. then only that part of the list would become circular. Actually the program checks whether a given list is circular or not. So I am making a circular list first and then checking it.

The loop in line 70 is running infinitely that is why your program hangs. Your loop finds the end of the list but I dont see the end being used anywhere else in the function

No the loop is not running infinitely. It runs till it finds the last node and then terminates.
If you will not call the check_circualr function while executing the program then everything is working fine. It is giving a circular list. I have checked it by printing the list again after making it circular.

If I call the check_circular function then the program hangs as soon as I enter the node no to which the circular list is to be connected (line 58). I tried printing something just after line 58 and it doesnt gets executed. How is my calling check_circular functions affecting the make_circular loop. If I dont call check_circular then make circular works perfectly. How is it possible that some other function is affecting a previous function even thought it hasn't been called at that point?

This article has been dead for over six months. Start a new discussion instead.