Did you forget that main returns an int?
Code:
head=(node*)malloc(sizeof(node));
FYI, the cast is unnecessary.
Code:
int checkround(node *head);
The name "checkaround" is misleading, since it actually alters the data structure. Try to use more meaningful names.
Code:
head=(node*)malloc(sizeof(node));
head->next=(node*)malloc(sizeof(node));
head->next->next=(node*)malloc(sizeof(node));
head->next->next->next=(node*)malloc(sizeof(node));
head->next->next->next->next=(node*)malloc(sizeof(node));
head->next->next->next->next->next=(node*)malloc(sizeof(node));
head->next->next->next->next->next->next=head;
head->value=2;
head->next->value=4;
head->next->next->value=1;
head->next->next->next->value=5;
head->next->next->next->next->value=3;
head->next->next->next->next->next->value=8;
Think modular. A simple function would make this so much easier. Consider something along these lines:
Code:
node * add_node(node **head, int value)
{
node* ptr = (node*)malloc(sizeof(node));
if(ptr == NULL)
return NULL;
if(*head == NULL)
*head = ptr;
ptr->next = *head;
ptr->value = value;
node *current = *head;
while(current->next != *head)
current = current->next;
current->next = ptr;
return ptr;
}
Then, in main, you can simply do:
Code:
int main( void )
{
node *head = NULL;
add_node(&head, 2);
add_node(&head, 4);
add_node(&head, 1);
add_node(&head, 5);
add_node(&head, 3);
add_node(&head, 8);
return 0;
}
>> so why it stops one step before the target
Print the values of all of the variables involved as you go around the data structure.
Code:
void print_list(node *head)
{
if(head == NULL)
return;
node *current = head;
do
{
printf("%x: %d\n", current, current->value);
current = current->next;
}
while(current != head);
}
int checkround(node *head)
{
int temp,n_temp;
node * ptr;
ptr=head;
do
{
printf("Begin Loop: ptr: %x, ptr->value: %d\n", ptr, ptr->value);
for(temp=ptr->value;temp>0;temp=temp-1) //here is the problem
{
printf("ptr: %x, ptr->value: %d, temp: %d\n", ptr, ptr->value, temp);
ptr=ptr->next;
}
n_temp=ptr->value;
if (n_temp==-1) return 0;
ptr->value=-1;
}while(ptr!=head);
ptr=head;
do
{
if (ptr->value!=-1)
{
return 1;
}
ptr=ptr->next;
}while(ptr!=head);
return 2;
}
int main( void )
{
node *head = NULL;
add_node(&head, 2);
add_node(&head, 4);
add_node(&head, 1);
add_node(&head, 5);
add_node(&head, 3);
add_node(&head, 8);
puts("List:");
print_list(head);
puts("Check:");
checkround(head);
return 0;
}
Finally, I would recommend indenting your code consistently, and giving your variables more "room to breath" (eg: instead of "a==-1" use "a == -1").