The specification of a nongeneric DoubleLinkedSeq class All

The specification of a non-generic DoubleLinkedSeq class

All the fields except dummy require accessor and mutator methods.

You should instantiate dummy at the declaration with two null parameters.

IN JAVA

            Constructor

Takes one node parameter to initialize the head. To initialize tail, head is assigned. Assigns dummy’s link the head (setLink() must be used). Note that dummy does not store any info data but null, and it is not part of the data structure. Its link is always the head, see that ADT invariant.

                        Instance Methods

addAfter( ) Takes a parameter for the new data value to be added to the structure and returns the currently added node. Cursor reference is always updated to the added node. A call to the addNodeAfter method of the Node class shall add the new node to the list

          after dummy if head is null

          after tail if cursor is null but head is not

after cursor if cursor is not null

Build the selection logic carefully and update the relevant fields as necessary

addBefore( ) Takes a parameter for the new data value to be stored in the structure and returns the currently added node. Cursor reference is always updated to the added node. A call to the addNodeAfter method of the Node class shall add the new node to the list

(i) after dummy if precursor is null

(ii)after precursor if precursor is not null

addAll( )Takes another linked sequence for parameter (‘other’) and joins the parameter list to this list after this tail. If the parameter is null or empty, the method returns. Otherwise, if the calling list is empty (head is null) this head assigned other head and this tail assigned other tail; else tail link assigned other head and tail assigned other tail.

advance() advances the cursor forward one step, if the cursor is not null and not the tail; precursor is updated accordingly. Null cursor is advanced to head, tail cursor advanced to null.

start() if the sequence is empty, the method returns; otherwise assigns cursor the head and precursor thedummy

clone( ) Returns a copy of the calling sequence. See the specifications in the book.

concatenate( ) static method; takes two linked sequence parameters and creates a third sequence by adding the second after the first. You have to use the listCopy( ) and getTail( ) static methods from Node. Do not use listCopyWithTail( ).

removeCurrent() removes the cursor if cursor is not null and returns the removed node. The new cursor is the following node if there is one, and precursor updated accordingly. If tail was removed, the new new cursor is head if there is head, if so precursor is dummy. If cursor is null, the method returns null.

displayList( ) convenience method. Prints all the nodes to the console; use the toString( ) call with respect to head for the recursive version and run a loop for the non-recursive version.

Solution

The question is incomplete.

However, I have provided the classes required for this assignment.
Please write a main method and test the same. Since you have not provided the output format and what
to output, I could not provide the output. However the code works fine.

Create two classes - Node.java and DoubleLinkedSeq.java in the same location.
----------------------------------------------------------------------------------
Node.java

class Node
{
private double data;
private Node link;

//constructors
public Node()
{
data = 0.0;
link = null;
}

public Node(double d, Node l)
{
data = d;
link = l;      
}
void setData(double newElement)
{
data = newElement;  
}
void setLink(Node newLink)
{
link = newLink;  
}
double getData()
{
return data;  
}
Node getLink()
{
return link;  
}
}
---------------------------------------------------------------------------
DoubleLinkedSeq.java
public class DoubleLinkedSeq implements Cloneable
{

//To allow us to traverse the list and call for specific information from each node without disturbing the list
   private Node head, tail, cursor;

//variable to keep track of the number of nodes in the link list
   private int manyNodes;    
  
// constructor.
public DoubleLinkedSeq()
{
       head = null;
       tail = null;
       cursor = null;
      
       manyNodes = 0;      
}

// addAfter will add the element after the current element.
public void addAfter(double element)
{   
   Node aNode = new Node();
aNode.setLink(cursor.getLink());
aNode.setData(element);
  
cursor.setLink(aNode);
  
cursor = aNode;
      
       //check to see if new node is at the end of the list, if so then have tail
       //be equal to cursor
       if(cursor.getLink() == null)
       {
      
               tail = cursor;
      
       }//end if
      
       manyNodes++;
  
}//end addAfter method

// addBefore will add the element before the current element.
public void addBefore(double element)
{      
   //check to see if adding with an empty list
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


// Place the contents of another sequence at the end of this sequence.
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

// Move forward, so that the current element is now the next element in this sequence.
public void advance()
{
  
if(isCurrent() == true)
{
  
       cursor = cursor.getLink();

}//end if statement
else
{
//throw new IllegalStateException(\"There is no current element\");   
}//end else statement
   
}//end advance method

// Generate a copy of this sequence.
public Object clone()
{
       DoubleLinkedSeq clone;
      
       try
       {      
           clone = (DoubleLinkedSeq)super.clone();
       }
       catch(CloneNotSupportedException e)
       {      
           throw new RuntimeException(\"This class does not support Cloneable\");
       }
  
clone.head = listCopy(head);
return clone;
}

//Returns the head of the newly \"cloned\" linked list.
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
  
//first and only element in the list atm
copyHead = new Node(source.getData(), null);
copyTail = copyHead;
copyCursor = copyHead;
  
//for loop will add in the rest of the elements to the list. copyTail
//will act like cursor and set the link of the current node to the
//following node before moving to the new node
for(source = source.getLink(); source != null; source = source.getLink())
{
aNode = new Node(source.getData(), null);
copyTail.setLink(aNode);
copyTail = aNode;
copyCursor = copyTail;
}
  
return copyHead;

}

//Creates a copy of each node in the list. This method is needed to
//work with the clone method because the clone method does not create independent
//nodes from the original.
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(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;

}   
info[0] = copyHead;
info[1] = copyTail;
info[2] = copyCursor;

}
  
return info;

}//end listCopy method

