Homework 3
Double Linked List
Write a class
called DoubleLinkedList which is
a generic double linked list. This link list is similar to the
single linked list that was shown in class except that each
node in addition to having size
and next now
has prev. Download
the driver and the optional
exception classes
(DO NOT MODIFY THE DRIVER!) and write the following
classes:
The class DoubleLinkedList needs to
have the following:
-
Internal class Node which has
-
Instance Variables
- elem of type E
- next of type Node
- prev of type Node
-
Constructors
- Default
- Parameterized (optional)
-
Instance Variables
-
head of type Node which
always points to the beginning of the linked list
-
last of type Node
which always points to the end of the list.
-
size of type int which
always represents the size of the list.
-
Constructor
-
A default constructor that
initializes head to an
empty Node and
sets last to point at the
head.
-
Methods
-
size Returns the size of the
list.
-
returns the size of the list
-
insert Inserts a new
element into the list.
-
params
-
index - The place to insert the new element
-
elem - The element to insert
-
append Inserts a new element
at the end of the list (index = size)
-
params
-
elem - The element to insert
-
get Retrieve an element from
the list
-
params
-
index - The place from which to retrieve an element
-
returns The specified element.
-
delete Deletes an element
at the specified location.
-
params
-
index - The position to delete from the list.
-
returns The element that was deleted.
-
toString Gives a String
representation of the list.
-
returns The elements of the list separated by
spaces, as a String
-
inList Determines whether
a given element is contained in the list.
-
params
-
elem - The element to find
-
returns true if the element is contained in the
list, false otherwise.
Generating lists
Checking prev elements:
Items match!
Comparing ArrayList to your List:
Items match!
Deleting random elements
Checking last element vs the element at index = size - 1:
Items match!
Comparing ArrayList to your List:
Items match!
Testing 'inList' method
Success
Upload the .java file to the dropbox