代码问题是,当我运行代码时,list1中的元素会自动复制到链表节点中。

huangapple go评论84阅读模式
英文:

My code problem is that when i run the code then list1st elements is automatically copy to the ll node

问题

以下是您提供的内容的翻译部分:

问题出在我的代码上,当我运行代码时,list1的元素会自动复制到ll节点。

一个用于链表实现的类:

public class node {

    static public class Node {
        Node next;
        int data;

        Node(int d) {
            this.data = d;
            this.next = null;
        }

        static Node head = null;

        public void add(int data) {
            Node new_node = new Node(data);
            new_node.next = head;
            head = new_node;
        }

        public void printlist(Node list) {
            Node currNode = list.head;
            System.out.print("Linked List: ");
            
            while (currNode != null) {
                System.out.print(currNode.data + " ");
                currNode = currNode.next;
            }
            
            System.out.println();
        }
    }
}

第二个用于操作链表的类:

public class Linked_list {
    public static void main(String args[]) {
        Node list1 = new Node(0); // 第一个链表
        list1.add(10);
        list1.add(1);
        list1.add(15);
        list1.add(3);
        list1.add(88);
        list1.printlist(list1);

        Node ll = new Node(0); // 第二个链表
        ll.add(55);
        ll.add(44);
        ll.printlist(ll);
    }
}
英文:

My code problem is that when i run the code then list1st elements is automatically copy to the ll node

one class for implementation of linked list:

public class node {
  
	static public class Node{
		Node next;
		int data;
        Node(int d)
		{
         	this.data=d;
			this.next=null;
		}
        static Node head=null;
		public void add(int data)
		{
			 Node new_node = new Node(data); 
			    new_node.next = head; 
			    head = new_node; 
		
		}
public void printlist(Node list) {
		
			 Node currNode = list.head; 
			   
		        System.out.print("Linked List: "); 
		   
		        // Traverse through the Linked List 
		        while (currNode != null) { 
		            // Print the data at current node 
		            System.out.print(currNode.data + " "); 
		   
		            // Go to next node 
		            currNode = currNode.next; 
		        } 
		        System.out.println(" ");
		}
}
}

Second class for working on linked list:

    public class Linked_list {
    	public static void main(String args[])
    	{
    		Node list1=new Node(0);*// 1st linked list*
    		list1.add(10);
    		list1.add(1);
    		list1.add(15);
    		list1.add(3);
    		list1.add(88);
    		list1.printlist(list1);
    		Node ll=new Node(0);   **//second linked list**
    		ll.add(55);
    		ll.add(44);
    		ll.printlist(ll);
    	}
   
 }

My code problem is that when i run the code then list1st elements is automatically copy to the ll node.

答案1

得分: 1

你的代码存在一些问题。主要的bug在你的 Node 类中:

static Node head=null;

head 字段不应该是 static 的。将它改成这样:

private Node head = null;

你还需要修改其余的代码,以正确设置 head 变量:add 方法应该返回新创建的 Node 而不是 void,在 main 方法中你需要使用返回值。

为什么不能将它设为 static 呢:因为 static 会将它变成类级别的变量,被所有类的实例共享,这不是你想要的。

更多信息:Understanding Class Members

另外,printlist 方法不需要接受一个 Node 作为参数。只需要让它在调用它的 Node 上工作即可。

英文:

There are a number of things wrong with your code. The main bug is here in your class Node:

static Node head=null;

The field head must not be static. Change it to this:

private Node head = null;

You'll also need to modify the rest of the code to set the head variable appropriately: the add method should return the newly created Node instead of void, and in the main method you need to use the return value.

Why is it wrong to make it static: because static makes it a class-level variable, shared by all instances of the class, and that's not what you want.

More info: Understanding Class Members

Also, the printlist method does not need to take a Node as an argument. Just make it work on the Node you call it on.

huangapple
  • 本文由 发表于 2020年5月29日 19:59:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/62085479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定