| 1 | Create new node with data=10 | empty | new_node created, next=nullptr | ā
|
| 2 | Check if list empty | empty | head is nullptr, so list empty | ā
|
| 3 | Set new_node->next = new_node | 1 node | new_node.next ā new_node (self-loop) | āāāāāāāāāā
ā data:10ā
ā next:āāā
āāāāāāāāāā
āāāāāāā (head) |
| 4 | Set head = new_node | 1 node | head ā new_node | āāāāāāāāāā
ā data:10ā
ā next:āāā
āāāāāāāāāā
āāāāāāā (head) |
| 5 | Insert 20 at beginning: create new_node with data=20 | 1 node | new_node created, next=nullptr | āāāāāāāāāā
ā data:10ā
ā next:āāā
āāāāāāāāāā
āāāāāāā (head) |
| 6 | List not empty, find last node | 1 node | last starts at head (10), last->next == head (10), stop | āāāāāāāāāā
ā data:10ā
ā next:āāā
āāāāāāāāāā
āāāāāāā (head, last) |
| 7 | Set last->next = new_node | 2 nodes | 10.next ā 20 | āāāāāāāāāā āāāāāāāāāā
ā data:10āāāāāā data:20ā
ā next:āāā ā next:? ā
āāāāāāāāāā āāāāāāāāā
āāāāāāā (head) |
| 8 | Set new_node->next = head | 2 nodes | 20.next ā 10 | āāāāāāāāāā āāāāāāāāāā
ā data:10āāāāāā data:20ā
ā next:āāā ā next:āāā
āāāāāāāāāā āāāāāāāāāā
āāāāāāā āāāāā
(head) (new_node) |
| 9 | Set head = new_node | 2 nodes | head ā 20 | āāāāāāāāāā āāāāāāāāāā
ā data:20āāāāāā data:10ā
ā next:āāā ā next:āāā
āāāāāāāāāā āāāāāāāāāā
āāāāāāā āāāāā
(head) (last) |
| 10 | Insert 30 at beginning: create new_node with data=30 | 2 nodes | new_node created, next=nullptr | āāāāāāāāāā āāāāāāāāāā
ā data:20āāāāāā data:10ā
ā next:āāā ā next:āāā
āāāāāāāāāā āāāāāāāāāā
āāāāāāā āāāāā
(head) (last) |
| 11 | Find last node starting at head (20) | 2 nodes | last=10, last->next=20 (head), stop | āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
ā data:30ā ā data:20ā ā data:10ā
ā next:? ā ā next:? ā ā next:? ā
āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
new_node head last |
| 12 | Set last->next = new_node | 3 nodes | 10.next ā 30 | āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
ā data:30ā ā data:20ā ā data:10ā
ā next:? ā ā next:? ā ā next:āāā
āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
āāāāā |
| 13 | Set new_node->next = head | 3 nodes | 30.next ā 20 | āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
ā data:30āāāāāā data:20āāāāāā data:10ā
ā next:āāā ā next:āāā ā next:āāā
āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
āāāāāāāāāāāāāāāā
(new_node) |
| 14 | Set head = new_node | 3 nodes | head ā 30 | āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
ā data:30āāāāāā data:20āāāāāā data:10ā
ā next:āāā ā next:āāā ā next:āāā
āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
āāāāāāā ā ā
(head) (middle) (last) |
| 15 | End of insertions | 3 nodes | Pointers updated, circular list intact | āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
ā data:30āāāāāā data:20āāāāāā data:10ā
ā next:āāā ā next:āāā ā next:āāā
āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
āāāāāāā ā ā
(head) (middle) (last) |