Data structures API

Doubly linked list

class DoublyLinkedList

Stores values in nodes connected bidirectional.

Nodes and list pointers are private implementation details. Public methods accept zero-based positions and return stored data rather than exposing mutable node objects.

append(data: T) → None

Append data to the end of the list.

Parameters:

data – Value to store in the new next node.

prepend(data: T) → None

Prepend data to the beginning of the list.

Parameters:

data – Value to store in the new node.

insert_middle(position: int, data: T) → None

Insert data before an existing middle position.

Parameters:
  • position – Zero-based insertion position. It must be greater than zero and less than the current list length.

  • data – Value to store in the inserted node.

Raises:
  • TypeError – If position is not an integer.

  • IndexError – If position is zero, is an append position, or lies outside the list.

delete_head()

Remove and return the first value.

Returns:

The data previously stored at the beginning of the list.

Raises:

IndexError – If the list is empty.

delete_tail()

Remove and return the final value.

Returns:

The data previously stored at the end of the list.

Raises:

IndexError – If the list is empty.

remove(data: T) → T | None

Remove and return the first matching value.

Parameters:

data – Value to search for and remove.

Returns:

The removed value, or None if the value was not found.

Raises:

IndexError – If the list is empty.

display(forward=True)

Prints all stored values in the list.

Parameters:

forward –

  • If True, display from head to tail.

  • If False, display from tail to head.

display_node(position: int) → T

Return the data stored at a zero-based position.

Returning data instead of a node keeps list connections private.

Parameters:

position – Zero-based position to retrieve.

Returns:

The data stored at position.

Raises:
  • TypeError – If position is not an integer.

  • IndexError – If position lies outside the list.