Working with Positions in a List

A playlist, an editing history, and a sequence of laboratory readings all need entries kept in a particular order. The List ADT, or list abstract data type, describes a finite sequence and the operations available at its positions. You use its rules to predict what a program does before considering how the entries are stored. An array is one possible implementation; its storage choices affect the work required, while the list's observable behaviour stays the same.

Positions, values, and size

An index is an integer identifying an entry's position. Here, indices start at zero. For a list containing nn entries, the first index is 00 and the last is n−1n-1:


L=⟨a0,a1,…,an−1⟩,size⁡(L)=n.L = \langle a_0,a_1,\ldots,a_{n-1}\rangle, \qquad \operatorname{size}(L)=n.


The sequence has an order, but that order need not be alphabetical or numerical. A list also permits repeated values. Two equal readings can occupy different positions and represent different observations. Removing one occurrence does not automatically remove the other.


Four list entries occupy consecutive positions, with the same value appearing at two different indices.


An empty list has size zero and no valid entry index. Nevertheless, it has one valid insertion position: index zero. This distinction matters because insertion creates an entry rather than accessing one that already exists.



What the operations promise

The following operation names specify the behaviour used here. A particular programming library may use different names, so read its operation contract before translating these rules into code.


Operation

Effect

Valid index before the operation

get(i)

Returns the value at position ii without changing the list

0≤i<n0 \leq i < n

set(i, x)

Replaces the value at position ii with xx

0≤i<n0 \leq i < n

insert(i, x)

Places xx at position ii, moving later entries one position right

0≤i≤n0 \leq i \leq n

remove(i)

Removes and returns the entry at position ii, moving later entries one position left

0≤i<n0 \leq i < n

size()

Returns the number of entries

No index required


Insertion at index nn appends an entry at the end. Replacement does not insert anything: set changes a value but preserves the size and the positions of all other entries.


Check an index against the current size before each operation. In a sequence of operations, the original size may no longer apply. An index that was valid before removal might be outside the resulting list.

What an array implementation changes

A contiguous array implementation keeps the logical entries in consecutive slots. Its capacity CC is the number of allocated slots, whereas its size nn is the number currently occupied by list entries. Thus 0≤n≤C0\leq n\leq C. Spare slots are storage space, not additional list entries.


With spare capacity and no resizing, insertion at ii shifts the existing entries from ii through n−1n-1 right. Removal shifts the entries after ii left:


insertion shifts=n−i,removal shifts=n−1−i.\begin{aligned} \text{insertion shifts} &= n-i,\\ \text{removal shifts} &= n-1-i. \end{aligned}


In each formula, nn is the size immediately before that operation. These counts measure moves of existing entries; they exclude storing the inserted value or retrieving the removed value.


Example: A list of seven entries with spare capacity needs five shifts to insert at index two. Removing index two from a list of seven entries needs four shifts because the removed entry itself is not shifted.



Direct access and replacement need no shifts. Appending also needs no shifts when spare capacity exists. If insertion requires a larger array, copying into that array adds work beyond the shift count above.

What to remember

  • A list preserves sequence order and permits repeated values.
  • Access, replacement, and removal require an existing position; insertion also allows position nn.
  • Replacement preserves size; insertion increases it by one; removal decreases it by one.
  • Array shift counts depend on the current size and operation position, not on unused capacity.

Example: Editing a Playlist by Position

A music application stores track identifiers in a zero-based List ADT backed by a contiguous array of capacity nine. Its initial sequence is ⟨210,340,210,560,780⟩\langle210,340,210,560,780\rangle. Repeated identifiers represent repeated plays of the same track. A shift moves an existing entry one array slot left or right to preserve list order. Assume the array does not resize, and count shifts separately from writing a new or replacement value.


The initial playlist contains five track identifiers at indices zero through four.


a) Execute insert(2, 425), placing 425 before the entry currently at index two. Find the resulting sequence, size, and number of shifts.


The initial size is ________. Index two currently contains the second occurrence of 210. Inserting there preserves both occurrences and moves the entries at indices ________ one position right.


The number of shifts is 3, calculated from


n−i=5−2=3.n-i=5-2=3.


To avoid overwriting entries during this array insertion, move 780 from index four to five, then 560 from three to four, and then 210 from two to three. Finally, store 425 at index two.


The resulting sequence is


⟨210,340,425,210,560,780⟩.\boxed{\langle210,340,425,210,560,780\rangle}.


The new size is 6 entries, with 3 shifts. Capacity remains nine because the allocated array has enough space.


b) On the updated list, execute remove(4). Find the removed identifier, resulting sequence, and number of shifts.


The size immediately before removal is ________. In the updated list, index four contains 560. The 780 after it moves from index ________ to index four to close the gap.


The number of shifts is 1, calculated from


n−1−i=6−1−4=1.n-1-i=6-1-4=1.


The removal returns 560, leaving


⟨210,340,425,210,780⟩.\boxed{\langle210,340,425,210,780\rangle}.


The new size is 5 entries, with 1 shift. The unused slots do not become list entries, and the allocated capacity remains nine.


c) On the latest list, execute set(1, 690), replacing the entry at index one. Then evaluate get(3). State the final sequence, returned value, size, and capacity.


Replacement changes a value without creating a new position. Index one contains 340, so replace that value with 690. No other entry moves, and the replacement requires zero shifts.


The final sequence is


⟨210,690,425,210,780⟩.\boxed{\langle210,690,425,210,780\rangle}.


Reading index three returns 210. This reads the surviving occurrence at that position without changing it. The final size is 5 entries and capacity is 9 entries. As a check, the insertion and removal cancel in the size count, while replacement and access leave size unchanged:


5+1−1=5.5+1-1=5.

An arrival register starts as an empty List ADT. It appends Maya, then Eli, then Maya again, recording every arrival. Which sequence should it contain after these operations?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A sensor application uses sequence equality: two lists are equal when they have the same length and equal values at every corresponding index. Indices start at zero.


The recorded lists are A=[12,19,12,25]A=[12,19,12,25] and B=[12,12,19,25]B=[12,12,19,25]. Although they contain the same values with the same frequencies, corresponding positions must also agree.


Comparing from left to right, the first unequal pair occurs at index .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A zero-based list initially contains ⟨12,27,45,68⟩\langle12,27,45,68\rangle. Execute the operations in the table from top to bottom. insert(i, x) inserts before the existing entry at ii; remove(i) removes the entry at ii; set(i, x) replaces it. Enter the exact integer size and the value returned by get(1) immediately after each operation.

Operation just completedSizeValue at index 1
insert(1, 33)
remove(3)
set(1, 52)

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice