
- RETURNING A NODE OF LINKED LIST STACK OVERFLOW HOW TO
- RETURNING A NODE OF LINKED LIST STACK OVERFLOW UPDATE
10) When the user tries to delete the element from the empty stack then. Your problem is handled in int remove_end(LinkedList *list). Array, Pointer, Structure, Singly Linked List, Doubly Linked List, Graph. I've done the same as you and struggled at the same point, but finally implemented it cleanly. Otherwise we can just set the head to the second node in the list and return the removed node. the question is specifically about the situation when there's only one element left. I suspect that the word "last" in your question does not refer to the tail element of the list, but rather refers to the last remaining element of the list. The fact that you mention that head "is not pointer-to-pointer" suggests that this might indeed be important. Next contains a reference to the next node on the list. The main advantage of using a linked list over arrays is that it is possible to implement a stack that can shrink or grow as much as needed. Data contains the value to be stored in the node. Each element of a linked list is called a node, and every node has two different fields. I don't know whether that issue point is important in your case. Before going more in depth on what linked lists are and how you can use them, you should first learn how they are structured. For example, the caller's head pointer can be made accessible and modifiable from inside the delete function if the first parameter is declared as a pointer-to-pointer to head node int delete(struct node **phead, int item)Īlternatively delete function can be made to always return the updated head pointer value struct node *delete(struct node *head, int item) Typically, when one implements a function like that, one should make sure that the caller will know when the list becomes empty. It will point to now-deallocated memory, i.e.

After such deletion the caller's head pointer will become invalid.
RETURNING A NODE OF LINKED LIST STACK OVERFLOW HOW TO
Explain whats wrong with it and show how to fix the bug.
RETURNING A NODE OF LINKED LIST STACK OVERFLOW UPDATE
However, if the list initially contained only one element (meaning that head is already pointing to the last element) then, of course, you can still easily delete it, but unfortunately you can't update caller's head pointer from inside delete function. The following loop is supposed to delete all nodes from a linked list and release the memory they occupy. Thanks for contributing an answer to Stack Overflow Please be sure to answer the question.

Note that in that case the caller's head pointer will remain a perfectly valid pointer to a valid list. This change is not passed back to the caller. Simply iterate to the element before the last, delete the last and update the next pointer in new last element. The headNode newHead assignment in popHead is assigning to the local headNode variable. You can, of course, easily and safely delete the last element (= tail element of the list), if the list contains more than one element. The answer depends on what exactly is meant by the question.
