List and iterators Flashcards
Give the code to remove an element from a List ADT that uses an arraylist.
E temp = data[i];
for (int k=i; k < size−1; k++) // shift elements to fill hole
data[k] = data[k+1];
data[size−1] = null; // help garbage collection
size−−;
return temp;
All operations of an arrays & arrayList are O(1)? T/F
False, add and remove are O(n) because of all the shifting that has to take place
Give the code to add an element from a List ADT that uses an arraylist.
if (size == data.length) // not enough capacity
throw new IllegalStateException(“Array is full”);
for (int k=size−1; k >= i; k−−) // start by shifting rightmost
data[k+1] = data[k];
data[i] = e; // ready to place the new element
size++;
@ what index will add & remove of an array/arraylist be running @ O(1) and @ O(n)
when i ==0, O(n) for removal and addition. When i == n-1 then O(1) for both operations
In the case of add(Element e) do we add e to the front or the end of the array and in the case that the array is now full, what happens?
We add at the end of the array and if the array is full, we replace the list with a bigger one
What two methods of increasing the size of an array do we have and how do they work?
- incremental strategy works by increasing the array by a constant each time it is full
- Doubling works by doubling the size of the array each time it is full
In what case would one be more suitable than the other for incremental vs doubling?
Incremental would be more suitable in the case where the array just needs to be +/- c cells bigger. So when we have just a few elements to add instead of doubling and having a lot of cells that were actually not needed
Doubling would be best if we need to add a lot of elements into the cells, otherwise we might find ourselves incrementing lots of times.
What is the Big Oh of the doubling strategy?
T(n) = O(n) and to add a single element is O(1)
Big Oh of the incremental strategy?
T(n) = O(n^2) but a single add operation in the worst case is O(n)
why are postions preferable over indexes
Positions do not change even after elements shift due to deletion or insertion. The position is relative and not absolute. Even if the elemnent itself changes, the position will not
What is the only way that a position in Position ADT is altered?
When the position and its element are explicitly removed
Why would we want to get a position returned
to use it to traverse through the list
Safer because we don’t want the list altered
We shouldn’t be returning nodes
What single method does a Position ADT have
element()
Why would one want to use positions instead of indices for accessing elements?
Positions are relative and not absolute, meaning that even if we remove or add elements on to the data structure, the position will remain the same.
What are the properties/benefits of storing something in a positional list?
- referring to a place without an index
- add after and addbefore(v)
- resitricts acess to the methods that can change our references to other nodes, safe
- not using encapsulation a lot because we expose too much of our implementation detail to the use