Java Reference
In-Depth Information
9.
Write a method called
deleteBack
that deletes the last value (the value at the back of the list) and returns the
deleted value. If the list is empty, throw a
NoSuchElementException
.
10.
Write a method called
stutter
that doubles the size of a list by replacing every integer in the list with two of that
integer. For example, suppose a variable
list
stores the values
[1, 8, 19, 4, 17]
, after a call of
list.stutter()
, it should store
[1, 1, 8, 8, 19, 19, 4, 4, 17, 17]
.
11.
Write a method called
split
that rearranges the elements of a list so that all of the negative values appear before all of
the nonnegatives. For example, suppose a variable
list
stores the values
[8, 7, -4, 19, 0, 43, -8, -7, 2]
.
The call of
list.split();
should rearrange the list to put the negatives first:
[-4, -8, -7, 8, 7, 19, 0,
43, 2]
. It doesn't matter what order the numbers are in, only that the negatives appear before the nonnegatives, so
this is only one possible solution. You must solve the problem by rearranging the links of the list, not by swapping
data
values or creating new nodes. You also may not use auxiliary structures like arrays or
ArrayList
s to solve
this problem.
12.
Write a method called
transferFrom
that accepts a second linked list as a parameter and that moves values from
the second list to this list. You are to attach the second list's elements to the end of this list. You are also to empty the
second list. For example, suppose two lists called
list1
and
list2
store
[8, 17, 2, 4]
and
[1, 2, 3]
respec-
tively. The call of
list1.transferFrom(list2);
should change
list1
to
[8, 17, 2, 4, 1, 2, 3]
and
list2
to an empty list,
[]
. The order of the arguments matters;
list2.transferFrom(list1);
should change
list1
to an empty list,
[]
,and
list2
to
[1, 2, 3, 8, 17, 2, 4]
. Either of the two lists could be empty, but
you can assume that neither list is
null
. You are not to create any new nodes. Your method should simply change
links of the lists to join them together.
13.
Write a method called
removeAll
that removes all occurrences of a particular value. For example, if a variable
list
stores the values
[3, 9, 4, 2, 3, 8, 17, 4, 3, 18]
, the call of
list.removeAll(3);
would change
the list to store
[9, 4, 2, 8, 17, 4, 18]
.
14.
Write a method called
equals
that accepts a second list as a parameter, returns
true
if the two lists are equal, and
returns
false
otherwise. Two lists are considered equal if they store exactly the same values in exactly the same
order and have exactly the same length.
15.
Write a method called
removeEvens
that removes the values in even-numbered indexes from a list, returning a new
list that contains those values in their original order. For example, consider a variable
list1
that stores the
values
[8, 13, 17, 4, 9, 12, 98, 41, 7, 23, 0, 92]
and imagine that the following call is made:
LinkedIntList list2 = list1.removeEvens();
After the call,
list1
should store
[13, 4, 12, 41, 23, 92]
and
list2
should store
[8, 17, 9, 98, 7, 0]
.
You may not call any methods of the class other than the constructor to solve this problem. You may not create any
new nodes nor change the values stored in
data
fields to solve this problem. You must solve it by rearranging the links
of the list.
16.
Write a method called
removeRange
that accepts a starting and ending index as parameters and removes the
elements at those indexes (inclusive) from the list. For example, if a variable
list
stores the values
[8, 13, 17,
4, 9, 12, 98, 41, 7, 23, 0, 92]
, the call of
listRange.removeRange(3, 8);
should remove values
between index 3 and index 8 (the values
4
and
7
), leaving the list of
[8, 13, 17, 23, 0, 92]
. The method
should throw an
IllegalArgumentException
if either of the positions is negative. Otherwise you may assume
that the positions represent a legal range of the list (0
start
end
size).
Search WWH ::
Custom Search