- 2069 Bhadra · 7 marks
Delete employee from doubly linked list
Short answer
To delete the node with ID 165-64 from a doubly linked list, traverse the list to find it. Once found, update the previous node’s next pointer and the next node’s prev pointer to bypass the target node, then free it. Handle first, middle, and last node cases.
Suppose you have a doubly linked list that stores employee information. Explain how an employee with employee ID 165-64 can be deleted from the list with necessary illustrations and pseudo codes.
Assume each employee node is:
struct emp {
struct emp *prev;
char id[10]; /* e.g. "165-64" */
char name[30];
float salary;
struct emp *next;
};
struct emp *start; /* first node */
Idea
Search the list for the node whose ID is "165-64"; then make its previous node and next node point to each other, and free it. There are three cases: first node, middle node, last node.
Illustration
Before:
start -> [NULL|101-12|*] <-> [*|165-64|*] <-> [*|201-07|NULL]
^ p
After:
start -> [NULL|101-12|*] <-> [*|201-07|NULL]
The first node's next is set to the third node, and the third node's prev is set to the first node. Node "165-64" is freed.
Pseudo code
DELETE_EMP(start, "165-64")
1. if start == NULL then print "empty list"; return
2. p = start
3. while p != NULL and p->id != "165-64"
p = p->next
4. if p == NULL then print "employee not found"; return
5. if p->prev == NULL // deleting first node
start = p->next
else
p->prev->next = p->next
6. if p->next != NULL // not the last node
p->next->prev = p->prev
7. free(p)
C code
void delete_emp(struct emp **start, const char *id) {
struct emp *p = *start;
while (p != NULL && strcmp(p->id, id) != 0)
p = p->next;
if (p == NULL) { printf("Not found\n"); return; }
if (p->prev == NULL) *start = p->next; /* first node */
else p->prev->next = p->next;
if (p->next != NULL) p->next->prev = p->prev;
free(p);
}
/* call: delete_emp(&start, "165-64"); */
Cases: if it is the first node, start moves to the second node; if it is the only node, start becomes p->next, which is NULL, and step 6 is skipped; if it is the last node, step 6 is skipped. Time is O(n) for the search and O(1) for the removal.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Doubly linked list: bank customer records · Data Structure and Algorithm, chapter 4
- Insert and delete kth node in doubly linked list · Data Structure and Algorithm, chapter 4 · asked 2×
- Delete last node of doubly linked list · Data Structure and Algorithm, chapter 4
- Delete Kth node from doubly linked list · Data Structure and Algorithm, chapter 4
- Insert and delete before a node, singly linked · Data Structure and Algorithm, chapter 4
- Doubly vs singly linked list · Data Structure and Algorithm, chapter 4 · asked 2×
- Subnet 202.70.64.0/19 for 100-800 hosts · Computer Networks and Security, chapter 4 · asked 2×
- RA: delete Electrical dept projects · Database Management System, chapter 3
- Manager derived from person and employee · Object Oriented Programming, chapter 6
- Map sequence diagram to pseudo code · Object Oriented Analysis and Design, chapter 4