An abstract data type (ADT) defines a conceptual model for how data may be stored and manipulated.
A list ADT is simply a container for values which are ordered in a sequence, where each value has at most one preceding and one succeeding value. A given value may appear more than once in a list.
A list data structure is a concrete implementation of the list ADT in some programming language, which, in addition to adhering to the basic premise of the ADT, will also typically support operations that:
The implementation of any data structure will generally rely on simpler, constituent data types (e.g., "primitive" types offered by the language), the choice of which may affect the runtime complexities of said operations.
class List:
### subscript-based access ###
def __getitem__(self, idx):
"""Implements `x = self[idx]`"""
pass
def __setitem__(self, idx, value):
"""Implements `self[idx] = x`"""
pass
def __delitem__(self, idx):
"""Implements `del self[idx]`"""
pass
### stringification ###
def __repr__(self):
"""Supports inspection"""
return '[]'
def __str__(self):
"""Implements `str(self)`"""
return '[]'
### single-element manipulation ###
def append(self, value):
pass
def insert(self, idx, value):
pass
def pop(self, idx=-1):
pass
def remove(self, value):
pass
### predicates (T/F queries) ###
def __eq__(self, other):
"""Implements `self == other`"""
return True
def __contains__(self, value):
"""Implements `val in self`"""
return True
### queries ###
def __len__(self):
"""Implements `len(self)`"""
return len(self.data)
def min(self):
pass
def max(self):
pass
def index(self, value, i, j):
pass
def count(self, value):
pass
### bulk operations ###
def __add__(self, other):
"""Implements `self + other_array_list`"""
return self
def clear(self):
pass
def copy(self):
pass
def extend(self, other):
pass
### iteration ###
def __iter__(self):
"""Supports iteration (via `iter(self)`)"""
pass
class List:
def append(self, value):
self.data = value
def __getitem__(self, idx):
"""Implements `x = self[idx]`"""
return self.data
def __setitem__(self, idx, value):
"""Implements `self[idx] = x`"""
self.data = value
def __repr__(self):
"""Supports inspection"""
return str(self.data)
l = List()
l.append('hello')
l[0]
l[0] = 'bye'
l[0]
l[10] = 'yo'
l[5]
We need a storage mechanism that allows us to store and keep track of multiple values. In most languages, an appropriate primitive data container would be an array.
But Python doesn't give us primitive arrays!
list
as array¶To use the built-in list as though it were a primitive array, we will constrain ourselves to just the following APIs on a given list lst
:
lst[i]
for getting and setting values at an existing, positive index i
len(lst)
to obtain the number of slotslst.append(None)
to grow the list by one slot at a timedel lst[len(lst)-1]
to delete the last slot in a listWe can assume operation (1) has time complexity O(1), based on our previous analysis of list indexing.
Operation (2) is trivially O(1), since we are merely asking the list for its size (which it keeps track of and just returns).
Operation (3) is a bit more complicated, but, generally speaking, the built-in list implementation does a good job ensuring that it can be incrementally increased in size in O(1) time. Occasionally, expanding a list may require that it be moved in memory to a new location with sufficient space to accommodate all the contiguous values, but this is rare, and the cost is amortized over the many fast operations.
Operation (4) is trivially O(1), since shrinking a list doesn't require any additional memory and doesn't affect any of the values other than the last one.
ArrayList
data structure¶class ArrayList:
def __init__(self):
self.data = []
def append(self, value):
self.data.append(value)
def __getitem__(self, idx):
"""Implements `x = self[idx]`"""
assert(isinstance(idx, int))
return self.data[idx]
def __setitem__(self, idx, value):
"""Implements `self[idx] = x`"""
assert(isinstance(idx, int))
self.data[idx] = value
def __delitem__(self, idx):
"""Implements `del self[idx]`"""
assert(isinstance(idx, int))
for i in range(idx+1, len(self.data)):
self.data[i-1] = self.data[i]
del self.data[len(self.data)-1]
def __len__(self):
"""Implements `len(self)`"""
return len(self.data)
def __repr__(self):
"""Supports inspection"""
return str(self.data)
lst = ArrayList()
lst
for x in range(10):
lst.append(x)
lst[5]
len(lst)
lst
lst['a'] # fails due to assertion
del lst[9]
lst
del lst[0]
lst
lst[-1]
class ArrayList(ArrayList):
def __init__(self):
self.data = []
def append(self, value):
self.data.append(value)
def _normalize_idx(self, idx):
nidx = idx
if nidx < 0:
nidx += len(self.data)
if nidx < 0:
nidx = 0
return nidx
def __getitem__(self, idx):
"""Implements `x = self[idx]`"""
assert(isinstance(idx, int))
idx = self._normalize_idx(idx)
if idx > len(self.data):
raise IndexError()
return self.data[idx]
lst = ArrayList()
for x in range(10):
lst.append(x)
lst[-1]
lst[100]