【问题标题】:Implementing a Circular Single Linked List using a scanner使用扫描仪实现循环单链表
【发布时间】:2021-09-15 01:56:42
【问题描述】:

这是一个给定的活动,我们应该在其中创建一个插入节点的方法,并使用扫描仪进行输入。到目前为止,我可以从列表中输入 3 个对象,但是当我尝试添加另一个对象时出现了问题:

它是这样的:1,2,3,当我尝试添加另一个时,它会转到这个 1,2,4 但我想要的是这个 1,2,3,4。

非常感谢您提前提供的帮助。

这里是主要方法:

import java.util.Scanner;

public class MySinglyLinkedCircularListMain {
    public static void main(String[] args) throws ListOverflowException {
        Scanner keyboard = new Scanner(System.in);

        MySinglyLinkedCircularList<Node> singlyLinkedCircularList = new MySinglyLinkedCircularList<>();

        while (true) {
            System.out.println("+---------------------------------------------------+");
            System.out.println("| Select the Number to be executed:                 |\n" +
                    "| 1) Insert an element                              |\n" +
                    "| 2) Delete an element from the list                |\n" +
                    "| 3) Get an element from the list                   |\n" +
                    "| 4) Search an element in the list                  |\n" +
                    "| 5) Number of elements in the list                 |\n" +
                    "| 6) Show the elements in the list                  |");
            System.out.print("+---------------------------------------------------+ \n");
            System.out.print("Input your choice: ");
            int intInput = keyboard.nextInt();

            if (intInput == 1) {
                singlyLinkedCircularList.insert(new Node(singlyLinkedCircularList));

            } else if (intInput == 2) {
                singlyLinkedCircularList.delete(new Node(singlyLinkedCircularList));
             

            } else if (intInput == 3) {
                singlyLinkedCircularList.getElement(new Node(singlyLinkedCircularList));

            } else if (intInput == 4) {
                singlyLinkedCircularList.search(new Node(singlyLinkedCircularList));

            } else if (intInput == 5){
                System.out.println("The current capacity of the single circular linked list is "
                        + singlyLinkedCircularList.getSize());

            } else if (intInput == 6) {
                singlyLinkedCircularList.showAllElements();
                System.out.println();
            }
        }
    }
}

这里是节点类

public class Node<T> {
    T data;
    Node<T> next;

    public Node(T data) {
        this.data = data;
        next = null;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setNext(Node<T> node) {
        next = node;
    }

    public Node<T> getNext() {
        return next;
    }
}

这是插入方法的类

import java.util.NoSuchElementException;
import java.util.Scanner;

public class MySinglyLinkedCircularList<E> implements MyList<E> {
    Scanner keyboard = new Scanner(System.in);
    int size;
    Node<E> startNode;
    Node<E> endNode;

    public MySinglyLinkedCircularList() {
        size = 0;
        startNode = endNode = null;
    }

    public int getSize() {
        return size;
    }

    public void insert(E data) throws ListOverflowException {
        System.out.print("Input the element you want: ");
        data = (E) keyboard.next();

        Node<E> newNode = new Node(data);


        if (startNode == null) {
            startNode = endNode = newNode;
            startNode.next = startNode;
            size++;
            System.out.println("Element " + startNode.getData() + " has been stored in position "
                    + getSize() + " and is now referenced itself");

        } else {
            Node<E> addNode = endNode;

            for (int i = 0; i < getSize(); i++) {
                addNode = addNode.getNext();
            }
            addNode.next = newNode;
            newNode.next = startNode;
            size++;
}
}

这里是显示元素方法

public void showAllElements() {
        Node<E> showNode = startNode;
        int i = 0;

        System.out.print("Here are the current elements: ");
        while (i<getSize()) {
            System.out.print(showNode.getData() + " ");
            showNode = showNode.getNext();
            i++;
        }
        System.out.print(showNode.getData());
    }

界面如下

public interface MyList<E> {
    public int getSize();
    public void insert(E data) throws ListOverflowException;
    public E getElement(E data) throws NoSuchElementException; 
    public boolean delete(E data); // returns false if the data is not deleted in the list
    public boolean search(E data);
    public void showAllElements();
}

【问题讨论】:

    标签: java java.util.scanner circular-list


    【解决方案1】:

    这是有问题的:

    public void insert(E data) throws ListOverflowException {
        System.out.print("Input the element you want: ");
        data = (E) keyboard.next();  // ***** here *****
    

    您的方法接受 E 数据参数 insert(E data),但会立即丢弃该方法第二行中的参数 data = (E) keyboard.next(); 中包含的任何结果,将其替换为甚至不应该存在的 Scanner 输入。 MySinglyLinkedCircularList 类应该有一个 Scanner 对象,也不应该接受用户输入,而是应该处理循环列表的逻辑,仅此而已。它甚至不应该有 println 语句,除了您可能在创建期间仅用于调试目的的临时 println 语句。将所有 UI 代码(用户界面代码 -- Scanner 输入和 println 语句)放在 UI 类中。

    请注意,我不知道这是否是您的全部错误的原因,并且要检查这将需要更密集的调试,我敦促您自己做更多的调试,因为这都是您的责任首先调试,这是一项必要且有用的技能,只有随着使用才能变得更好。如果您不确定如何执行此操作,请查看How to debug small programs。它不会解决您的直接问题,但会为您提供可以遵循的步骤,这些步骤应该可以帮助您自己解决问题,或者即使这不成功,也至少可以帮助您更好地隔离您的问题,以便您的问题可以更专注,更容易回答。

    【讨论】:

    • 这似乎比这更奇怪。插入调用看起来像(删除过多的变量名)list.insert(new Node(list))。为什么 Node ctor 需要知道它将很快插入的列表尚不清楚。也许它被忽略是最好的。
    • @user16632363:是的,你是对的。我不得不承认没有详细地倾注代码(代码太多,我的时间太少了),但是如果您想发布自己的答案,或者添加到这个社区 wiki 答案,请这样做。跨度>
    • 这部分味道不对:Node&lt;E&gt; addNode = endNode,然后找到位置for (int i = 0; i &lt; getSize(); i++)。如果我们知道结束节点,我们可能会在结束之后附加下一个条目。迭代是为了什么?而且因为我们从最后开始迭代,如果感觉那里可能存在一个错误。但我没有费心去解决它,所以这不是一个答案。 (代码太多/时间太少也是如此)。
    • @user16632363:我的“答案”也不是一个完整的答案,这就是我抄袭并发布社区维基的原因。 OP 确实确实需要先进行更多调试,而且似乎来得太早了。
    • 'get input'不属于实现链表的代码。您希望(作为一般情况)能够使用链接列表而不必与人交谈。在用户说他想插入一些东西之后,询问要在列表中放入什么的正确位置就在main。 “你要插入什么?”属于那里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2012-09-27
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多