当前位置: 首页 > 知识库问答 >
问题:

Java链接列表交换两个数字

西门伟
2023-03-14

我的代码中有错误。错误是:错误:类SwapNodes是公共的,应该在名为SwapNodes的文件中声明。java公共类SwapNodes{
^Main.java:104:错误:内部类SwapNodes中的静态声明非法。Main公共静态void Main(String[]args){
^modifier'static'仅允许在常量变量声明中使用。我的程序如下所示:

public class SwapNodes {  
      
    //Represent a node of the singly linked list  
    class Node{  
        int data;  
        Node next;  
          
        public Node(int data) {  
            this.data = data;  
            this.next = null;  
        }  
    }  
   
    //Represent the head and tail of the singly linked list  
    public Node head = null;  
    public Node tail = null;  
      
    //addNode() will add a new node to the list  
    public void addNode(int data) {  
        //Create a new node  
        Node newNode = new Node(data);  
          
        //Checks if the list is empty  
        if(head == null) {  
            //If list is empty, both head and tail will point to new node  
            head = newNode;  
            tail = newNode;  
        }  
        else {  
            //newNode will be added after tail such that tail's next will point to newNode  
            tail.next = newNode;  
            //newNode will become new tail of the list  
            tail = newNode;  
        }  
    }  
      
    //swap() will swap the given two nodes  
    public void swap(int n1, int n2){  
        Node prevNode1 = null, prevNode2 = null, node1 = head, node2 = head;  
          
        //Checks if list is empty  
        if(head == null) {  
            return;  
        }  
          
        //If n1 and n2 are equal, then list will remain the same  
        if(n1 == n2)  
            return;  
          
        //Search for node1  
        while(node1 != null && node1.data != n1){  
            prevNode1 = node1;  
            node1 = node1.next;  
        }  
          
        //Search for node2  
        while(node2 != null && node2.data != n2){  
            prevNode2 = node2;  
            node2 = node2.next;  
        }  
          
        if(node1 != null && node2 != null) {  
              
            //If previous node to node1 is not null then, it will point to node2  
            if(prevNode1 != null)  
                prevNode1.next = node2;          
            else  
                head  = node2;  
              
            //If previous node to node2 is not null then, it will point to node1  
            if(prevNode2 != null)  
                prevNode2.next = node1;  
            else  
                head  = node1;  
              
            //Swaps the next nodes of node1 and node2  
            Node temp = node1.next;   
            node1.next = node2.next;   
            node2.next = temp;       
        }      
        else {  
            System.out.println("Swapping is not possible");  
        }  
    }  
      
    //display() will display all the nodes present in the list  
    public void display() {  
        //Node current will point to head  
        Node current = head;  
          
        if(head == null) {  
            System.out.println("List is empty");  
            return;  
        }  
        while(current != null) {  
            //Prints each node by incrementing pointer  
            System.out.print(current.data + " ");  
            current = current.next;  
        }  
        System.out.println();  
    }  
      
    public static void main(String[] args) {  
          
        SwapNodes sList = new SwapNodes();  
          
        //Add nodes to the list  
        sList.addNode(1);  
        sList.addNode(2);  
        sList.addNode(3);  
        sList.addNode(4);  
        sList.addNode(5);  
          
        System.out.println("Original list: ");  
        sList.display();  
          
        //Swaps the node 2 with node 5  
        sList.swap(2,5);  
          
        System.out.println("List after swapping nodes: ");  
        sList.display();  
    }  
}  

共有2个答案

广献
2023-03-14

类SwapNodes是公共的,应该在名为SwapNodes的文件中声明。java这意味着由于类是公共的,所以文件名必须是SwapNodes而不是Main。将文件名更改为SwapNodes

输出将是:

Original list: 
1 2 3 4 5 
List after swapping nodes: 
1 5 3 4 2 

注意:总是尝试将文件命名为公共类。例如:公共类ClassName必须在文件ClassName.java中。

姜旭
2023-03-14

由于类SwapNodes是公共的,应该在名为SwapN的文件中声明odes.java你需要将持有类的文件重命名为SwapNodes.java,因为顶级类必须位于同名的文件中。

 类似资料:
  • 大家好,我正在尝试在两个双链接列表之间完全交换两个节点(值和地址也是)。只有位于相同位置的节点才能在两个节点之间交换,即位置2的节点只能由另一个LinkedList中位置2的节点交换。考虑下面的2个链接列表示例: 假设我们想交换第三个元素,即162和830。交换后,领英列表变成: 我已经尝试了下面的代码,但它不能替换前面的元素。 我怎样才能完成这项任务? elseif不交换前面的元素,例如,如果我

  • 问题内容: 我想使用接口交换两个数字,但是接口概念令我感到困惑。 http://play.golang.org/p/qhwyxMRj-c 这是代码和游乐场。如何使用接口并交换两个输入数字?我需要定义两个结构吗? 问题答案: 首先,类型只是接受所有值的类型,因为它是带有空方法集的接口,并且每种类型都可以满足该要求。例如没有任何方法,也没有。 对于交换两个变量的值的方法,首先需要确保这些变量实际上是可

  • 问题内容: 我正在尝试了解go的内部原理。考虑以下代码 上面的代码完美地交换了2个数字,a变成5,b变成10。我无法理解它是如何工作的。在第二行代码中考虑,如果将a首先分配给b,则b将为10。现在,如果将b分配给a,那么a也不应也为10。 请帮助我了解它是如何工作的 谢谢 问题答案: TL; DR :反汇编表明CPU必须足够聪明才能看到正在发生的事情,并使用寄存器来避免覆盖内存中的现有值。 这个问

  • 问题内容: 我试图根据某些条件并执行一些步骤来找到两个列表。在学习阶段找不到方法:) 如您所见,逻辑很简单 根据某个过滤器从订单中获取所有项目,并与另一个过滤器相交并做一些事情。 问题答案: 最简单的方法是这样的:

  • 本文向大家介绍awk 交换表格数据中的两列,包括了awk 交换表格数据中的两列的使用技巧和注意事项,需要的朋友参考一下 示例 给定一个;用作列定界符的文件。排列第一和第二列是通过完成            

  • 我在Java中实现一个双链接列表时遇到了一个问题。特别是要交换2个以下节点(在我的例子中,一个节点包含一个政治候选人)。 假设下面的DLL: head- 作为输出,我没有从头到尾的正确DLL,但从头到尾都很好: 我已经写了几个版本的这个方法reverseTwoNode。我甚至尝试在节点内部交换数据,而不是交换节点,我也有同样的问题。你能帮我真是太好了,我花了这么多时间在这个简单的功能上,我看不出有