In this chapter, we will study implementations of the
and
interfaces where the underlying data is stored in an array, called the
backing array.
The following table summarizes the running times
of operations for the data structures presented in this chapter:
![]() ![]() |
![]() ![]() |
|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
In this chapter, and throughout this book, it will be convenient to
have arrays that keep track of their size. The usual C++ arrays do
not do this, so we have defined a class,
, that keeps track
of its length. The implementation of this class is straightforward.
It is implemented as a standard C++ array,
, and an integer,
:
T *a; int length;The size of an
array(int len) { length = len; a = new T[length]; }The elements of an array can be indexed:
T& operator[](int i) { assert(i >= 0 && i < length); return a[i]; }Finally, when one array is assigned to another, this is just a pointer manipulation that takes constant time:
array<T>& operator=(array<T> &b) { if (a != NULL) delete[] a; a = b.a; b.a = NULL; length = b.length; return *this; }