Question: Need help writing an addAfter() method for a linked sequence in java. Below is problem description and code I have already written...confused as the what

Need help writing an addAfter() method for a linked sequence in java. Below is problem description and code I have already written...confused as the what type of parameter it will take and how to update the cursor reference

Here is the full problem specification for clarity, however I am only asking for help w/ the addAfter() method. My code can be found at the bottom. Also, if you have any tips or hints concerning the code I have written already that is appreciated.

LinkedSequence (Generic Class)

Fields

The field manyNodes is optional.

It can be convenient to have direct access to the size of the sequence, but this variable needs careful update in each method that manipulates the size, and this can be a source of errors. For instance, the removeNodeAfter() method changes the size most of the time, but no change when tail is the reference. The listLength() method of the Node class makes the field dispensable, but the length algorithm is O(n).

There are five private variables of type Node:

head

tail

cursor

precursor

dummy

The LinkedSequence invariant

For your convenience the invariant as part of the class documentation is supplied. When you design and implement the methods, it is important that you pay attention to the invariant.

manyNodes is the number of nodes in the sequence; in particular the value is 0 for an empty sequence

For a non-empty sequence head is the reference to the first, and tail is a reference to the last node of the sequence; for an empty sequence head and tail are null

For a non-empty sequence cursor can be a reference to any of the nodes of the sequence, it cannot reference dummy. The cursor can be null; for an empty sequence cursor is null

If cursor is not null, precursor is a reference to a node such that cursor is precursor.link. If cursor is null, precursor is null; in particular precursor does not reference tail

dummy is a node reference and it is never null (dummy is not an element of the sequence); dummy.link is always head (null or not). If cursor is the head, then precursor is dummy

Methods and Constructors

All the fields except dummy require accessor and mutator methods.

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

Constructor

Takes one node parameter to initialize the head. To initialize tail, head is assigned. Assigns dummys 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

--------------------------------------------------------------------------My Code so far...-------------------------------------------------------------------------------

public class LinkedSeq { private Node head; private Node tail; private Node cursor; private Node precursor; private Node dummy = new Node(null,null); //Constructor which takes one node parameter to initialize head public LinkedSeq(Node head){ this.head = head; tail = head; dummy.setLink(head); } public Node getHead(){ return head; } public void setHead(Node head){ this.head = head; } public Node getTail(){ return tail; } public void setTail(Node tail){ this.head = tail; } public Node getCursor(){ return cursor; } public void setCursor(Node cursor){ this.cursor = cursor; } public Node getPrecursor(){ return precursor; } public void setPrecursor(Node precursor){ this.precursor = precursor; } public Node addAfter(Node data){ Node d = data; **********************************************need w every part of this method }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!