Question

In: Computer Science

You must write an appropriate code for all member methods so that a double-linked list functionality...

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
}
}

Solutions

Expert Solution

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;

   }
}


Related Solutions

You must write the correct code for all member methods so that queue functionality can be...
You must write the correct code for all member methods so that queue 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; /** * A queue structure of Movie objects. Only the Movie values contained * in the queue are visible through the standard queue methods. The Movie * values are stored in a DoubleLink object provided in class as attribute....
# List the two private member variables (including name and functionality) in the node class. #Write...
# List the two private member variables (including name and functionality) in the node class. #Write a general pattern for a loop statement that traverses all the nodes of a linked list
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Double Linked List. 5. Insert a new node at the end of a DoubleLinked List 6. Insert a new node after the value 5 of Double Linked List 7. Delete the node with value 6. 8. Search an existing element in a...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
c++ example of a double linked list of chars I need to create a double linked...
c++ example of a double linked list of chars I need to create a double linked list of chars. this should be a class that allows you to input a single character at a time, list the resulting characters, find any character and delete the first example of a character.
write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT