In: Computer Science
You must write an appropriate code for all member methods so that a double-linked list functionality can be obtained. Changes to the class definition, instance variables, and signatures of the membership method are not allowed. This class has no main method.
package javaclass;
/**
* The class for doubly-linked data structures. Provides attributes
* and implementations for getLength, isEmpty, and toArray methods.
* The head attribute is the first node in any doubly-linked list and
* last is the last node.
*
* @author
* @version 2017-11-01
*
*/
public class DoubleLink {
// First node of double linked list
private DoubleNode head = null;
// Number of elements currently stored in linked list
private int length = 0;
// Last node of double linked list.
private DoubleNode last = null;
/**
* Adds a new Movie element to the list at the head position
* before the previous head, if any. Increments the length of the List.
*
* @param value
* The value to be added at the head of the list.
*
* @return true if node is added successfully, else false.
*/
public final boolean addNode(final Movie value) {
//your code here
return false;//you must change this statement as per your requirement
}
/**
* Removes the value at the front of this List.
*
* @return The value at the front of this List.
*/
public Movie removeFront() {
// your code here
return null;//you must change this statement as per your requirement
}
/**
* Returns the head element in the linked structure. Must be copy safe.
*
* @return the head node.
*/
public final DoubleNode getHead() {
//your code here
return null;//you must change this statement as per your requirement
}
/**
* Returns the current number of elements in the linked structure.
*
* @return the value of length.
*/
public final int getLength() {
//your code here
return -1;//you must change this statement as per your requirement
}
/**
* Returns the last node in the linked structure. Must be copy safe.
*
* @return the last node.
*/
public final DoubleNode getLast() {
//your code here
return null;//you must change this statement as per your requirement
}
/**
* Determines whether the double linked list is empty or not.
*
* @return true if list is empty, false otherwise.
*/
public final boolean isEmpty() {
//your code here
return true;//you must change this statement as per your requirement
}
/**
* Returns all the data in the list in the form of an array.
*
* @return The array of Movie elements. Must be copy safe
*/
public final Movie [] toArray() {
//your code here
return null;//you must change this statement as per your requirement
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DoubleNode class
package doubleNode;
/**
*The individual node for a double-linked structure where movies are
stored * objects. This is a double-linked node. The node link can
be updated, * but not node data to prevent data moving between
nodes. * Data structures must be rearranged by moving nodes.
*
* @author
* @version 2010-10-09
*/
public final class DoubleNode {
// Link to the next DoubleNode.
private DoubleNode next = null;
// Link to the previous DoubleNode.
private DoubleNode prev = null;
// The Movie data.
private Movie value = null;
/**
* Creates a new node with data and link to the previous and next
nodes. Not
* copy safe as it accepts a reference to the data rather than a
copy of the
* data.
*
* @param value
* the data to store in the node.
* @param next
* the previous node to link to.
* @param next
* the next node to link to.
*/
public DoubleNode(final Movie value, final DoubleNode prev,
final DoubleNode next) {
//your code here
}
/**
* Returns the next node in the linked structure.
*
* @return The node that follows this node. Must be copy safe.
*/
public final DoubleNode getNext() {
//your code here
return null;//you must change this statement as per
your requirement
}
/**
* Returns the previous node in the linked structure.
*
* @return The node that precedes this node. Must be copy
safe.
*/
public final DoubleNode getPrev() {
//your code here
return null;//you must change this statement as per
your requirement
}
/**
* Returns the node data. Must be copy safe.
*
* @return The data portion of the node.
*/
public final Movie getValue() {
//your code here
return null;//you must change this statement as per
your requirement
}
/**
* Links this node to the next node.
*
* @param next
* The new node to link to.
*/
public final void setNext(final DoubleNode next) {
//your code here
}
/**
* Links this node to the previous node.
*
* @param prev
* The new node to link to.
*/
public final void setPrev(final DoubleNode prev) {
//your code here
}
}
Please find below the testing code snippet fron the operations on doubly linked list along with the sample output:
Please find POJO for movie which was necessary for the completion of code :
---------------------------------------------------------------------------
Movie.java
public class Movie {
private int movieId;
private String name;
public int getMovieId() {
return movieId;
}
public void setMovieId(int movieId) {
this.movieId = movieId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Movie(int movieId, String name) {
super();
this.movieId = movieId;
this.name = name;
}
@Override
public String toString() {
return "Movie [movieId=" + movieId
+ ", name=" + name + "]";
}
}
-------------------------------------------------------------------------------
DoubleNode.java
package org.bnymellon.js;
/**
* The individual node for a double-linked structure where movies
are stored *
* objects. This is a double-linked node. The node link can be
updated, * but
* not node data to prevent data moving between nodes. * Data
structures must be
* rearranged by moving nodes.
*
* @author
* @version 2010-10-09
*/
public final class DoubleNode {
// Link to the next DoubleNode.
private DoubleNode next = null;
// Link to the previous DoubleNode.
private DoubleNode prev = null;
// The Movie data.
private Movie value = null;
/**
* Creates a new node with data and link to the
previous and next nodes. Not
* copy safe as it accepts a reference to the data
rather than a copy of the
* data.
*
* @param value the data to store in the node.
* @param next the previous node to link to.
* @param next the next node to link to.
*/
public DoubleNode(final Movie value, final DoubleNode
prev, final DoubleNode next) {
this.next = next;
this.prev = prev;
this.value = value;
}
/**
* Returns the next node in the linked structure.
*
* @return The node that follows this node. Must be
copy safe.
*/
public final DoubleNode getNext() {
return this.next;
}
/**
* Returns the previous node in the linked
structure.
*
* @return The node that precedes this node. Must be
copy safe.
*/
public final DoubleNode getPrev() {
return this.prev;
}
/**
* Returns the node data. Must be copy safe.
*
* @return The data portion of the node.
*/
public final Movie getValue() {
return this.value;
}
/**
* Links this node to the next node.
*
* @param next The new node to link to.
*/
public final void setNext(final DoubleNode next)
{
if(this.next == null) {
this.next =
next;
next.next =
null;
next.prev =
this;
}else {
next.next =
this.next;
next.prev =
this;
this.next.prev =
next;
this.next =
next;
}
}
/**
* Links this node to the previous node.
*
* @param prev The new node to link to.
*/
public final void setPrev(final DoubleNode prev)
{
if(prev.prev == null) {
this.prev =
prev;
prev.next =
this;
prev.prev =
null;
}else {
this.prev.next =
prev;
prev.prev =
this.prev;
this.prev =
prev;
prev.next =
this;
}
}
}
------------------------------------------------------------------------------------------
DoubleLink.java
package org.bnymellon.js;
public class DoubleLink {
// First node of double linked list
private DoubleNode head = null;
// Number of elements currently stored in linked list
private int length = 0;
// Last node of double linked list.
private DoubleNode last = null;
/**
*
* Adds a new Movie element to the list at the head
position
*
* before the previous head, if any. Increments the
length of the List.
*
*
*
* @param value
*
* The value to be added at the head of the list.
*
*
*
* @return true if node is added successfully, else
false.
*
*/
public final boolean addNode(final Movie value)
{
try {
DoubleNode temp
= new DoubleNode(value, null, null);
if(head ==null )
{
head = temp;
last = head;
}else {
this.head.setPrev(temp);
}
this.head =
temp;
this.length++;
return
true;
}catch(Exception e) {
e.printStackTrace();
return
false;
}
}
/**
*
* Removes the value at the front of this List.
*
*
*
* @return The value at the front of this List.
*
*/
public Movie removeFront() {
DoubleNode front = this.head;
this.head =
this.head.getNext();
this.length--;
return front.getValue();
}
/**
*
* Returns the head element in the linked structure.
Must be copy safe.
*
*
*
* @return the head node.
*
*/
public final DoubleNode getHead() {
return this.head;
}
/**
*
* Returns the current number of elements in the linked
structure.
*
*
*
* @return the value of length.
*
*/
public final int getLength() {
return this.length;
}
/**
*
* Returns the last node in the linked structure. Must
be copy safe.
*
*
*
* @return the last node.
*
*/
public final DoubleNode getLast() {
return this.last;
}
/**
*
* Determines whether the double linked list is empty
or not.
*
*
*
* @return true if list is empty, false
otherwise.
*
*/
public final boolean isEmpty() {
if(this.length == 0) {
return
true;
}else {
return
false;
}
}
/**
*
* Returns all the data in the list in the form of an
array.
*
*
*
* @return The array of Movie elements. Must be copy
safe
*
*/
public final Movie[] toArray() {
Movie[] returnArr = new
Movie[this.length];
DoubleNode curr = this.head;
int i=0;
while(curr!=null) {
returnArr[i] =
curr.getValue();
curr =
curr.getNext();
i++;
}
return returnArr;
}
}