---
title: "Doubly linked list: bank customer records"
subject: "Data Structure and Algorithm"
chapter: 4
key: "semester_4/data_structure_and_algorithm:doubly-bank-customer"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/doubly-bank-customer
site: BCT Engineering
---

# Doubly linked list: bank customer records

Data Structure and Algorithm · Chapter 4: Linked lists

- Asked in: 2071 Bhadra, 6+4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/doubly-bank-customer
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-4

## Question

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?

## 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.

## Answer

Assume each node stores the customer data and the list is kept sorted by account number:
```c
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
1. If `start == NULL`, print "list empty" and stop.
2. Set `p = start`; while `p != NULL` and `strcmp(p->acc, "00056PR") != 0`, set `p = p->next`.
3. If `p == NULL`, print "customer not found" and stop.
4. If `p->prev != NULL`, set `p->prev->next = p->next`; otherwise (first node) `start = p->next`.
5. If `p->next != NULL`, set `p->next->prev = p->prev`.
6. `free(p)`.

```text
Before: [A] <-> [00056PR] <-> [B]
After : [A] <-> [B]      A->next = B, B->prev = A
```

#### Inserting a new customer (kept in sorted order)
1. Allocate node `new` and fill its data.
2. If the list is empty or `strcmp(new->acc, start->acc) < 0`: `new->prev = NULL; new->next = start;` if `start != NULL` then `start->prev = new`; `start = new`; stop.
3. 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;`
4. `new->next = p->next; new->prev = p;`
5. If `p->next != NULL` then `p->next->prev = new`.
6. `p->next = new`.

```text
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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is customer 00056PR found for deletion?
   A. Traverse from start comparing account numbers with strcmp until a match or NULL
   B. Jump to index 56
   C. Use prev from start
   D. Binary search the list
   Answer: A. A linked list needs sequential search by key. (Key point: Search)

2. After finding p, what changes?
   A. Only p->next = NULL
   B. p->prev->next = p->next and p->next->prev = p->prev
   C. start is always changed
   D. The customer name is blanked
   Answer: B. Neighbours are linked together, with a NULL check at both ends. (Key point: Delete links)

3. How does deletion differ from a singly linked list?
   A. It needs no search
   B. It uses less memory
   C. A doubly list needs no trailing pointer because p->prev gives the predecessor
   D. It requires fewer pointer updates for insert
   Answer: C. The singly list needs a trailing pointer maintained during the search. (Key point: Difference from singly)

4. Inserting a new customer between p and q in a doubly list requires how many link updates?
   A. Two
   B. One
   C. Six
   D. Four: new->prev, new->next, p->next, q->prev
   Answer: D. Singly needs only two links, which is why doubly is more complex. (Key point: Insert links)

## Related in Data Structure and Algorithm

- [Doubly vs singly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-vs-singly-linked) — Data Structure and Algorithm, chapter 4, asked 2×
- [Singly vs doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Asingly-vs-doubly-linked) — Data Structure and Algorithm, chapter 4
- [Delete employee from doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-employee-delete) — Data Structure and Algorithm, chapter 4
- [Insert and delete before a node, singly linked](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Asingly-insert-delete-before) — Data Structure and Algorithm, chapter 4
- [Insert and delete kth node in doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-insert-delete-kth) — Data Structure and Algorithm, chapter 4, asked 2×
- [Delete last node of doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adelete-end-node-doubly) — Data Structure and Algorithm, chapter 4

## Related in other subjects

- [SQL: customers with account in Dharan](https://www.bctengineer.com/question.md?key=semester_6%2Fdatabase_management_system%3Asql-customers-dharan-branch) — Database Management System, chapter 3
- [SQL: customer, branch, account details](https://www.bctengineer.com/question.md?key=semester_6%2Fdatabase_management_system%3Asql-natural-join-details) — Database Management System, chapter 3
- [Constraint specifications in bank account model](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aconstraint-specification-bank-account-model) — Object Oriented Analysis and Design, chapter 2
- [Customer lifetime value and CAC numerical](https://www.bctengineer.com/question.md?key=semester_7%2Fentrepreneurship_development%3Altv-cac-unit-economics) — Entrepreneurship Development, chapter 4
