![]() (*head_ref) = new_node // Store the newly allocated memory addressĪs you were tagging your question c++ originally this isn't necessary using c++ code. New_node->next = (*head_ref) // Dereferencing head_ref yields the current Here each new node will be dynamically allocated, so overflow. If you want to insert at the end of the list, you can also hold the end of the list as a pointer and insert a new node after it. Void push(struct Node** head_ref, int new_data) Using an array will restrict the maximum capacity of the array, which can lead to stack overflow. If you want to insert at the beginning of the list, you just make the new list head the node you want to insert, and link it to the previous list head. The value stored in the head pointer variable should be changed from within the call to push(), so you need to pass the address of the head variable to change the (single * pointer) value. In plain c code you can't have references (as in contrast to c++), but just pointers. I would be glad if anybody could explain the reason to me, Thanks.īut I don't see why it is necessary to use the adress of head and not head itself. I understand that the second function should work, but I don't see why it is necessary to use the adress of head and not head itself. move the head to point to the new node */Īnd this is the main for the function with double **: int main() on the other hand, when I give the function the adress of head pointer instead of the pointer itself, it does work: void push(struct Node** head_ref, int new_data) it seems very logical, although it didn't work. So, what I did is that I made the new_node point to the same node pointed by head, after that I make the new node the new head of the linked list. Democracies elect holders of high office by voting. Struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)) Voting is a method by which a group, such as a meeting or an electorate, convenes together for the purpose of making a collective decision or expressing an opinion usually following discussions, debates or election campaigns. this is my code: void push(struct Node* head, int new_data) ![]() so I created one that takes 2 arguments: the integer to add and the pointer to the head of linked list, but it didn't work. I had this exercice that asks me to create a function that adds a number in a new node to the head of a linked list based on a struct containing one integer. ![]()
0 Comments
Leave a Reply. |