1

First: This is a Homework assignment! I don't really want someone to code this for me but I need help locating where I messed this up.

Second: The Issue: We are working with ADTs and creating currently a DoubleLinkedNode ADT (holds specifically doubles). I have watched my lectures (online student), read the reading material, and done online research both here and on other sites attempting to fix this. I seem to be able to add nodes and in previous assignments remove them / change elements / index (node) location / clone the node... but each time I try to implement a method to go node for node and output my method ends up grabbing the tail of my Nodes and printing it either infinitely .. or the number of times I loop the method (IE manyNodes).

Here is my current code:

public class DoubleNode {
    private double data;
    private DoubleNode link;

    public DoubleNode(double initialData, DoubleNode initialLink)
    {
        data = initialData;
        link = initialLink;
    }

    public void addNodeAfter(double item)
    {
        link = new DoubleNode(item, link);
    }

    public double getData( )
    {
        return data;
    }

    public DoubleNode getLink( )
    {
        return link;
    }

    public static DoubleNode listCopy(DoubleNode source)
    {
        DoubleNode copyHead;
        DoubleNode copyTail;

        if (source == null)
            return null;

        copyHead = new DoubleNode(source.data, null);
        copyTail = copyHead;

        while (source.link != null)
        {
            source = source.link;
            copyTail.addNodeAfter(source.data);
            copyTail = copyTail.link;
        }

        return copyHead;
    }

    public static DoubleNode[ ] listCopyWithTail(DoubleNode source)
    {
        DoubleNode copyHead;
        DoubleNode copyTail;
        DoubleNode[ ] answer = new DoubleNode[2];

        if (source == null)
            return answer;

        copyHead = new DoubleNode(source.data, null);
        copyTail = copyHead;

        while (source.link != null)
        {
            source = source.link;
            copyTail.addNodeAfter(source.data);
            copyTail = copyTail.link;
        }

        answer[0] = copyHead;
        answer[1] = copyTail;
        return answer;
    }

    public static int listLength(DoubleNode head)
    {
        DoubleNode cursor;
        int answer;

        answer = 0;
        for (cursor = head; cursor != null; cursor = cursor.link)
            answer++;

        return answer;
    }

    public static DoubleNode[ ] listPart(DoubleNode start, DoubleNode end)
    {
        DoubleNode copyHead;
        DoubleNode copyTail;
        DoubleNode cursor;
        DoubleNode[ ] answer = new DoubleNode[2];

        copyHead = new DoubleNode(start.data, null);
        copyTail = copyHead;
        cursor = start;

        while (cursor != end)
        {
            cursor = cursor.link;
            if (cursor == null)
                throw new IllegalArgumentException
                        ("\n\n***LAST NODE NOT ON LIST***\n\n");
            copyTail.addNodeAfter(cursor.data);
            copyTail = copyTail.link;
        }

        // Return the head and tail references
        answer[0] = copyHead;
        answer[1] = copyTail;
        return answer;
    }

    public static DoubleNode listPosition(DoubleNode head, int position)
    {
        DoubleNode cursor;
        int i;

        if (position <= 0)
            throw new IllegalArgumentException("\n\n***POSITION DOES NOT EXIST***\n\n");

        cursor = head;
        for (i = 1; (i < position) && (cursor != null); i++)
            cursor = cursor.link;

        //love you

        return cursor;
    }

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

    public void removeNodeAfter( )
    {
        link = link.link;
    }

    public void setData(double newData)
    {
        data = newData;
    }

    public void setLink(DoubleNode newLink)
    {
        link = newLink;
    }

    public String toString() {
        String str;
        str = "\n" + data;
        return str;
    }
}

public class DoubleLinkedSeq implements Cloneable {
    private static DoubleNode head, tail, currentElement, cursor, precursor, cursorTemp;
    private static int manyNodes;

    public DoubleLinkedSeq(double element) {
        head = null;
        tail = null;
        currentElement = null;
        manyNodes = 0;
    }

    public DoubleLinkedSeq() {
        cursor = tail;
        precursor = head;
    }

    public static void addFirst(double element) {
        if (head == null) {
            DoubleNode node = new DoubleNode(element, null);
            head = node;
            tail = node;
            cursor = head;
            precursor = head;
        }
        else {
            DoubleNode node = new DoubleNode(element, head);
            precursor = head;
            cursor = precursor.getLink();
        }
         manyNodes++;
    }

    public static void addAfter(double element) {
        if (isCurrent()) {
            cursor.addNodeAfter(element);
            precursor = cursor;
            cursor = cursor.getLink();
        }
        else {
            if(tail == null) {
                tail = new DoubleNode(element, null);
                precursor = tail;
                cursor = tail;
                tail.addNodeAfter(element);
                cursor = tail.getLink();
            }
            else {
                precursor = tail;
                tail = tail.getLink();
                cursor = tail;
                cursor.addNodeAfter(element);
            }
        }
        manyNodes++;
    }

    public static void addBefore(double element) {
        if (isCurrent()) {
            if (cursor == head) {
                precursor = new DoubleNode(element, cursor);
                head = precursor;
                cursor = precursor.getLink();
            }
            else {
                precursor = new DoubleNode(element, cursor);
                cursor = precursor.getLink();
            }
        }
        else {
            if (head == null) {
                head = new DoubleNode(element, null);
                cursor = head;
                precursor = head;
                cursor = precursor.getLink();
                tail = cursor;
            }
            else {
                precursor = new DoubleNode(element, cursor);
                head = precursor;
                tail = cursor;
            }
        }
        manyNodes++;
    }

