- 2071 Bhadra · 6+4 marks
Doubly linked list: bank customer records
Short answer
To delete customer 00056PR, locate its node p. Update links: if not first, set p->prev->next = p->next else start = p->next; if p->next exists, set p->next->prev = p->prev; free(p). For sorted insertion, find insertion point after p, link new node between p and p->next. Doubly deletion uses prev directly, avoiding a trailing pointer needed in a singly list.
Suppose you have a doubly linked list that stores bank customers information. Explain how a customer with account number 00056PR can be deleted and how a new customer can be inserted. What is the difference in this implementation relative to singly linked list?
Assume each node stores the customer data and the list is kept sorted by account number:
struct cust {
struct cust *prev;
char acc[10]; /* e.g. "00056PR" */
char name[30];
float balance;
struct cust *next;
};
start points to the first node.
Deleting customer 00056PR
- If
start == NULL, print "list empty" and stop. - Set
p = start; whilep != NULLandstrcmp(p->acc, "00056PR") != 0, setp = p->next. - If
p == NULL, print "customer not found" and stop. - If
p->prev != NULL, setp->prev->next = p->next; otherwise (first node)start = p->next. - If
p->next != NULL, setp->next->prev = p->prev. free(p).
Before: [A] <-> [00056PR] <-> [B]
After : [A] <-> [B] A->next = B, B->prev = A
Inserting a new customer (kept in sorted order)
- Allocate node
newand fill its data. - If the list is empty or
strcmp(new->acc, start->acc) < 0:new->prev = NULL; new->next = start;ifstart != NULLthenstart->prev = new;start = new; stop. - Else find
p, the last node whose account number is smaller than the new one:p = start; while (p->next != NULL && strcmp(p->next->acc, new->acc) < 0) p = p->next; new->next = p->next; new->prev = p;- If
p->next != NULLthenp->next->prev = new. p->next = new.
Before: [p] <-> [q]
After : [p] <-> [new] <-> [q]
Difference from a singly linked list
| Point | Doubly | Singly |
|---|---|---|
| Delete found node | Uses p->prev directly | Needs a trailing pointer prev kept during search |
| Insert | Set 4 links (new->prev, new->next, p->next, q->prev) | Set 2 links |
| Memory | Extra pointer per customer | Less |
| Search direction | Can search from either end | Only forward |
So a doubly list makes deletion simpler (no trailing pointer) but needs two extra pointer updates and more memory.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Doubly vs singly linked list · Data Structure and Algorithm, chapter 4 · asked 2×
- Singly vs doubly linked list · Data Structure and Algorithm, chapter 4
- Delete employee from doubly linked list · Data Structure and Algorithm, chapter 4
- Insert and delete before a node, singly linked · 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
- SQL: customers with account in Dharan · Database Management System, chapter 3
- SQL: customer, branch, account details · Database Management System, chapter 3
- Constraint specifications in bank account model · Object Oriented Analysis and Design, chapter 2
- Customer lifetime value and CAC numerical · Entrepreneurship Development, chapter 4