A double-ended list is a sequence that provides constant-time operations at both ends: adding/removing from the front and adding/removing from the back. It is typically implemented as a linked structure with explicit references to both the head and the tail. This enables efficient, predictable performance for queue-like and stack-like behaviors in the same structure.
Double-ended lists are closely related to deques (double-ended queues). In many libraries, a deque is the abstract interface, and a double-ended list is one of the concrete ways to implement that interface.
# Node with links in both directions
class Node:
value
prev
next
class DoubleEndedList:
head
tail
size
def __init__():
head = null
tail = null
size = 0
def is_empty():
return size == 0
def push_front(x):
n = new Node()
n.value = x
n.prev = null
n.next = head
if head != null:
head.prev = n
else:
# list was empty; new node is also the tail
tail = n
head = n
size += 1
def push_back(x):
n = new Node()
n.value = x
n.next = null
n.prev = tail
if tail != null:
tail.next = n
else:
# list was empty; new node is also the head
head = n
tail = n
size += 1
def pop_front():
if head == null: raise Empty
x = head.value
head = head.next
if head != null:
head.prev = null
else:
# became empty
tail = null
size -= 1
return x
def pop_back():
if tail == null: raise Empty
x = tail.value
tail = tail.prev
if tail != null:
tail.next = null
else:
# became empty
head = null
size -= 1
return x