    public static void addAll(DoubleLinkedSeq addend) {
        if(addend == null) {
            throw new IllegalArgumentException("\n\n***LIST IS EMPTY***\n\n");
        }
        if(addend.size() > 0) {
            tail.setLink(addend.head);
            tail = addend.tail;
            manyNodes += addend.size();
        }
    }

    public static void advance() {
        if(!isCurrent()) {
            return;
        }
        precursor = cursor;
        cursor = cursor.getLink();
    }

    public Object clone() {
        DoubleLinkedSeq answer;
        try {
            answer = (DoubleLinkedSeq) super.clone();
        }
        catch(CloneNotSupportedException e) {
            throw new RuntimeException("\n\n***CLASS DOES NOT IMPLEMENT CLONEABLE***\n\n");
        }
        answer.head = DoubleNode.listCopy(head);
        return answer;
    }

    public static DoubleLinkedSeq catenation(DoubleLinkedSeq s1, DoubleLinkedSeq s2) {
        DoubleLinkedSeq s3 = new DoubleLinkedSeq(s1.manyNodes + s2.manyNodes);
        System.arraycopy(s1.currentElement, 0, s3.currentElement, 0, s1.manyNodes);
        System.arraycopy(s2.currentElement, 0, s3.currentElement, s1.manyNodes, s2.manyNodes);
        s3.manyNodes = (s1.manyNodes + s2.manyNodes);
        return s3;
    }

    public double getCurrent( )
    {
        if(!isCurrent()) {
            throw new IllegalStateException("\n\n***CURRENT ELEMENT IS NULL***\n\n");
        }
        return cursor.getData();
    }

    public static boolean isCurrent( )
    {
        if (cursor == null) {
            return false;
        }
        else {
            return true;
        }
    }

    public static void removeCurrent( )
    {
        if(!isCurrent()) {
            throw new IllegalStateException("\n\n***CURRENT ELEMENT IS NULL***\n\n");
        }
        else if(manyNodes == 0) {
            throw new IllegalStateException("\n\n***LIST IS EMPTY***\n\n");
        }
        else if(manyNodes == 1) {
            head = null;
            tail = null;
            cursor = null;
        }
        else if(cursor == head) {
            head = head.getLink();
            cursor = head;
        }
        else if(cursor == tail) {
            tail = null;
            cursor = null;
        }
        else {
            DoubleNode pre = head;
            while(pre.getLink() != cursor) {
                pre = pre.getLink();
            }
            if(cursor == tail) {
                pre.setLink(null);
                tail = pre;
                cursor = null;
            }
            else {
                pre.setLink(cursor.getLink());
                cursor = pre;
            }
        }
        manyNodes--;
    }

    public static int size( )
    {
        return manyNodes;
    }

    public static void start( )
    {
        if(head == null) {
            cursor = null;
        }
        cursor = head;
    }
    /**
     * Return for Output
     * @return
     */
    public String toString() {
        String str="-> ";
        cursorTemp = head;
        while (cursorTemp != null) {
            str = str + cursorTemp.toString();
            cursorTemp = cursorTemp.getLink();
        }
        return str;
    }
}//END CLASS

public class DoubleLinkedSeqTest {
    private static final SecureRandom generator = new SecureRandom();
    private static int index, menuSelect, i, size;
    private static String tmp;
    private static double dataManip;
    private static DoubleLinkedSeq list = new DoubleLinkedSeq();

    public static void main(String[] args) {
    DoubleLinkedSeq.start();
    populateSequence();

    }
    private static void populateSequence() {
        size = generator.nextInt(15);
        for (i = 0; i < size; i++) {
            dataManip = ((generator.nextDouble() * 12.3152) - (generator.nextDouble() * 7.9221));
            list.addFirst(dataManip);
        }
        for(i = 0; i < size; i++) {
            System.out.printf("%s %n", list.toString());
        }
    } //END populateSequence METHOD
}
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
  • Did you try stepping through this with your debugger to see what is going on "under the hood"? – GBlodgett Oct 04 '18 at 00:22
  • I struggle with the debugger (admittedly), but running through it just now it appears I am getting a negative element? Which is confusing me as addFirst creates a new node if head == null and then sets the head to the new node. . . – Mark Louprette Jr Oct 04 '18 at 00:38
  • Which method are you having trouble with? Try to simplify the problem by removing as much code as you can while still having the problem. (Maybe construct a Doubly linked list with only three elements and then try to call your method and see what happens to those three elements – GBlodgett Oct 04 '18 at 00:45
  • Specifically *I think* public String toString() - in DoubleLinkedSeq is what is beating me up. I am getting an output: "-> 0.8065082516264238 -> 0.8065082516264238 " that looks like the above over and over again. It is grabbing a single node and printing it repeatedly Also thank you for recommending I minimize my code, it is helping me sift through this – Mark Louprette Jr Oct 04 '18 at 00:52
  • Given that output then it would seem that the `link` field is pointing to itself. I would look at the constructor you use and which method you use to add the double to the list – GBlodgett Oct 04 '18 at 01:00

1 Answers1

2

Fixed - Thank you for the help. Each suggestion got me there.

Broken code segment was: public static void addFirst

Fixed Code:

public static void addFirst(double element) {
    if (head == null) {
        DoubleNode node = new DoubleNode(element, null);
        head = tail = node;
        precursor = cursor = head;
    } else {
        DoubleNode node = new DoubleNode(element, head);
        head = precursor = node;
        cursor = precursor.getLink();
    }
    manyNodes++;
    System.out.println("Node Count: " + manyNodes);
}
Rafael
  • 7,605
  • 13
  • 31
  • 46