List and iterators Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Give the code to remove an element from a List ADT that uses an arraylist.

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

All operations of an arrays & arrayList are O(1)? T/F

A

False, add and remove are O(n) because of all the shifting that has to take place

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give the code to add an element from a List ADT that uses an arraylist.

A

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++;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

@ what index will add & remove of an array/arraylist be running @ O(1) and @ O(n)

A

when i ==0, O(n) for removal and addition. When i == n-1 then O(1) for both operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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?

A

We add at the end of the array and if the array is full, we replace the list with a bigger one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What two methods of increasing the size of an array do we have and how do they work?

A
  1. incremental strategy works by increasing the array by a constant each time it is full
  2. Doubling works by doubling the size of the array each time it is full
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In what case would one be more suitable than the other for incremental vs doubling?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the Big Oh of the doubling strategy?

A

T(n) = O(n) and to add a single element is O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Big Oh of the incremental strategy?

A

T(n) = O(n^2) but a single add operation in the worst case is O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

why are postions preferable over indexes

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the only way that a position in Position ADT is altered?

A

When the position and its element are explicitly removed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why would we want to get a position returned

A

to use it to traverse through the list
Safer because we don’t want the list altered
We shouldn’t be returning nodes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What single method does a Position ADT have

A

element()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why would one want to use positions instead of indices for accessing elements?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the properties/benefits of storing something in a positional list?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the properties of iterators?

A

~ it hides away the complexity of traversing through nodes/cells/positions
~ Can check if a next exists and go to the next
~ traversal is independent of collection implementation

17
Q

What do you need to implement an iterator in an array vs LL

A

Array: array and index
LL: pointer to current position, DLL

18
Q

What are some properties of Arraylists?

A

~ elements stored in linear order
~ alias list/seq.
~ index referenced elements

19
Q

In what case would add and remove in an array list at the front run in O(1)?

A

Case the array is circular

20
Q

What methods do iterators have?

A

next and hasnext()

21
Q

Snapshot vs Dynamic

A

Snapshot: maintains a private copy of the sequence of elements at the time the iterator object was made

Dynamic: does not maintain the sequence but instead change with the data structure/sequence of elements

22
Q

When would a for-each loop be ideal to use?

A
  1. you don’t know how many elements are in a iterable collection
  2. you need to access each element of an iterable collection without using an index
23
Q

Position iterators

A
have positions() that will bring back an iterable object to be used to iterate through the positions
2. keeps the newest iterator as the valid one to avoid collisions
24
Q

Properties of a seq.?

A

~ multiple inheritence because it implements lists and array
~ access via indexes and positions
~ union of arraylists and positional lists
~ bridge methods: atIndex(i), indexOf(p)