4. Insert node at the ith position #
Created Wednesday 15 January 2020
There are 2 variables:
- Existence: head==NULL or not
- Index: wether 0 or not
- 0
-
0
- Wrote the general algorithm, then the exceptions. Only requirements, no need for optimizations here.
- index 0 is always valid, heads need to be updated.
- index > 0 has two cases:
- Within bound
- Greater than length
- negative indices
void insertNode(Node &head, int index, int data) { / cases possible
- Empty list
- Index at 0, i.e make the first node
- Index != 0 do nothing
- Non Empty list
- index at 0, Make the node shift the head
how to prevent negative indices ?
- index < length -1, reach the (i-1)th node, create a node
- index > length - 1 , do nothing
Conclusion: 1. 1.a and 2. a are the same thing, create a node at the beginning
- 1.b is different // impossible
- 2.b and 2.c are intertwined //
*/
if (index == 0) // case 1.a and 2.a done { Node *newnode = new Node(data); newnode->next = head; // handles both cases, i.e empty and non-empty lists head = newnode; return; }
if (head == NULL) // case 1. b done return;
// case 2.b and 2.c Node *trav = head; for (int i = 0; i < index - 1 && trav != NULL; i++, trav = trav->next) // also prevents negative indices ;
// 2.c handled if (trav == NULL) // the construction site is null, this is possible if index > length-1 return;
Node *newnode = new Node(data); newnode->next = trav->next; trav->next = newnode; return; }--------------------