Data structures API

Singly linked list

class SinglyLinkedList

Store values in nodes connected in one direction.

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

add_data(data: T) None

Append data to the end of the list.

Parameters:

data – Value to store in the new tail node.

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

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.

display_linkedlist() None

Print all stored values from first to last.

delete_first() T

Remove and return the first value.

Returns:

The data previously stored at the beginning of the list.

Raises:

IndexError – If the list is empty.

remove_middle(position: int) T

Remove and return data from a middle position.

Parameters:

position – Zero-based position to remove. It must be greater than zero and less than the final position.

Returns:

The removed data.

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

  • IndexError – If position refers to the first or last node, or lies outside the list.

delete_last() T

Remove and return the final value.

Returns:

The data previously stored at the end of the list.

Raises:

IndexError – If the list is empty.