Hi,
Here is my try:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
void non_cir(void);
void cir_ll (void);
struct list
{
unsigned data;
struct list *next;
};
void simple_err(const char *const str)
{
fprintf(stderr,"%s\n",str);
exit (EXIT_FAILURE);
}
bool contains_cir(struct list * head)
{
bool r=false;
struct list *current=NULL;
if(head==NULL)
simple_err("head is NULL");
for(current=head->next; current->next!=NULL; current=current->next)
{
if(current==head)
{
r=true;
break;
}
}
return r;
}
int main(void)
{
//printf("a3\n");
non_cir();
cir_ll();
return EXIT_SUCCESS;
}
void non_cir(void)
{
static struct list nc[3]={{0,nc+1},{1,nc+2},{3,NULL}};
if(!contains_cir(nc))
printf("%s is not circular.\n",__FUNCTION__);
else
simple_err("problem in non_cir.");
return;
}
void cir_ll (void)
{
static struct list cll[2]={{0,cll+1},{1,cll}};
if(contains_cir(cll))
printf("%s is circular.\n",__FUNCTION__);
else
simple_err("problem in cir_ll.");
return;
}