Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

First code: // This is an assignment for students to complete after reading Chap

ID: 3799719 • Letter: F

Question


First code:

// This is an assignment for students to complete after reading Chapter 4 of // "Data Structures and Other Objects Using Java" by Michael Main.

/****************************************************************************** * This class is a homework assignment; * A DoubleLinkedSeq is a collection of double numbers. * The sequence can have a special "current element," which is specified and * accessed through four methods that are not available in the sequence class * (start, getCurrent, advance and isCurrent). * * <dl><dt><b>Limitations:</b> * Beyond Int.MAX_VALUE elements, the size method * does not work. * * <dt><b>Note:</b><dd> * This file contains only blank implementations ("stubs") * because this is a Programming Project for my students. * * <dt><b>Outline of Java Source Code for this class:</b><dd> * <A href="../../../../edu/colorado/collections/DoubleLinkedSeq.java"> * http://www.cs.colorado.edu/~main/edu/colorado/collections/DoubleLinkedSeq.java * </A> * </dl> * * @version * Jan 24, 1999 ******************************************************************************/ public class DoubleLinkedSeq implements Cloneable {
/** * Initialize an empty sequence. * @param - none * <dt><b>Postcondition:</b><dd> * This sequence is empty. **/    public DoubleLinkedSeq( ) { // Implemented by student. }    /** * Add a new element to this sequence, after the current element. * @param element</CODE> * the new element that is being added * <dt><b>Postcondition:</b><dd> * A new copy of the element has been added to this sequence. If there was * a current element, then the new element is placed after the current * element. If there was no current element, then the new element is placed * at the end of the sequence. In all cases, the new element becomes the * new current element of this sequence. * @exception OutOfMemoryError * Indicates insufficient memory for a new node. **/ public void addAfter(int element) { // Implemented by student. }

/** * Add a new element to this sequence, before the current element. * @param element</CODE> * the new element that is being added * <dt><b>Postcondition:</b><dd> * A new copy of the element has been added to this sequence. If there was * a current element, then the new element is placed before the current * element. If there was no current element, then the new element is placed * at the start of the sequence. In all cases, the new element becomes the * new current element of this sequence. * @exception OutOfMemoryError * Indicates insufficient memory for a new node. **/ public void addBefore(int element) { // Implemented by student. } /** * Place the contents of another sequence at the end of this sequence. * @param addend</CODE> * a sequence whose contents will be placed at the end of this sequence * <dt><b>Precondition:</b><dd> * The parameter, addend</CODE>, is not null. * <dt><b>Postcondition:</b><dd> * The elements from addend</CODE> have been placed at the end of * this sequence. The current element of this sequence remains where it * was, and the addend</CODE> is also unchanged. * @exception NullPointerException * Indicates that addend</CODE> is null. * @exception OutOfMemoryError * Indicates insufficient memory to increase the size of this sequence. **/ public void addAll(DoubleLinkedSeq addend) { // Implemented by student. }    /** * Move forward, so that the current element is now the next element in * this sequence. * @param - none * <dt><b>Precondition:</b><dd> * isCurrent()</CODE> returns true. * <dt><b>Postcondition:</b><dd> * If the current element was already the end element of this sequence * (with nothing after it), then there is no longer any current element. * Otherwise, the new element is the element immediately after the * original current element. * @exception IllegalStateException * Indicates that there is no current element, so * advance</CODE> may not be called. **/ public void advance( ) { // Implemented by student. } /** * Generate a copy of this sequence. * @param - none * @return * The return value is a copy of this sequence. Subsequent changes to the * copy will not affect the original, nor vice versa. * @exception OutOfMemoryError * Indicates insufficient memory for creating the clone. **/ public DoubleLinkedSeq clone( ) { // Clone a DoubleLinkedSeq object. // Student will replace this return statement with their own code: return null; }
/** * Create a new sequence that contains all the elements from one sequence * followed by another. * @param s1</CODE> * the first of two sequences * @param s2</CODE> * the second of two sequences * <dt><b>Precondition:</b><dd> * Neither s1 nor s2 is null. * @return * a new sequence that has the elements of s1</CODE> followed by the * elements of s2</CODE> (with no current element) * @exception NullPointerException. * Indicates that one of the arguments is null. * @exception OutOfMemoryError * Indicates insufficient memory for the new sequence. **/    public static DoubleLinkedSeq concatenation(DoubleLinkedSeq s1, DoubleLinkedSeq s2) { // Student will replace this return statement with their own code: return null; }

/** * Accessor method to get the current element of this sequence. * @param - none * <dt><b>Precondition:</b><dd> * isCurrent()</CODE> returns true. * @return * the current element of this sequence * @exception IllegalStateException * Indicates that there is no current element, so * getCurrent</CODE> may not be called. **/ public double getCurrent( ) { // Student will replace this return statement with their own code: return 0; }

/** * Accessor method to determine whether this sequence has a specified * current element that can be retrieved with the * getCurrent</CODE> method. * @param - none * @return * true (there is a current element) or false (there is no current element at the moment) **/ public boolean isCurrent( ) { // Student will replace this return statement with their own code: return true; }    /** * Remove the current element from this sequence. * @param - none * <dt><b>Precondition:</b><dd> * isCurrent()</CODE> returns true. * <dt><b>Postcondition:</b><dd> * The current element has been removed from this sequence, and the * following element (if there is one) is now the new current element. * If there was no following element, then there is now no current * element. * @exception IllegalStateException * Indicates that there is no current element, so * removeCurrent</CODE> may not be called. **/ public void removeCurrent( ) { // Implemented by student. } /** * Determine the number of elements in this sequence. * @param - none * @return * the number of elements in this sequence **/ public int size( ) { // Student will replace this return statement with their own code: return 0; } /** * Set the current element at the front of this sequence. * @param - none * <dt><b>Postcondition:</b><dd> * The front element of this sequence is now the current element (but * if this sequence has no elements at all, then there is no current * element). **/ public void start( ) { // Implemented by student. } }
// This is an assignment for students to complete after reading Chapter 4 of // "Data Structures and Other Objects Using Java" by Michael Main.

/****************************************************************************** * This class is a homework assignment; * A DoubleLinkedSeq is a collection of double numbers. * The sequence can have a special "current element," which is specified and * accessed through four methods that are not available in the sequence class * (start, getCurrent, advance and isCurrent). * * <dl><dt><b>Limitations:</b> * Beyond Int.MAX_VALUE elements, the size method * does not work. * * <dt><b>Note:</b><dd> * This file contains only blank implementations ("stubs") * because this is a Programming Project for my students. * * <dt><b>Outline of Java Source Code for this class:</b><dd> * <A href="../../../../edu/colorado/collections/DoubleLinkedSeq.java"> * http://www.cs.colorado.edu/~main/edu/colorado/collections/DoubleLinkedSeq.java * </A> * </dl> * * @version * Jan 24, 1999 ******************************************************************************/ public class DoubleLinkedSeq implements Cloneable {
/** * Initialize an empty sequence. * @param - none * <dt><b>Postcondition:</b><dd> * This sequence is empty. **/    public DoubleLinkedSeq( ) { // Implemented by student. }    /** * Add a new element to this sequence, after the current element. * @param element</CODE> * the new element that is being added * <dt><b>Postcondition:</b><dd> * A new copy of the element has been added to this sequence. If there was * a current element, then the new element is placed after the current * element. If there was no current element, then the new element is placed * at the end of the sequence. In all cases, the new element becomes the * new current element of this sequence. * @exception OutOfMemoryError * Indicates insufficient memory for a new node. **/ public void addAfter(int element) { // Implemented by student. }

/** * Add a new element to this sequence, before the current element. * @param element</CODE> * the new element that is being added * <dt><b>Postcondition:</b><dd> * A new copy of the element has been added to this sequence. If there was * a current element, then the new element is placed before the current * element. If there was no current element, then the new element is placed * at the start of the sequence. In all cases, the new element becomes the * new current element of this sequence. * @exception OutOfMemoryError * Indicates insufficient memory for a new node. **/ public void addBefore(int element) { // Implemented by student. } /** * Place the contents of another sequence at the end of this sequence. * @param addend</CODE> * a sequence whose contents will be placed at the end of this sequence * <dt><b>Precondition:</b><dd> * The parameter, addend</CODE>, is not null. * <dt><b>Postcondition:</b><dd> * The elements from addend</CODE> have been placed at the end of * this sequence. The current element of this sequence remains where it * was, and the addend</CODE> is also unchanged. * @exception NullPointerException * Indicates that addend</CODE> is null. * @exception OutOfMemoryError * Indicates insufficient memory to increase the size of this sequence. **/ public void addAll(DoubleLinkedSeq addend) { // Implemented by student. }    /** * Move forward, so that the current element is now the next element in * this sequence. * @param - none * <dt><b>Precondition:</b><dd> * isCurrent()</CODE> returns true. * <dt><b>Postcondition:</b><dd> * If the current element was already the end element of this sequence * (with nothing after it), then there is no longer any current element. * Otherwise, the new element is the element immediately after the * original current element. * @exception IllegalStateException * Indicates that there is no current element, so * advance</CODE> may not be called. **/ public void advance( ) { // Implemented by student. } /** * Generate a copy of this sequence. * @param - none * @return * The return value is a copy of this sequence. Subsequent changes to the * copy will not affect the original, nor vice versa. * @exception OutOfMemoryError * Indicates insufficient memory for creating the clone. **/ public DoubleLinkedSeq clone( ) { // Clone a DoubleLinkedSeq object. // Student will replace this return statement with their own code: return null; }
/** * Create a new sequence that contains all the elements from one sequence * followed by another. * @param s1</CODE> * the first of two sequences * @param s2</CODE> * the second of two sequences * <dt><b>Precondition:</b><dd> * Neither s1 nor s2 is null. * @return * a new sequence that has the elements of s1</CODE> followed by the * elements of s2</CODE> (with no current element) * @exception NullPointerException. * Indicates that one of the arguments is null. * @exception OutOfMemoryError * Indicates insufficient memory for the new sequence. **/    public static DoubleLinkedSeq concatenation(DoubleLinkedSeq s1, DoubleLinkedSeq s2) { // Student will replace this return statement with their own code: return null; }

/** * Accessor method to get the current element of this sequence. * @param - none * <dt><b>Precondition:</b><dd> * isCurrent()</CODE> returns true. * @return * the current element of this sequence * @exception IllegalStateException * Indicates that there is no current element, so * getCurrent</CODE> may not be called. **/ public double getCurrent( ) { // Student will replace this return statement with their own code: return 0; }

/** * Accessor method to determine whether this sequence has a specified * current element that can be retrieved with the * getCurrent</CODE> method. * @param - none * @return * true (there is a current element) or false (there is no current element at the moment) **/ public boolean isCurrent( ) { // Student will replace this return statement with their own code: return true; }    /** * Remove the current element from this sequence. * @param - none * <dt><b>Precondition:</b><dd> * isCurrent()</CODE> returns true. * <dt><b>Postcondition:</b><dd> * The current element has been removed from this sequence, and the * following element (if there is one) is now the new current element. * If there was no following element, then there is now no current * element. * @exception IllegalStateException * Indicates that there is no current element, so * removeCurrent</CODE> may not be called. **/ public void removeCurrent( ) { // Implemented by student. } /** * Determine the number of elements in this sequence. * @param - none * @return * the number of elements in this sequence **/ public int size( ) { // Student will replace this return statement with their own code: return 0; } /** * Set the current element at the front of this sequence. * @param - none * <dt><b>Postcondition:</b><dd> * The front element of this sequence is now the current element (but * if this sequence has no elements at all, then there is no current * element). **/ public void start( ) { // Implemented by student. } }


Second code:
A DoubleNode provides a node for a linked list with * double data in each node. * * @note * Lists of nodes can be made of any length, limited only by the amount of * free memory in the heap. But beyond Integer.MAX_VALUE (2,147,483,647), * the answer from listLength is incorrect because of arithmetic * overflow. * * @see * <A href="../../../../edu/colorado/nodes/DoubleNode.java"> * Java Source Code for this class * (www.cs.colorado.edu/~main/edu/colorado/nodes/DoubleNode.java) </A> * * @author Michael Main * <A href="mailto:main@colorado.edu"> (main@colorado.edu) </A> * * @version * March 6, 2002 * * @see Node * @see BooleanNode * @see ByteNode * @see CharNode * @see FloatNode * @see IntNode * @see LongNode * @see ShortNode ******************************************************************************/ public class DoubleNode { // Invariant of the DoubleNode class: // 1. The node's double data is in the instance variable data. // 2. For the final node of a list, the link part is null. // Otherwise, the link part is a reference to the // next node of the list. private double data; private DoubleNode link;   

/** * Initialize a node with a specified initial data and link to the next * node. Note that the initialLink may be the null reference, * which indicates that the new node has nothing after it. * @param initialData * the initial data of this new node * @param initialLink * a reference to the node after this new node--this reference may be null * to indicate that there is no node after this new node. * @postcondition * This node contains the specified data and link to the next node. **/    public DoubleNode(double initialData, DoubleNode initialLink) { data = initialData; link = initialLink; }

/** * Modification method to add a new node after this node.    * @param item * the data to place in the new node * @postcondition * A new node has been created and placed after this node. * The data for the new node is item. Any other nodes * that used to be after this node are now after the new node. * @exception OutOfMemoryError * Indicates that there is insufficient memory for a new * DoubleNode. **/ public void addNodeAfter(double item)    { link = new DoubleNode(item, link); } /** * Accessor method to get the data from this node.    * @param - none * @return * the data from this node **/ public double getData( )    { return data; } /** * Accessor method to get a reference to the next node after this node. * @param - none * @return * a reference to the node after this node (or the null reference if there * is nothing after this node) **/ public DoubleNode getLink( ) { return link;    }       /** * Copy a list. * @param source * the head of a linked list that will be copied (which may be * an empty list in where source is null) * @return * The method has made a copy of the linked list starting at * source. The return value is the head reference for the * copy. * @exception OutOfMemoryError * Indicates that there is insufficient memory for the new list.    **/ public static DoubleNode listCopy(DoubleNode source) { DoubleNode copyHead; DoubleNode copyTail;    // Handle the special case of the empty list. if (source == null) return null; // Make the first node for the newly created list. copyHead = new DoubleNode(source.data, null); copyTail = copyHead;    // Make the rest of the nodes for the newly created list. while (source.link != null) { source = source.link; copyTail.addNodeAfter(source.data); copyTail = copyTail.link; } // Return the head reference for the new list. return copyHead; } /** * Copy a list, returning both a head and tail reference for the copy. * @param source * the head of a linked list that will be copied (which may be * an empty list in where source is null) * @return * The method has made a copy of the linked list starting at * source. The return value is an * array where the [0] element is a head reference for the copy and the [1] * element is a tail reference for the copy. * @exception OutOfMemoryError * Indicates that there is insufficient memory for the new list.    **/ public static DoubleNode[ ] listCopyWithTail(DoubleNode source) { DoubleNode copyHead; DoubleNode copyTail; DoubleNode[ ] answer = new DoubleNode[2]; // Handle the special case of the empty list.    if (source == null) return answer; // The answer has two null references .    // Make the first node for the newly created list. copyHead = new DoubleNode(source.data, null); copyTail = copyHead;    // Make the rest of the nodes for the newly created list. while (source.link != null) { source = source.link; copyTail.addNodeAfter(source.data); copyTail = copyTail.link; }    // Return the head and tail references. answer[0] = copyHead; answer[1] = copyTail; return answer; } /** * Compute the number of nodes in a linked list. * @param head * the head reference for a linked list (which may be an empty list * with a null head) * @return * the number of nodes in the list with the given head * @note * A wrong answer occurs for lists longer than Int.MAX_VALUE.    **/    public static int listLength(DoubleNode head) { DoubleNode cursor; int answer;    answer = 0; for (cursor = head; cursor != null; cursor = cursor.link) answer++;    return answer; }
/** * Copy part of a list, providing a head and tail reference for the new copy. * @param start/end * references to two nodes of a linked list * @param copyHead/copyTail * the method sets these to refer to the head and tail node of the new * list that is created * @precondition * start and end are non-null references to nodes * on the same linked list, * with the start node at or before the end node. * @return * The method has made a copy of the part of a linked list, from the * specified start node to the specified end node. The return value is an * array where the [0] component is a head reference for the copy and the * [1] component is a tail reference for the copy. * @exception IllegalArgumentException * Indicates that start and end are not references * to nodes on the same list. * @exception NullPointerException * Indicates that start is null. * @exception OutOfMemoryError * Indicates that there is insufficient memory for the new list. **/    public static DoubleNode[ ] listPart(DoubleNode start, DoubleNode end) { DoubleNode copyHead; DoubleNode copyTail; DoubleNode cursor; DoubleNode[ ] answer = new DoubleNode[2];    // Make the first node for the newly created list. Notice that this will // cause a NullPointerException if start is null. copyHead = new DoubleNode(start.data, null); copyTail = copyHead; cursor = start;    // Make the rest of the nodes for the newly created list. while (cursor != end) { cursor = cursor.link; if (cursor == null) throw new IllegalArgumentException ("end node was not found on the list"); copyTail.addNodeAfter(cursor.data); copyTail = copyTail.link; }    // Return the head and tail references answer[0] = copyHead; answer[1] = copyTail; return answer; } /** * Find a node at a specified position in a linked list. * @param head * the head reference for a linked list (which may be an empty list in * which case the head is null) * @param position * a node number * @precondition * position > 0. * @return * The return value is a reference to the node at the specified position in * the list. (The head node is position 1, the next node is position 2, and * so on.) If there is no such position (because the list is too short), * then the null reference is returned. * @exception IllegalArgumentException * Indicates that position is not positive. **/    public static DoubleNode listPosition(DoubleNode head, int position) { DoubleNode cursor; int i;    if (position <= 0) throw new IllegalArgumentException("position is not positive");    cursor = head; for (i = 1; (i < position) && (cursor != null); i++) cursor = cursor.link;
return cursor; }

/** * Search for a particular piece of data in a linked list. * @param head * the head reference for a linked list (which may be an empty list in * which case the head is null) * @param target * a piece of data to search for * @return * The return value is a reference to the first node that contains the * specified target. If there is no such node, the null reference is * returned.    **/    public static DoubleNode listSearch(DoubleNode head, double target) { DoubleNode cursor;    for (cursor = head; cursor != null; cursor = cursor.link) if (target == cursor.data) return cursor;    return null; }
/** * Modification method to remove the node after this node.    * @param - none * @precondition * This node must not be the tail node of the list. * @postcondition * The node after this node has been removed from the linked list. * If there were further nodes after that one, they are still * present on the list. * @exception NullPointerException * Indicates that this was the tail node of the list, so there is nothing * after it to remove. **/ public void removeNodeAfter( )    { link = link.link; } /** * Modification method to set the data in this node.    * @param newData * the new data to place in this node * @postcondition * The data of this node has been set to newData. **/ public void setData(double newData)    { data = newData; }    /** * Modification method to set the link to the next node after this node. * @param newLink * a reference to the node that should appear after this node in the linked * list (or the null reference if there is no node after this node) * @postcondition * The link to the node after this node has been set to newLink. * Any other node (that used to be in this link) is no longer connected to * this node. **/ public void setLink(DoubleNode newLink) { link = newLink; } } A DoubleNode provides a node for a linked list with * double data in each node. * * @note * Lists of nodes can be made of any length, limited only by the amount of * free memory in the heap. But beyond Integer.MAX_VALUE (2,147,483,647), * the answer from listLength is incorrect because of arithmetic * overflow. * * @see * <A href="../../../../edu/colorado/nodes/DoubleNode.java"> * Java Source Code for this class * (www.cs.colorado.edu/~main/edu/colorado/nodes/DoubleNode.java) </A> * * @author Michael Main * <A href="mailto:main@colorado.edu"> (main@colorado.edu) </A> * * @version * March 6, 2002 * * @see Node * @see BooleanNode * @see ByteNode * @see CharNode * @see FloatNode * @see IntNode * @see LongNode * @see ShortNode ******************************************************************************/ public class DoubleNode { // Invariant of the DoubleNode class: // 1. The node's double data is in the instance variable data. // 2. For the final node of a list, the link part is null. // Otherwise, the link part is a reference to the // next node of the list. private double data; private DoubleNode link;   

/** * Initialize a node with a specified initial data and link to the next * node. Note that the initialLink may be the null reference, * which indicates that the new node has nothing after it. * @param initialData * the initial data of this new node * @param initialLink * a reference to the node after this new node--this reference may be null * to indicate that there is no node after this new node. * @postcondition * This node contains the specified data and link to the next node. **/    public DoubleNode(double initialData, DoubleNode initialLink) { data = initialData; link = initialLink; }

/** * Modification method to add a new node after this node.    * @param item * the data to place in the new node * @postcondition * A new node has been created and placed after this node. * The data for the new node is item. Any other nodes * that used to be after this node are now after the new node. * @exception OutOfMemoryError * Indicates that there is insufficient memory for a new * DoubleNode. **/ public void addNodeAfter(double item)    { link = new DoubleNode(item, link); } /** * Accessor method to get the data from this node.    * @param - none * @return * the data from this node **/ public double getData( )    { return data; } /** * Accessor method to get a reference to the next node after this node. * @param - none * @return * a reference to the node after this node (or the null reference if there * is nothing after this node) **/ public DoubleNode getLink( ) { return link;    }       /** * Copy a list. * @param source * the head of a linked list that will be copied (which may be * an empty list in where source is null) * @return * The method has made a copy of the linked list starting at * source. The return value is the head reference for the * copy. * @exception OutOfMemoryError * Indicates that there is insufficient memory for the new list.    **/ public static DoubleNode listCopy(DoubleNode source) { DoubleNode copyHead; DoubleNode copyTail;    // Handle the special case of the empty list. if (source == null) return null; // Make the first node for the newly created list. copyHead = new DoubleNode(source.data, null); copyTail = copyHead;    // Make the rest of the nodes for the newly created list. while (source.link != null) { source = source.link; copyTail.addNodeAfter(source.data); copyTail = copyTail.link; } // Return the head reference for the new list. return copyHead; } /** * Copy a list, returning both a head and tail reference for the copy. * @param source * the head of a linked list that will be copied (which may be * an empty list in where source is null) * @return * The method has made a copy of the linked list starting at * source. The return value is an * array where the [0] element is a head reference for the copy and the [1] * element is a tail reference for the copy. * @exception OutOfMemoryError * Indicates that there is insufficient memory for the new list.    **/ public static DoubleNode[ ] listCopyWithTail(DoubleNode source) { DoubleNode copyHead; DoubleNode copyTail; DoubleNode[ ] answer = new DoubleNode[2]; // Handle the special case of the empty list.    if (source == null) return answer; // The answer has two null references .    // Make the first node for the newly created list. copyHead = new DoubleNode(source.data, null); copyTail = copyHead;    // Make the rest of the nodes for the newly created list. while (source.link != null) { source = source.link; copyTail.addNodeAfter(source.data); copyTail = copyTail.link; }    // Return the head and tail references. answer[0] = copyHead; answer[1] = copyTail; return answer; } /** * Compute the number of nodes in a linked list. * @param head * the head reference for a linked list (which may be an empty list * with a null head) * @return * the number of nodes in the list with the given head * @note * A wrong answer occurs for lists longer than Int.MAX_VALUE.    **/    public static int listLength(DoubleNode head) { DoubleNode cursor; int answer;    answer = 0; for (cursor = head; cursor != null; cursor = cursor.link) answer++;    return answer; }
/** * Copy part of a list, providing a head and tail reference for the new copy. * @param start/end * references to two nodes of a linked list * @param copyHead/copyTail * the method sets these to refer to the head and tail node of the new * list that is created * @precondition * start and end are non-null references to nodes * on the same linked list, * with the start node at or before the end node. * @return * The method has made a copy of the part of a linked list, from the * specified start node to the specified end node. The return value is an * array where the [0] component is a head reference for the copy and the * [1] component is a tail reference for the copy. * @exception IllegalArgumentException * Indicates that start and end are not references * to nodes on the same list. * @exception NullPointerException * Indicates that start is null. * @exception OutOfMemoryError * Indicates that there is insufficient memory for the new list. **/    public static DoubleNode[ ] listPart(DoubleNode start, DoubleNode end) { DoubleNode copyHead; DoubleNode copyTail; DoubleNode cursor; DoubleNode[ ] answer = new DoubleNode[2];    // Make the first node for the newly created list. Notice that this will // cause a NullPointerException if start is null. copyHead = new DoubleNode(start.data, null); copyTail = copyHead; cursor = start;    // Make the rest of the nodes for the newly created list. while (cursor != end) { cursor = cursor.link; if (cursor == null) throw new IllegalArgumentException ("end node was not found on the list"); copyTail.addNodeAfter(cursor.data); copyTail = copyTail.link; }    // Return the head and tail references answer[0] = copyHead; answer[1] = copyTail; return answer; } /** * Find a node at a specified position in a linked list. * @param head * the head reference for a linked list (which may be an empty list in * which case the head is null) * @param position * a node number * @precondition * position > 0. * @return * The return value is a reference to the node at the specified position in * the list. (The head node is position 1, the next node is position 2, and * so on.) If there is no such position (because the list is too short), * then the null reference is returned. * @exception IllegalArgumentException * Indicates that position is not positive. **/    public static DoubleNode listPosition(DoubleNode head, int position) { DoubleNode cursor; int i;    if (position <= 0) throw new IllegalArgumentException("position is not positive");    cursor = head; for (i = 1; (i < position) && (cursor != null); i++) cursor = cursor.link;
return cursor; }

/** * Search for a particular piece of data in a linked list. * @param head * the head reference for a linked list (which may be an empty list in * which case the head is null) * @param target * a piece of data to search for * @return * The return value is a reference to the first node that contains the * specified target. If there is no such node, the null reference is * returned.    **/    public static DoubleNode listSearch(DoubleNode head, double target) { DoubleNode cursor;    for (cursor = head; cursor != null; cursor = cursor.link) if (target == cursor.data) return cursor;    return null; }
/** * Modification method to remove the node after this node.    * @param - none * @precondition * This node must not be the tail node of the list. * @postcondition * The node after this node has been removed from the linked list. * If there were further nodes after that one, they are still * present on the list. * @exception NullPointerException * Indicates that this was the tail node of the list, so there is nothing * after it to remove. **/ public void removeNodeAfter( )    { link = link.link; } /** * Modification method to set the data in this node.    * @param newData * the new data to place in this node * @postcondition * The data of this node has been set to newData. **/ public void setData(double newData)    { data = newData; }    /** * Modification method to set the link to the next node after this node. * @param newLink * a reference to the node that should appear after this node in the linked * list (or the null reference if there is no node after this node) * @postcondition * The link to the node after this node has been set to newLink. * Any other node (that used to be in this link) is no longer connected to * this node. **/ public void setLink(DoubleNode newLink) { link = newLink; } }

In this assignment you wi ll need to use DoubleSedesaxa and 0: Read Chapter 4 from the textbook. 1: Implement the Sequence class from Section 4.5. Outline of Java code for this class is available in DoubleLinkedSegjava. Your sequence class must have five private instance variables as described in Section 4.5. You need to use DoubleNodejava for your node class. Follow instructions from Section 4.5, except that you do not need to put your class in a package. You need to write the invariant for your sequence ADT (invariant is explained on p.126) You need to add implementations instead of blank implementations for the following: constructor DoubleLinkedSeqO add After add Before addAI advance clone concatenation getCurrent isCurrent remove Current start In addition to that you need to implement the following methods: addAtFront a method to add a new clement at the front of the sequence removeAtFront a method to remove the element at the front of the sequence tolth a method that makes the ith element become the current element (assume that the front element is the 1 toString a method that represents the sequence as a string (in any reasonable fomat which shows the order of elements) reverse a method that returns a new sequence that contains the same elements as the original sequence but in reverse order. The original sequence should remain unchanged. For instance, if original sequence contains elements l 2 -3.5 4.3 644 in this order, then the new sequence should contain elements 644 4.3 3.5 1.2. The header of the method should be the following: public DoubleLinkedSeq reverse() everyOther a method that returns a new sequence that contains every other element of the original sequence. The original sequence should remain unchanged. For instance, if original sequence contains elements 1.2-3.5 4.3-644, then the new sequence should contain elements 1.2 4.3. If the original sequence contains elements 3.2 -2.5 1.0 7.1 -9.5, then

Explanation / Answer

In the following code you can get every task which are asked in your assignment.

public class DoubleLinkedSeq implements Cloneable

{

private Node head, tail, cursor;

private int manyNodes;

public DoubleLinkedSeq()

{   

head = null;

tail = null;

cursor = null;

manyNodes = 0;

}

public void addAfter(double element)

{

Node aNode = new Node();

aNode.setLink(cursor.getLink());

aNode.setData(element);   

cursor.setLink(aNode);   

cursor = aNode;

if(cursor.getLink() == null)

{

tail = cursor;

} //end if

manyNodes++;

}//end addAfter method

public void addBefore(double element)

{

if(cursor == head)

{

addFront(element);

}//end first if

else

{

Node aNode = new Node();

aNode.setData(cursor.getData());

aNode.setLink(cursor.getLink());

cursor.setLink(aNode);

cursor.setData(element);

if(tail == cursor)

{

tail = aNode;

}//end second if

manyNodes++;

}//end first else

}//end addBefore method   

public void addAll(DoubleLinkedSeq addend)

{

if(addend == null)

{   

throw new NullPointerException("The addend is null.");   

}//end if statement

else

{

tail.setLink(addend.head);

tail = addend.tail;

manyNodes += addend.manyNodes;

}//end else statement

}//end addAll method

public void advance()

{   

if(isCurrent() == true)

{   

cursor = cursor.getLink();   

}//end if statement

else

{   

}//end else statement

}//end advance method

public Object clone()

{

DoubleLinkedSeq clone;

try

{

clone = (DoubleLinkedSeq)super.clone();

}//end try

catch(CloneNotSupportedException e)

{

throw new RuntimeException("This class does not support Cloneable");

}//end catch   

clone.head = listCopy(head);

return clone;

}//end clone method  

public static Node listCopy(Node source)

{  

Node copyHead = null;

Node copyTail = null;

Node copyCursor = null;

Node aNode;   

//check to see if the source is an empty list

if(source == null)

{   

return null;   

}//end if statement   

copyHead = new Node(source.getData(), null);

copyTail = copyHead;

copyCursor = copyHead;

for(source = source.getLink(); source != null; source = source.getLink())

{   

aNode = new Node(source.getData(), null);

copyTail.setLink(aNode);

copyTail = aNode;

copyCursor = copyTail;   

}//end for loop   

return copyHead;

}//end listCopy method

public static Node[] listCopy(Node source, Node cursor, Node head, Node tail)

{

Node copyHead = null;

Node copyTail = null;

Node copyCursor = null;

Node[] info = new Node[3];   

//if the link list is empty

if(source == null)

{   

info[0] = null;

info[1] = null;

info[2] = null;

return info;

}//end if   

copyHead = new Node(source.getData(), null);

copyTail = copyHead;   

if(cursor == head)

{   

copyCursor = head;   

}for(source = source.getLink(); source != null; source = source.getLink())

{   

Node aNode = new Node(source.getData(), null);

copyTail.setLink(aNode);

copyTail = aNode;   

if(source == cursor)

{

copyCursor = aNode;   

}//end if statement

info[0] = copyHead;

info[1] = copyTail;

info[2] = copyCursor;

}//end for loop   

return info;

}//end listCopy method

public static DoubleLinkedSeq catenation(DoubleLinkedSeq s1, DoubleLinkedSeq s2)

{   

DoubleLinkedSeq seq = new DoubleLinkedSeq();   

if((s1 == null)||(s2 == null))

{   

throw new NullPointerException("Can not have an empty list.");   

}//end if statement

else

{   

s1.tail.setLink(s2.head.getLink());

seq.head = s1.head;

seq.tail = s2.tail;

seq.cursor = s1.cursor;   

}//end else statement

return seq;

}//end catenation method

public double getCurrent()

{   

double currentElement = 0.0;   

if(isCurrent() == true)

{

currentElement = cursor.getData();

}//end if

else

{

throw new IllegalStateException ("There is no current element.");

}//end else

return currentElement;

}//end getCurrent method

public boolean isCurrent()

{   

boolean answer;

if(cursor != null)

{

answer = true;

}//end if statement

else

{   

answer = false;

}//end else statement

return answer;

}//end isCurrent method   

public void removeCurrent()

{

if(cursor == null)

{

throw new IllegalStateException("No current Node to be removed.");   

}//end if statement

else

{   

if(cursor == tail)

{   

Node preCursor;   

for(preCursor = head; preCursor.getLink() != tail;

preCursor = preCursor.getLink());   

preCursor.setLink(null);

tail = preCursor;

manyNodes--;   

}//end if statement

else

{   

Node next = cursor.getLink();

cursor.setLink(next.getLink());

cursor.setData(next.getData());

cursor = next;

manyNodes--;

}//end else statement

}//end else statement

}//end removeCurrent method   

public int size()

{   

return manyNodes;

}//end size method

public void start()

{   

cursor = head;

}//end start method

public void addFront(double element)

{

if(head == null)

{   

Node aNode = new Node();

aNode.setData(element);

head = aNode;

tail = aNode;

cursor = aNode;

}//end if statement

else

{   

Node aNode = new Node();

aNode.setData(element);

aNode.setLink(head.getLink());

head = aNode;

cursor = aNode;   

}//end else statement   

manyNodes++;  

}//end addFront method

public void currentLast()

{

if(head == null)

{   

throw new NullPointerException("The list is empty.");   

}//end if statement

else

{   

cursor = tail;

}//end else statement

}//end currentLast method

public void addEnd(double element)

{

cursor = tail;   

Node aNode = new Node(element, null);

cursor.setLink(aNode);   

tail = aNode;

cursor = aNode;   

manyNodes++;

}//end addEnd method

public void setCurrent(int i)

{   

if(head != null)

{   

start();

for(int j = 1; j < i; j++)

{

advance();

}//end for loop

}//end if statement

else

{

throw new NullPointerException("The list is empty.");

}//end else statement

}//end setCurrent method  

public void replaceNodeData(double element)

{  

cursor.setData(element);

}//end replaceNodeData method

}//end DoubleLinkedSeq class