I have a linked list of structures and would like to delete and return the element (a struct) from the first position of the linked list.
Now, it's good practice to free the memory location previously occupied by the node so I am not sure if my only option to return the deleted node is creating a malloced deep copy before deleting it and returning that copy?
Item *deleteFromStack(stack *s) {
if (s == NULL) return NULL;
Item *first = getFirst(s->list);
deleteFirst(q->list); // PROBLEM HERE: deleteFirst will free the memory pointed by first
return first; // now first has garbage values
}