java - DoubleNode ADT - 打印节点的节点

标签 java nodes sequence adt doubly-linked-list

首先:这是一项家庭作业!我真的不希望有人为我编写此代码,但我需要帮助找到我搞砸的地方。

第二:问题:我们正在使用 ADT,目前正在创建一个 DoubleLinkedNode ADT(专门保存 double )。我观看了我的讲座(在线学生),阅读了阅读 Material ,并在这里和其他网站上进行了在线研究,试图解决这个问题。我似乎能够添加节点并在之前的分配中删除它们/更改元素/索引(节点)位置/克隆节点...但是每次我尝试实现一个方法来逐个节点并输出我的方法最终会捕获我的节点的尾部并无限地打印它..或我循环该方法的次数(即manyNodes)。

这是我当前的代码:

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
}

最佳答案

已修复 - 感谢您的帮助。每个建议都让我到达那里。

损坏的代码段是:public static void addFirst

固定代码:

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

关于java - DoubleNode ADT - 打印节点的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52637273/

相关文章:

java - eclipse调试器: enter a method without entering nested methods

java - spring boot集成测试时如何正确连接testcontainers redis?

Neo4J - 将元素存储为用户属性或节点和关系哪个更好?

javascript - 如何在for循环中等待每次迭代并返回响应作为nodeJS中的API响应

compression - 序列压缩?

java - AffdexMe,Android,LicenseException : could not open license file

java - 显示用户输入的最小值和最大值并查找平均值

c++ - 排序节点(链表)C++

python - 透明地将数字类型处理为单值序列(反之亦然)

c - 如何随机化字符序列?