How do I transfer the list into a function via parameter?

Good day, I have been struggling with the task of creating a linked list, and not add elements.
The point is that the function item is added correctly, but as soon as the output from the function list how-to, and no...
And so:
Here is the element structure:
struct Node
{
 string word;
 Node* next;
};

Is a function of the initialization list:
Node* init(string str)
{
 Node* item = new Node;
 item->word = str;
 item->next = NULL;
 return item;
}

Let's say the list is initialized(it is not), and I want to add a new item by calling the following function:
void AddNode(Node* pfirst, string str)
{
 Node *tmp, *item = new Node;
 item->word = str;
 item->next = NULL;

 tmp = pfirst;
 while (tmp->next != NULL)
 tmp = tmp->next;
 tmp->next = item;
 delete tmp;
}


According to the debugger:
The list is passed to the function and adds a new element, but after this function, the list turns to garbage.
Here is Main:
int main()
{
 Node* head = NULL;

 head = init("One");

 AddNode(head, "Two");

 PrintList(head); // print the list

 return 0;
}


Your question: when I transfer the list into the function is the same list or a new one that just will not return the pointer head? Do I need to return a list return'ohms? How to implement it?
The whole code:
spoiler

struct Node
{
 string word;
 Node* next;
};

Node* init(string str)
{
 Node* item = new Node;
 item->word = str;
 item->next = NULL;
 return item;
}

void AddNode(Node* pfirst, string str)
{
 Node *tmp, *item = new Node;
 item->word = str;
 item->next = NULL;

 tmp = pfirst;
 while (tmp->next != NULL)
 tmp = tmp->next;
 tmp->next = item;
 delete tmp;
}

void PrintList(Node* pfirst)
{
 Node* tmp = pfirst;
 while (true)
{
 cout << tmp->word << endl;
 if (tmp->next == NULL) break;
 tmp = tmp->next;
}
}

int main()
{
 Node* head = NULL;

 head = init("One");
 AddNode(head, "Two");
 return 0;
}



Do not suggest creating a list class, I need to keep that way. Thank you!
April 3rd 20 at 18:35
1 answer
April 3rd 20 at 18:37
Solution
You are all correct except no need to call
delete tmp;
in the function AddNode(). So you are removing the penultimate element of the list, and it does not need to do.
From AddNode to return don't need anything, you pass it in parameters a pointer and the structure it points to, modify it.
but I use the item to create a new item, and tmp is a thing which should find the last item and stick to it item. Then I don't need to create the item? - marie_Grady commented on April 3rd 20 at 18:40
@marie_Grady, all the way, a pointer tmp is the last element, cling to it created item, and all the more nothing to do. You added to the end of the list a new item. Although tmp continues to point to the penultimate cell, no memory leak in this. It's just a local variable - the pointer. When you exit the function the variable is destroyed, but the data to which it points will remain intact. - brandon.Treutel commented on April 3rd 20 at 18:43
@brandon.Treutel, o Lord, for sure. Dynamic memory he stood out, thanks! - marie_Grady commented on April 3rd 20 at 18:46

Find more questions by tags C++Data structures