Write a C function for the following void listtailattachNode

Write a C++ function for the following:

void list_tail_attach(Node*& head_ptr, const Node::Item& entry)
// Precondition: head_ptr is the head pointer of a linked list.
// Postcondition: A new node containing the given entry has been added at
// the tail of the linked list; if this happens to be the first node of
// the linked list, then head_ptr now points to this node (otherwise
// head_ptr is unchanged).

Solution

void list_tail_attach(Node*& head_ptr, const Node::Item& entry)
{
//If head ptr is null then assign entry address to head_ptr
if(head_ptr==NULL)
{
head_ptr = &entry;
}
//IF not then find the last node and add entry as a last entry to the list
else
{
Node *temp = head_ptr;
while(temp->next != NULL)
temp = temp->next;
  
temp->next = &entry;
}
  
return;
}

Write a C++ function for the following: void list_tail_attach(Node*& head_ptr, const Node::Item& entry) // Precondition: head_ptr is the head pointer of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site