It's my First Program using circuar singly linklist

typedef struct clist
{
    int data;
    struct clist *next;
}CLIST;
CLIST *cp = NULL;

Add new node function

void add(int num)
{
   CLIST *r;
   r =  malloc (sizeof (CLIST));
   if (!r)
   {
       printf("\n\n Not Enough Memory");
       return;
   }
   r->data = num;

   if (!cp)
   {
       cp = r;
       cp->next = cp;
   }
   else
   {
       r->next = cp->next;
       cp->next = r;
       cp = r;
   }

}

And Display Function

void display()
{
   CLIST *temp;
   temp = cp;
   if (!cp)
   {
       printf("\n  Empty!!");
       return;
   }

   do
   {
       temp = temp->next;
       printf("\n   %d",temp->data);

   }while (temp != cp);

}

IS IT CORRECT??

Also it's showing one warning

warning: incompatible implicit declaration of built-in function 'malloc'

Why?

Recommended Answers

All 3 Replies

"warning: incompatible implicit declaration of built-in function 'malloc'"

Are you including stdlib.h?

I was using process.h , now i replaced it with malloc.h.
No warning now.. thanx

But what about my Circular Linklist ??

Is it correct CIRCULAR implementation??

>I was using process.h , now i replaced it with malloc.h.
So you replaced a header which didn't contain the function wanted with a header that might not even exist. Brilliant. And what about including stdlib.h as gerard suggested? That's the correct solution.

>Is it correct CIRCULAR implementation??
Assuming you want to write code for a living, you can't just toss your code to others for confirmation of correctness. You need to answer this question yourself with thorough test cases.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.