Java Reference
In-Depth Information
Indeed, you can check that there are no
new
keywords in the body of these
functions.
7.7 Summary on linked lists
Linked lists allow one to consider fully dynamic data structures. It is, however,
quite useful to start building a list from a former array. A list object is nothing
more than a reference to the cell of the linked list, namely a reference to
the head of the list. We have presented
singly chained linked list
for
unidirectionally traversing elements from the head to the tail. It is also possible
to create
doubly chained linked lists
that contain in their class definition two
reference fields beyond the container: Say,
prev
and
next
references.
class
DoublyLinkedList
{
int
container ;
DoublyLinkedList prev , next ;
}
Finally, notice that in Java, we do not need to free unused cells (for example
cells discarded when removing elements) since the
garbage collector
(GC) will
do it automatically on our behalf. Let us now consider an application using
linked lists: hashing.
7.8 Application of linked lists: Hashing
Hashing is a technique for storing a collection of homogeneous objects taken
from a gigantic space into a compact array. To make it more concrete, assume
we have to deal with a collection of strings of type
String
. The potential full
collection of strings is gigantic and is called the
universe
from which objects
are taken. If we consider a given set of
n
object, we would like to store them
eciently in a data-structure so that we can insert new objects and search for
objects in almost constant time. The underpinning principle of hashing is to
first transcode the string into an integer using a conversion procedure named
String2Integer
.
static int
String2Integer (String s)
{
int
result=0;
// this is the method s.hashCode()
for
(
int
j=0;j
<
s . length () ; j++)
result=result
∗
31+s . charAt ( j ) ;
Search WWH ::
Custom Search