Hi everyone! I need your help to understand the situation below. Please be good to me.. :)
For example I have the given structure:
Now I am required to implement a create function using singly linked list with a dummy node. I actually have several answers and I am not sure which one is correct.Code:struct orders
{int quantity;}
char foodname [50];
typedef struct orders ORDER;
struct Table
{int tableno;
int priority;
ORDER *orders;struct Table *next;}
typedef struct Table TABLE
1st answer:
2nd answer:Code:TABLE *create()
{TABLE *newO;newO = (TABLE *)malloc(sizeof(TABLE));}
newO->next = NULL;
return newO;
As you can see on my 2nd answer, i have set all the elements on the structure as NULL;Code:TABLE *create()
{TABLE *newO;}
newO = (TABLE *)malloc(sizeof(TABLE));
newO->tableno = NULL;
newO->priority = NULL;
strcpy(newO->orders.foodname, " ");
newO->orders.quantity = NULL;
newO->next = NULL;
return newO;
3rd answer:
On my 3rd answer, I have an if statement to check if the pointer is null.Code:TABLE *create()
{TABLE *newO;
newO = (TABLE *)malloc(sizeof(TABLE));
if (newO)
{newO->next = NULL;
return newO;}
else
{return O;}}
4th answer:
Just same with 3rd answer but with the elements of the data structure set to NULL.Code:TABLE *create()
{TABLE *newO;
newO = (TABLE *)malloc(sizeof(TABLE));
if (newO){newO->tableno = NULL;newO->priority = NULL;
strcpy(newO->orders.foodname, " ");
newO->orders.quantity = NULL;newO->next = NULL;
return newO;}
else
{return 0;}}
Can you please tell me which one is the correct answer?
Or are my answer all acceptable?
Also if there is wrong with my answers, can you pinpoint which part is it?
Looking forward to hear from everyone really soon. :)