## Chapter 5: Vectors, Lists, and Sequences

• The concept of a sequence, where each object comes before or after another, is fundamental (linear data structure).
• Vector, list and sequence ADTs, each of which represents a collection of linearly arranged elements (sequences) and provides functions for accessing, inserting and removing arbitrary elements.
• Stacks, queues and deques ADTs can be viewed as restricted types of sequences that access only the first and/or last elements.
• The vector ADT is an extension of the concrete array data structure. It provides accessor functions that can index into the middle of the sequence and update functions for adding and removing elements by their indexes.
• We use the term rank to refer to the index of an element in a vector.
• The list ADT is an object-oriented extension of the concrete linked list data structure. It provides accessor and update functions based on an object-oriented encapsulation of the node objects used by linked lists.
• We call these abstract node objects positions to refer to the elements of list ADT.
• The sequence ADT is a unification of the vector and list concepts.

### 5.1 Vectors

5.1.1 The Vector Abstract Data Type
• The rank of an element e in a sequence S is the number of elements that are before e in S.
• A linear sequence that supports access to its elements by their ranks is called a vector.
• The vector ADT extends the notion of array by storing a sequence of arbitrary objects.
• An element can be accessed, inserted or removed by specifying its rank (number of elements preceding it)
• An exception is thrown if an incorrect rank is specified (e.g., a negative rank)
• elemAtRank(int r): returns the element at rank r without removing it
• replaceAtRank(int r, Object o): replace the element at rank r with o
• insertAtRank(int r, Object o): insert a new element o to have rank r
• removeAtRank(int r): removes the element at rank r
• Additional operations: size() and isEmpty()
5.1.2 A Simple Array-Based Implementation
• Use an array V of size N
• A variable n keeps track of the size of the vector (number of elements stored)
• Operation elemAtRank(r) is implemented in O(1) time by returning V[r]

Insertion
• In operation insertAtRank(r,o), we need to make room for the new element by shifting forward the n − r elements V[r], . . ., V[n − 1]
• In the worst case (r = 0), this takes O(n) time

Deletion
• In operation removeAtRank(r), we need to fill the hole left by the removed element by shifting backward the n − r − 1 elements V[r + 1], . . ., V[n − 1]
• In the worst case (r = 0), this takes O(n) time

Performance
• In the array based implementation of a vector ADT
• The space used by the data structure is O(n)
• size, isEmpty, elemAtRank and replaceAtRank run in O(1) time
• insertAtRank and removeAtRank run in O(n) time
• If we use the array in a circular fashion, insertAtRank(0) and removeAtRank(0) run in O(1) time
• In an insertAtRank operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one
5.1.3 An Extendable Array Implementation
html-5.2
html-5.3
Fixed Length Capacity Increment

### 5.2 Lists

5.2.1 Node-Based Operations and Positions

• We view a list ADT as a container of elements that stores each element at a position and positions are arranged in a linear order
• The position ADT models the notion of place within a data structure where a single object is stored
• A special nullposition refers to no object
• Positions provide a unified view of diverse ways of storing data, such as
• a cell of an array
• a node of a linked list
• Member functions:
• Object& element(): returns the element stored at this position
• bool isNull(): returns true if this is a null position
• A position is always defined relatively, that is in term of its neighbors. p will always be "after" some position q and "before" some position s (unless p is the first or last position).
5.2.2 The List Abstract Data Type
• The list ADT models a sequence of positions storing arbitrary objects
• It establishes a before/after relation between positions
• Generic methods:
• size(), isEmpty()
• Query methods:
• isFirst(p), isLast(p)
• Accessor methods:
• first(), last()
• before(p), after(p)
• Update methods:
• replaceElement(p, o), swapElements(p, q)
• insertBefore(p, o), insertAfter(p, o)
• insertFirst(o), insertLast(o)
• remove(p)
html-5.4

• A doubly linked list provides a natural implementation of the list ADT
• Nodes implement positions and store:
• element
• link to the previous node
• link to the next node
• Special trailer and header nodes

html-5.5 (Node)

Using Positions
html-5.6 (Positions)
html-5.7 (InvalidPositionException)

Insertion
We visualize operation insertAfter(p, X), which returns position q

Deletion
We visualize remove(p), where p = last()

html-5.10 (NodeList1)
html-5.11 (NodeList2)

Performance
• In the implementation of the list ADT by means of a doubly linked list
• The space used by a list with n elements is O(n)
• The space used by each position of the list is O(1)
• All the operations of the list ADT run in O(1) time
• Operation element() of the position ADT runs in O(1) time

### 5.3 Sequences

5.3.1 The Sequence Abstract Data Type
• The sequence ADT is the union of the vector and list ADTs
• Elements accessed by
• Rank, or
• Position
• Generic methods:
• size(), isEmpty()
• Vector-based methods:
• elemAtRank(r), replaceAtRank(r, o),
• insertAtRank(r, o), removeAtRank(r)
• List-based methods:
• first(), last(),
• before(p), after(p),
• replaceElement(p, o), swapElements(p, q),
• insertBefore(p, o), insertAfter(p, o),
• insertFirst(o), insertLast(o),
• remove(p)
• Bridge methods:
• atRank(r),
• rankOf(p)
Multiple Inheritance in the Sequence ADT
html-5.12 (Sequence)

5.3.2 Implementing a Sequence with a Doubly Linked List
All of the functions of the list ADT can be easily implemented to run O(1) time.

html-5.13 (NodeSequence1)
html-5.14 (NodeSequence2)

5.3.3 Implementing a Sequence with an Array

• We use a circular array storing positions
• A position object stores:
• Element
• Rank
• Indices f and l keep track of first and last positions

5.3.4 Comparing Sequence Implementations

 Operation Array List size, isEmpty 1 1 atRank, rankOf, elemAtRank 1 n first, last, before, after 1 1 replaceElement, swapElements 1 1 replaceAtRank 1 n insertAtRank, removeAtRank n n insertFirst, insertLast 1 1 insertAfter, insertBefore n 1 remove n 1

### 5.5 Iterators

• An iterator abstracts the process of scanning through a collection of elements
• An iterator consists of a sequence S, a current position in S, and a way of stepping to the next position in S and making it the current position.
5.5.1 Iterator Functions
• We define the ObjectIterator ADT:
• boolean hasNext()
• object next()
• Extends the concept of position by adding a traversal capability
• May be implemented with an array or singly linked list
• An iterator is typically associated with an another data structure
• We can augment the Stack, Queue, Vector, List and Sequence ADTs with method:
• ObjectIterator elements()
• Two notions of iterator:
• snapshot: freezes the contents of the data structure at a given time
• dynamic: follows changes to the data structure
html-16 (ObjectIterator)

STL Iterators
html-17 (STLiterator)

### 5.6 A Hierarchy of Sequence ADTs

5.6.1 Functions of a Container
A container is a data structure that stores and organizes a collection of objects, called the elements of the container, and provides access to them through the functions of an abstract data type.