- 2069 Poush · 3 marks
Delete Kth node from doubly linked list
Short answer
If list empty, underflow. Traverse to Kth node p. If p->prev != NULL, set p->prev->next = p->next else start = p->next. If p->next != NULL, set p->next->prev = p->prev. Free p and return its info.
Write an algorithm for deleting Kth node from the doubly linked list.
With node (prev, info, next) and start pointing to the first node:
Algorithm DELETE_K(start, k)
- If
start == NULLprint "underflow" and stop. - Set
p = start; repeat k-1 times:p = p->next; ifpbecomes NULL print "invalid position" and stop. - If
p->prev != NULLthenp->prev->next = p->next; elsestart = p->next. - If
p->next != NULLthenp->next->prev = p->prev. x = p->info;free(p); return x.
Before: [a] <-> [p(K)] <-> [b]
After : [a] <-> [b]
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Insert and delete kth node in doubly linked list · Data Structure and Algorithm, chapter 4 · asked 2×
- Insert before Kth node in doubly linked list · Data Structure and Algorithm, chapter 4
- Delete last node of doubly linked list · Data Structure and Algorithm, chapter 4
- Insert and delete before a node, singly linked · Data Structure and Algorithm, chapter 4
- Delete employee from doubly linked list · Data Structure and Algorithm, chapter 4