ArticleslgStudy

science

Doubly linked list

Doubly linked list is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Doubly linked list rather than just read about it. In short: In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains three fields: two link fields (references to the previous and to the next node in the sequence of nodes) and one data field.

Key takeaways

  • Doubly linked list belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Doubly linked list to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Doubly linked list from memory before moving on to harder problems.

Reference excerpt

In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains three fields: two link fields (references to the previous and to the next node in the sequence of nodes) and one data field. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.

The two node links allow traversal of the list in either direction. While adding or removing a node in a doubly linked list requires changing more links than the same operations on a singly linked list, the operations are simpler and potentially more efficient (for nodes other than first nodes) because there is no need to keep track of the previous node during traversal or no need to traverse the list to find the previous node, so that its link can be modified.

Nomenclature and implementation The first and last nodes of a doubly linked list for all practical applications are immediately accessible (i.e., accessible without traversal, and usually called head and tail) and therefore allow traversal of the list from the beginning or end of the list, respectively: e.g., traversing the list from beginning to end, or from end to beginning, in a search of the list for a node with specific data value. Any node of a doubly linked list, once obtained, can be used to begin a new traversal of the list, in either direction (towards beginning or end), from the given node. The link fields of a doubly linked list node are often called next and previous or forward and backward. The references stored in the link fields are usually implemented as pointers, but (as in any linked data structure), they may also be address offsets or indices into an array where the nodes live.

Basic algorithms Consider the following basic algorithms written in Ada:

Open doubly linked lists record DoublyLinkedNode { next // A reference to the next node prev // A reference to the previous node data // Data or a reference to data }

record DoublyLinkedList { DoublyLinkedNode firstNode // points to first node of list DoublyLinkedNode lastNode // points to last node of list }

Traversing the list Traversal of a doubly linked list can be in either direction. In fact, the direction of traversal can change many times, if desired. Traversal is often called iteration, but that choice of terminology is unfortunate, for iteration has well-defined semantics (e.g., in mathematics) which are not analogous to traversal. Forwards

node := list.firstNode while node ≠ null <do something with node.data> node := node.next

Backwards

node := list.lastNode while node ≠ null <do something with node.data> node := node.prev

Inserting a node These symmetric functions insert a node either after or before a given node:

function insertAfter(List list, Node node, Node newNode) newNode.prev := node if node.next == null newNode.next := null -- (not always necessary) list.lastNode := newNode else newNode.next := node.next node.next.prev := newNode node.next := newNode

function insertBefore(List list, Node node, Node newNode) newNode.next := node if node.prev == null newNode.prev := null -- (not always necessary) list.firstNode := newNode else newNode.prev := node.prev node.prev.next := newNode node.prev := newNode

We also need a function to insert a node at the beginning of a possibly empty list:

function insertBeginning(List list, Node newNode) if list.firstNode == null list.firstNode := newNode list.lastNode := newNode newNode.prev := null newNode.next := null else insertBefore(list, list.firstNode, newNode)

A symmetric function inserts at the end:

function insertEnd(List list, Node newNode) if list.lastNode == null insertBeginning(list, newNode) else insertAfter(list, list.lastNode, newNode)

Removing a node Removal of a node is easier than insertion, but requires special handling if the node to be removed is the firstNode or lastNode:

function remove(List list, Node node) if node.prev == null list.firstNode := node.next else node.prev.next := node.next if node.next == null list.lastNode := node.prev else node.next.prev := node.prev

One subtle consequence of the above procedure is that deleting the last node of a list sets both firstNode and lastNode to null, and so it handles removing the last node from a one-element list correctly. Notice that we also don't need separate "removeBefore" or "removeAfter" methods, because in a doubly linked list we can just use "remove(node.prev)" or "remove(node.next)" where these are valid. This also assumes that the node being removed is guaranteed to exist. If the node does not exist in this list, then some error handling would be required.

Circular doubly linked lists

Traversing the list Assuming that someNode is some node in a non-empty list, this code traverses through that list starting with someNode (any node will do): Forwards

node := someNode do do something with node.value node := node.next while node ≠ someNode

Backwards

node := someNode do do something with node.value node := node.prev while node ≠ someNode

Notice the postponing of the test to the end of the loop. This is important for the case where the list contains only the single node someNode.

Inserting a node This simple function inserts a node into a doubly linked circularly linked list after a given element:

function insertAfter(Node node, Node newNode) newNode.next := node.next newNode.prev := node node.next.prev := newNode node.next := newNode

To do an "insertBefore", we can simply "insertAfter(node.prev, newNode)". Inserting an element in a possibly empty list requires a special function:

function insertEnd(List list, Node node) if list.lastNode == null node.prev := node node.next := node else insertAfter(list.lastNode, node) list.lastNode := node

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Doubly linked list

Start with the simplest possible case. Write down what Doubly linked list claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Doubly linked list before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Doubly linked list ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Doubly linked list

In research
Doubly linked list appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Doubly linked list in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Doubly linked list is common in secondary-school and first-year university syllabi. It links to neighbouring topics Linked lists, so understanding it makes those chapters shorter.
In everyday life
Look for Doubly linked list outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Doubly linked list” →

How to study Doubly linked list in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Doubly linked list means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Doubly linked list out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Doubly linked list in simple terms?

In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains three fields: two link fields (references to the previous and to the next node in the sequence of nodes) and one data field.

Why does Doubly linked list matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Doubly linked list?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Doubly linked list.

Tags

  • Linked lists

Keep exploring