Circular Linked List

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

programmercarlito@gmail.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 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
            }

Last edited on
Topic archived. No new replies allowed.