Bird
0
0
DSA Cprogramming~10 mins

Insert at End of Circular Linked List in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insert at End of Circular Linked List
Create new node
Is list empty?
Yesnew_node.next = new_node
head = new_node
Start at head
Traverse until current.next == head
Set current.next = new_node
Set new_node.next = head
Done
Create a new node, if list empty point it to itself. Otherwise, traverse to last node, link it to new node, and new node back to head.
Execution Sample
DSA C
struct Node {
  int data;
  struct Node* next;
};

void insertEnd(struct Node** head, int val) {
  struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
  new_node->data = val;
  new_node->next = NULL;

  if (*head == NULL) {
    new_node->next = new_node;
    *head = new_node;
    return;
  }

  struct Node* current = *head;
  while (current->next != *head) {
    current = current->next;
  }
  current->next = new_node;
  new_node->next = *head;
}
This code inserts a new node with value val at the end of a circular linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=10emptynew_node created, next=∅No nodes
2Check if list empty (head == NULL)emptyYes, new_node.next = new_node┌────────┐ │ data:10│ │ next:─┐│ └───────┘│ └─→ (points to itself)
3Set head = new_node1 nodehead → new_node┌────────┐ │ data:10│ │ next:─┐│ └───────┘│ └─→ (points to itself) head
4Insert 20 at end: create new node1 nodenew_node created with data=20, next=∅┌────────┐ │ data:10│ │ next:─┐│ └───────┘│ └─→ (points to itself) head new_node: ┌────────┐ │ data:20│ │ next: ∅│ └───────┘
5Traverse to last node (node with next=head)1 nodecurrent at node 10┌────────┐ │ data:10│ │ next:─┐│ └───────┘│ └─→ head head
6Set current.next = new_node2 nodesnode 10.next → node 20┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ next:─┐│──→ │ next: ∅│ └───────┘│ └───────┘ │ head └─────────────→ ?
7Set new_node.next = head2 nodesnode 20.next → head (node 10)┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ next:─┐│──→ │ next:─┐│ └───────┘│ └───────┘│ │ └─→ head head └───────────────→
8Insert 30 at end: create new node2 nodesnew_node created with data=30, next=∅List: ┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ next:─┐│──→ │ next:─┐│ └───────┘│ └───────┘│ │ └─→ head head └───────────────→ new_node: ┌────────┐ │ data:30│ │ next: ∅│ └───────┘
9Traverse to last node (node with next=head)2 nodescurrent moves: 10 → 20┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ next:─┐│──→ │ next:─┐│ └───────┘│ └───────┘│ │ └─→ head head └───────────────→ current at node 20
10Set current.next = new_node3 nodesnode 20.next → node 30┌────────┐ ┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ data:30│ │ next:─┐│──→ │ next:─┐│──→ │ next: ∅│ └───────┘│ └───────┘│ └───────┘ │ │ head └──────────────┘
11Set new_node.next = head3 nodesnode 30.next → head (node 10)┌────────┐ ┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ data:30│ │ next:─┐│──→ │ next:─┐│──→ │ next:─┐│ └───────┘│ └───────┘│ └───────┘│ │ │ └─→ head head └──────────────┘
12End of insertions3 nodesNo pointer changesFinal circular list: ┌────────┐ ┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ data:30│ │ next:─┐│──→ │ next:─┐│──→ │ next:─┐│ └───────┘│ └───────┘│ └───────┘│ │ │ └─→ head head └──────────────┘
💡 Insertion ends after linking new node's next to head, maintaining circular structure.
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 7After Step 10After Step 11Final
headNULLNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)
new_nodeNULLNode(10)Node(20)Node(20)Node(30)Node(30)NULL
currentNULLNULLNode(10)Node(10)Node(20)Node(20)NULL
list_size0112233
Key Moments - 3 Insights
Why does the new node point back to head instead of NULL?
Because it's a circular linked list, the last node must point back to head to keep the circle. See execution_table rows 7 and 11 where new_node.next is set to head.
Why do we traverse until current.next == head, not current == NULL?
In circular lists, there is no NULL at the end. The last node points back to head. So we stop when current.next is head, as shown in steps 5 and 9.
What happens if the list is empty before insertion?
We create a new node that points to itself and set head to it, making a single-node circular list. See steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what does new_node.next point to?
ANULL
Bhead node (data=10)
Cprevious last node (data=10)
Dnew_node itself
💡 Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 7.
At which step does the list first have two nodes?
AStep 3
BStep 6
CStep 7
DStep 10
💡 Hint
Look at 'Nodes in List' and 'Pointer Changes' columns to see when second node is linked.
If the list was empty, what pointer does head point to after first insertion?
Anew_node itself
Bnext node (which is NULL)
CNULL
Dprevious node
💡 Hint
Refer to variable_tracker and execution_table steps 2 and 3.
Concept Snapshot
Insert at End of Circular Linked List:
- Create new node
- If list empty: new_node.next = new_node, head = new_node
- Else traverse to last node (current.next == head)
- Set current.next = new_node
- Set new_node.next = head
- Maintains circular link structure
Full Transcript
To insert at the end of a circular linked list, first create a new node with the given data. If the list is empty (head is NULL), point the new node's next to itself and set head to this new node, forming a single-node circle. If the list is not empty, start at head and traverse nodes until you find the last node whose next points back to head. Change this last node's next pointer to the new node. Then set the new node's next pointer back to head, preserving the circular structure. This process ensures the new node is added at the end and the list remains circular.