Is it possible for someone to draw me this code line by line? I tried drawing numerous amount of times but still cant seem to get it.
Can you send me the picture to my email
void insert(T item)
{
// step 1 : prepare our new Node with the given "item"
Node<T>* n = new Node<T>;
n->data = item; // set the "data" field of the node to the given "item" coming from the function parameter
if (head == NULL)// CASE 1 : if initially the linked list is empty
{
// make this new node "n" as the head
head = n;
head->next = head; // circular link back on the node itself as ONLY ONE node right now
}
else
{
// traverse circular linked list
Node<T>* traverse = head; // start from head
if (traverse->data > item) // CASE 2 : insertion at head node position
{
// insert "n" before head
n->next = head;
// traverse ahead to find the "last" node
Node* last = traverse;
traverse = traverse->next;
while (traverse != head)
{
last = traverse;
traverse = traverse->next;
}
last->next = n; // update the "Next" of last node to the new node "n"
// now update "head"
head = n;
return; // insertion done, so just return (exit) from this function
}