Exam 2 Extra Flashcards

1
Q

What is the big -O for the single-linked list get operation

A

The list must be searched for in the index, thus O(n)

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

What is the big-O for the set operation

A

The list must be searched for the index, thus O(n)

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

What is the big-O for each add method?

A

Since a reference to the tail of the list is available, the add method is constant or O(1)

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

Each node in a single-linked list, has a reference to _______ and _______

A

The data

The next node

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

In a double-linked list, each node has a reference to _________, _________, and __________

A

Data
The next node
The previous node

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

To remove an item from a single-linked list, you need a reference to ____________.

A

The previous node

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

To remove an item from a double-linked list, you need a reference to _________.

A

the node

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

Why don’t we implement the Orderedlist by extending What would happen if someone called the add method? How about the set method?

A

extending the Linkedlist class we expose all of the public of the. If either the add or set method were called the invariant were ordered could be violated.

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

What other methods in the List interface could we include in the orderedlist class?

A
hashCode
indexOf 
clear 
isEmpty 
subList 
contains 
containsAll 
equals 
lastIndexOf 
removeAll 
toArray
retainAll
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why don’t we a listiterator method for the OrderedList class?

A

If we implement the listIterator method using delegation, we would obtain an instance of ListIterator that supported the add and set methods. however, we could have defined own ListIterator class that delegated to the ListIterator returned the List , but did not pass the add and set methods through

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

Why didn’t we write the hasPrevious method as follows?

public boolean hasPrevious () {
return nextItem.prev != null || (nextItem == null && size != 0);

A

If nextItem was null, then the expression nextItem.prev would result in a NullPointerException .

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

Why must we call next or previous before we call remove?

A

A call to next or previous sets lastItemReturned; if this is not set, then NullPointerException will be thrown

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

What happens if we call remove after we call add What does the Java API documentation say? What does our implementation do?

A

Java documentation says that IllegalStateException is thrown if remove called after a call to add oun implementation will NullPointerException lastItemReturned is set to null by add

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

Why don’t implement the OrderedList by extending Linkedlist? What would happen if someone called the add method ? How about the set method ?

A

By extending the LinkedList class we expose all of the public methods of the Linkedlist class. either the add set method were called the invariant that the items were ordered could be violated.

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

Why don’t we provide a listIterator method for the Orderedlist class?

A

If we implement the listIterator method using delegation, we would obtain an instance of ListIterator that supported the add and set methods. However, we could have defined our own ListIterator class that delegated to the ListIterator from the underlying List class, but did not pass the add and set

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