Question: Please explain each line of the codes. queue lab. c lang /* Attempt to insert element at head of queue. Return true if successful. Return
Please explain each line of the codes. queue lab. c lang
/*
Attempt to insert element at head of queue.
Return true if successful.
Return false if q is NULL or could not allocate space.
Argument s points to the string to be stored.
The function must explicitly allocate space and copy the string into it.
*/
bool q_insert_head(queue_t *q, char *s)
{ list_ele_t *newh;
/* What should you do if the q is NULL? */
if(!q)
return false;
newh = malloc(sizeof(list_ele_t));
if(!newh)
return false;
/* Don't forget to allocate space for the string and copy it */
/* What if either call to malloc returns NULL? */
char *value;
value = malloc(1 + strlen(s));
if(!value)
{ if(newh)
free(newh);
return false;
}
strcpy(value, s);
newh->value = value;
newh->next = q->head;
if(q->head == NULL)
q->tail = newh;
q->head = newh;
q->cs += 1;
return true;
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
