removeLast
method for the various linked list variations: singly-linked, doubly-linked; one-pointer, two-pointer; linear, circular; with and without header.
The implementations will be prefixed by their flavor: link-type topology #-pointers uses-header; e.g, SL1N — Single-linked, Linear, 1-pointer, No Header
Aside from the programming with nodes and linked representations, the objective is to have you get a feeling for the 'appropriateness' of an implementation wrt a particular
operation, e.g. is a single-linked, linear, single-pointer implementation a good choice when implementing an addLast
method.
removeLast
:
Deque
interface for the implementation to implement
code
directory; all the implementations implement the same interface
DequeDemo
that is essentially the same demo we've been using all along
demo
that receives a
i.e., a concrete class that implements Deque
and puts it through its paces
removeLast
method
CollectionException
class of the sort presented in class, and provided in lab03
.class
and .java
files; I suggest you incoprprate the .class
files into
your IDE's project for the experience, but you are of course free to use the .java file
if you prefer. The skeletal implementations are provided as .java files
only as they are incomplete.
All the above files can be access via the Code
link at the bottom of this page/
Deque
?removeLast
?removeLat
fits that bill: adFirst
and removeFirst
operate at the front of the list eliminating any need to circularity of tail pointers.
While addLast
does require access to the end of the list, removeLast
goes a step farther and requires access to the next-to-last node as well.
removeLast
requires access to both the last and next-to-last nodes. To do this without a traversal
to the next-to-last node require prev references as well as next references, i.e., a doubly-linked node.
while (p != null)
condition for traversal termination
addFirst
, removeFirst
, addLast
).
header
need never be modified; it will always point to the header node.
header
rather than head for the deque's instance variable
removeLast
. You are thus provided with quite a bit of logic — some of it similar to
that used in removeLast
.
size
and toString
removeLast
. You can find similar logic in the size
and
toString
methods.
removeFirst
removeFirst
from removeLast
invoke removeFirst
in that situation. You can see similar logic in the addLast
code (i.e., leveraging addFirst
).
OTOH, if you're interested in getting into the weeds, you might consider avoiding invoking removeFirst
from removeLast
, it forces you to deal with any special case
logic on your own, and may give you greater insight into the advantages/disadvantages of the various implementations.
CollectionException
: a generic exceptiojn class we will use for our collections/containers
Deque
: the Deque interface that all the implementations adhere to
interface Deque { void addFirst(E value); void addLast(E value); E removeFirst(); E removeLast(); int size(); boolean isEmpty(); int capacity(); }
DequeDemo
: a canonical deque demo/test. This has been slight modified from the one presented in the earlier lectures (I've added isolated addFirst/removeFirst
and addLast/removeLast sections in addition to alternate first/last operations).
Each of the following sections contains a link to the implementation's code directory containing the skeletal Deque class source for the particular
implementation. This code is missing the removeList
metho, which you are to supply. The directory also contains the implementation's demo app.
The useless implementations are also listed but have not links.
OTOH, if you are feeling a bit unsure of yourself or are intimidated by manipulating references as 'pointers', you might consider doing 8.11 (DC1H) first — it's actually the simplest as you can read in it's notes section. You might then try 8.01 (SL1N) and then 8.02 (SL1H); the former to get you going with the traversal, and the latter to warm you up to the header node.
Either way, if you manage to complete them all, good job! You should be fairly fluent in woring with linked representations by then.
removeLast
method for each of the following deque implementations. I would recommend you copy the related code into your IDE, add the method, and get it working
(i.e., produce the output as shown in the "The Supporting code" section above), before submitting it to CodeLab (see last section below).
Write the removeLast
method for a Deque
class implemented using a singly-linked, linear, one pointer, no header node list
head
; head
should be null
when the list is empty.
Deque
can be found above.
removeFirst
from removeList
, there's no special logic
removeFirst
removeFirst
, you need to make sure head is set to null when you remove the only remaining node on the list
Write the removeLast
method for a Deque
class implemented using a singly-linked, linear, one pointer, with header node list
header
Deque
can be found above.
Write the removeLast
method for a Deque
class implemented using a singly-linked, linear, two pointers (head and tail)
head
, and the last node is referenced by the
instance variable tail
. Both references are null
when the list is empty.
Deque
can be found above.
null
the other one does as well.
removeFirst
avoids this whole issue.
Write the removeLast
method for a Deque
class implemented using a singly-linked, linear, two pointers (head and tail), with header node list
header
, and the last node is referenced by the
instance variable tail
. tail
should be null
when the list is empty.
Deque
can be found above.
is initialized to null (and thus is null
for an empty list), and references the last node in a non-empty list. This is one of the more annoying implementations:
you have to maintain a header node AND two pointers with essentially no benefit from either.
Write the removeLast
method for a Deque
class implemented using a singly-linked, circular, one pointer (tail), with no header node list.
tail
. tail
should be null
when the list is empty.
Deque
can be found above.
tail
from/to null
; again
calling removeFirst
from removeLast
handles that situation.
DC1N
are two of the rare situations where a do … while
loop comes in handy
This implementation provides no benefits.
Write the removeLast
method for a Deque
class implemented using a doubly-linked, linear, one pointer, no header node list
head
. head
is null
when the list is empty.
Deque
can be found above.
prev
field into consideration
Write the removeLast
method for a Deque
class implemented using a doubly-linked, linear, one pointer, with header node list
header
.
Deque
can be found above.
prev
field into consideration
Write the removeLast
method for a Deque
class implemented using a doubly-linked, linear, two pointers, no header node list
head
, and the last node is referenced by the
instance variable tail
. Both references are null
when the list is empty.
Deque
can be found above.
null
in their prev
and next
fields respectively requires special logic
for the empty-to-singleton (and vice-versa) case.
removeFirst
Write the removeLast
method for a Deque
class implemented using a doubly-linked, linear, two pointers, with header node list
header
, and the last node is referenced by the
instance variable tail
. tail
is null
when the list is empty.
Deque
can be found above.
tail
to/from null
Write the removeLast
method for a Deque
class implemented using a doubly-linked, circular, one pointer (head), no header node list
head
. head
. head
is null
when the list is empty.
Deque
can be found above.
tail
head
to/from null
in the empty-to-singleton cases.
removeLast
by calling removeFirst
null
at either end.
SC1N
are two of the rare situations where a do … while
loop comes in handy
Write the removeLast
method for a Deque
class implemented using a doubly-linked, circular, one pointer, with header node list
header
. head
.
Deque
can be found above.
null
at either end