A collection of nodes composed of:

  • the item (piece of data)
  • the pointer to the next node (or null pointer)

Implementation

from random import randint
 
class Node:
	def __init__(self, data: any, next_pointer: int):
		self.data = data
		self.next_pointer = next_pointer
 
class LinkedList:
	def __init__(self, items: list[any]):
		self.nodes = []
		if self.nodes is not []:
			for i in range(len(items)):
				self.nodes.append(
					Node(items[i], i + 1)
				)
			self.nodes[len(self.nodes) - 1].next_pointer = -1
	
	def output(self):
		if self.nodes is []: return
		current_node = 0 # for the sake of simplicity, we use the first node as the starter node
		while current_node != -1:
			print(f"Accessing location {str(current_node)}")
			node_insn = self.nodes[current_node]
			print(node_insn.data)
			current_node = node_insn.next_pointer
 
data = [randint(1, 100) for x in range(5)]
ll = LinkedList(data)
ll.output()