Java 在单个类中实现单个链表

Java 在单个类中实现单个链表,java,Java,我有下面的代码,但我不理解行w.next=nextnode; 这意味着什么? 我的代码似乎很复杂,还是有最简单的方法来实现 有什么建议或想法吗 class Node1{ private String data; private Node1 nextNode=null; private Node1 lastnode=null; private Node1 next=null; public Node1() { } public

我有下面的代码,但我不理解行w.next=nextnode; 这意味着什么? 我的代码似乎很复杂,还是有最简单的方法来实现 有什么建议或想法吗

class Node1{
    private String data;
    private Node1 nextNode=null;
    private Node1 lastnode=null;
    private Node1 next=null;
    public Node1()
     {

     }
     public Node1(String r)
     {
       data=r;  
     }
     public void displayitems()
     {
         System.out.println(data);
     }
    public void insert(String data){

     Node1 w=new Node1(data);
     if(w==null)
     {
        lastnode=w;
}
else
{
 w.next=nextNode;
 nextNode=w;
}
     }   
    }

    public void display()
    {
        System.out.println("Items in list");
        Node1 current=nextNode;
      current.displayitems();
    }

    public static void main(String[] arg){
      Node1 a=new Node1();
      a.insert("deepak");
      a.insert("deep");
      a.display();


        }
    }

首先,您可以尝试实现自己的数据结构。但如果您需要实现这样的功能,您需要对数据结构(在您的情况下是单链接列表)如何存储和检索数据进行深入分析&是保持插入顺序还是保持排序顺序等等

建议您在进行测试之前始终遵循一些要点 实施

  • 首先理解概念
  • 写下算法的工作原理
  • 设计它(也许将设计写在纸上是最佳实践,它会让你对你的实现有一个清晰的认识)
  • 实现并测试它
  • 在这里的例子中,您为linkedlist设计了一个节点类,它将负责所有与节点相关的任务,如存储下一个/上一个节点地址(SingleLinkedList将只存储下一个节点地址)。将此节点保持为LinkedList类的最佳做法

    private class Node<T>{
            private Node<T> next;
            private T data;
    
            public Node(T data, Node<T> next){
                this.data = data;
                this.next = next;
            }
        }
    
    私有类节点{
    私有节点下一步;
    私有T数据;
    公共节点(T数据,下一个节点){
    这个数据=数据;
    this.next=next;
    }
    }
    
    我在这里用过。您也可以不使用泛型来完成它。在遍历这些节点对象的linkedlist类中实现linkedlist操作(如添加/删除等)。你写下的任何算法都可以帮助你在这些地方高效地实现

    我这里有我的示例代码。无论您在这里发布什么代码,都不是一个好的实现。尝试遵循我指定的步骤&您将拥有更好的代码

    public class LinkedList<T> {
    
        private Node<T> head;
        private Node<T> nextNode;
        private int size = 0;
    
        public void addFirst(T element){
            head = new Node<T>(element, head);
        }
    
        public void add(T element){
            if(head == null)
                addFirst(element);
            else{
                Node<T> node = head;
                while(node.next != null){
                    node = node.next;
                }
                node.next = new Node<T>(element, null);
            }
            size++;
        }
    
        public int size(){
            return size;
        }
    
    // Inner class somewhere here
    }
    
    公共类链接列表{
    专用节点头;
    私有节点nextNode;
    私有整数大小=0;
    公共void addFirst(T元素){
    head=新节点(元素,head);
    }
    公共无效添加(T元素){
    if(head==null)
    addFirst(元素);
    否则{
    节点=头部;
    while(node.next!=null){
    node=node.next;
    }
    node.next=新节点(元素,空);
    }
    大小++;
    }
    公共整数大小(){
    返回大小;
    }
    //在这里的某个地方
    }
    

    这是简单的表示。您可以实现Iterable和Iterator接口来循环列表。

    您是如何“拥有”代码的?你为什么不自己写呢?这是一个示例代码…我想知道发生了什么…他们叫我java初学者,但这不是一个好例子。它有几个问题。这段代码很糟糕,如果它是示例代码,那么它对于任何初学者来说都是一个非常糟糕的示例。你最好忘了它,从头重写一切。不太相关。。但是
    如果(w==null)
    有点过于谨慎了。构造函数永远不会返回null。解释清楚,谢谢SVJNU,但是代码中没有main方法。为什么在这个类中需要main方法。使用main方法创建一个单独的类,并使用此LinkedList类。