// Create a new sequence that contains all the elements from one sequence followed by another.
public static DoubleLinkedSeq concatantn(DoubleLinkedSeq s1, DoubleLinkedSeq s2)
{
DoubleLinkedSeq seq = new DoubleLinkedSeq();
if((s1 == null)||(s2 == null))
{
throw new NullPointerException(\"Can not have an empty list.\");
}
else
{
s1.tail.setLink(s2.head.getLink());

seq.head = s1.head;
seq.tail = s2.tail;
seq.cursor = s1.cursor;
}
      
return seq;
   }

// Accessor method to get the current element of this sequence.
public double getCurrent()
{   
       double currentElement = 0.0;
  
       if(isCurrent() == true)
       {      
           currentElement = cursor.getData();
      
       }
       else
       {      
           throw new IllegalStateException (\"There is no current element.\");      
       }      
return currentElement;      
}

// Method to determine whether this sequence has a specified
public boolean isCurrent()
{   
       boolean answer;
      
       if(cursor != null)
       {      
           answer = true;      
       }
else
{
answer = false;
}      
return answer;
   }
  
// Remove the current element from this sequence.
public void removeCurrent()
{
if(cursor == null)
{
throw new IllegalStateException(\"No current Node to be removed.\");
}
else
{
        if(cursor == tail)
{
Node preCursor;
for(preCursor = head; preCursor.getLink() != tail;
preCursor = preCursor.getLink());
  
preCursor.setLink(null);
tail = preCursor;
manyNodes--;   
}
else
{   
Node next = cursor.getLink();
  
cursor.setLink(next.getLink());
cursor.setData(next.getData());
  
cursor = next;
  
manyNodes--;

}   
}      
}
  
// Determine the number of elements in this sequence.
public int size()
{
       return manyNodes;      
}

// Set the current element at the front of this sequence.
public void start()
{
       cursor = head;
}

// add a new node after the head.
public void addFront(double element)
{   
if(head == null)
{
Node aNode = new Node();
aNode.setData(element);
       
head = aNode;      
        tail = aNode;
        cursor = aNode;

}
else
{
Node aNode = new Node();
aNode.setData(element);
aNode.setLink(head.getLink());

head = aNode;
cursor = aNode;
  
}
manyNodes++;

}

//This method sets cursor to tail, the last element in the list. If the
//list is empty, it will throw a NullPointerException.
public void currentLast()
{   
if(head == null)
{
throw new NullPointerException(\"The list is empty.\");
  
}
else
{
cursor = tail;
}   
}

//This method adds a new Node to the end of the list. After the add, the
//cursor points to the new Node.
public void addEnd(double element)
{   
cursor = tail;
  
Node aNode = new Node(element, null);
cursor.setLink(aNode);
  
tail = aNode;
cursor = aNode;
  
manyNodes++;

}

//This method places the incoming data into the current Node
public void setCurrent(int i)
{   
if(head != null)
{
start();

for(int j = 1; j < i; j++)
{   
advance();   
}   
}
else
{
throw new NullPointerException(\"The list is empty.\");
}
}

public void replaceNodeData(double element)
{   
cursor.setData(element);
}  
}
--------------------------------------------------------------------------

The specification of a non-generic DoubleLinkedSeq class All the fields except dummy require accessor and mutator methods. You should instantiate dummy at the d
The specification of a non-generic DoubleLinkedSeq class All the fields except dummy require accessor and mutator methods. You should instantiate dummy at the d
The specification of a non-generic DoubleLinkedSeq class All the fields except dummy require accessor and mutator methods. You should instantiate dummy at the d
The specification of a non-generic DoubleLinkedSeq class All the fields except dummy require accessor and mutator methods. You should instantiate dummy at the d
The specification of a non-generic DoubleLinkedSeq class All the fields except dummy require accessor and mutator methods. You should instantiate dummy at the d
The specification of a non-generic DoubleLinkedSeq class All the fields except dummy require accessor and mutator methods. You should instantiate dummy at the d
The specification of a non-generic DoubleLinkedSeq class All the fields except dummy require accessor and mutator methods. You should instantiate dummy at the d
The specification of a non-generic DoubleLinkedSeq class All the fields except dummy require accessor and mutator methods. You should instantiate dummy at the d
The specification of a non-generic DoubleLinkedSeq class All the fields except dummy require accessor and mutator methods. You should instantiate dummy at the d

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site