链表数据结构的java实现

链表数据结构的java实现,java,linked-list,Java,Linked List,我试图使用java实现链表数据结构,它可以很好地插入或删除第一个元素,但无法使用removeLast()方法删除最后一个元素 我的链接列表节点类: 公共类LLNode{ 字符串值; LLNode-next public LLNode(String value){ this.value = value; this.next = null; } } 包含头节点的我的链表类: public class LL { LLNode head;

我试图使用java实现链表数据结构,它可以很好地插入或删除第一个元素,但无法使用removeLast()方法删除最后一个元素

我的链接列表节点类: 公共类LLNode{ 字符串值; LLNode-next

    public LLNode(String value){
        this.value = value;
        this.next = null;
    }

}
包含头节点的我的链表类:

public class LL {
    LLNode head;

    public LL(){
        this.head = null;   
    }

    public void insertHead(LLNode input){
        input.next = head;
        this.head = input;
    }

    public void removeFirst(){
        this.head = this.head.next;
    }

    public void removeLast(){
        if (this.head == null || this.head.next == null){
            this.head = null;
            return;
        }
        LLNode current = this.head;
        LLNode tmphead = current;
        while(current.next.next != null){
            current = current.next;
        }
        current.next.next = null;
        this.head = tmphead ;
    }

    public void printAll(){
        LLNode current = this.head;

        while(current != null){
            System.out.print(current.value+" ");
            current = current.next;
        }
        System.out.println();
    }


    public static void main( String[] args){

        LL test = new LL(); 
        LL test2 = new LL();
        String[] eben = {"one","two","three","four","five","six"};

        for(int i =0;i<eben.length;i++){
            test.insertHead(new LLNode(eben[i]));
        }
        test.printAll();
        test.removeFirst();
        test.printAll();
        test.removeLast();
        test.printAll();


    }

}
即使是这样:

six five four three two one 
five four three two one 
five four three two

我的实现有什么问题?

current.next.next=null

应该是

current.next=null



当尝试删除空列表的第一个元素时,NPE的实现失败

current.next.next=null

应该是

current.next=null



当(current.next.next!=null)尝试删除空列表的第一个元素时,NPE的实现会失败。当(current.next.next!=null)执行时,NPE将继续执行,直到它为真。此时,您设置了当前的
current.next.next=null;

而(current.next.next!=null)
一直向前推进直到它为真。在这一点上,您设置了
current.next.next=null;
。它已经是了。

如果像这样的任务需要大量代码和/或变量,您可能已经将自己过度复杂化到了一个角落。下面是我将如何做
removeLast()


未经测试!使用风险自负。购买价格不退款。

如果您需要大量代码和/或变量来完成此类任务,您可能已经将自己过度复杂化到了一个角落。下面是我将如何做的
removeLast()

未经测试!使用风险自负。购买价格不退款。

试试以下方法:

public class Node {

private int data; //holds an arbitrary integer
private Node next; //reference to the next node

public Node(int d,Node n)
{
    data=d;
    next=n;
}

// these methods let us use our variables
public void setNext(Node n)
{
    next=n;
}

public void setData(int d)
{
    data=d;
}
public Node getNext()
{
    return next;
}

public int getData()
{
    return data;
}
private static Node firstNode; //a reference to the first Node
private static Node lastNode=null; //a reference to the last Node

public static void display() //displays all the data in nodes
{
    Node n=firstNode;
    while(n!=null) // loops forward displaying 
    {
        System.out.print(n.getData()+",  ");
        n=n.getNext(); //move to the next node in the list
    }
}

public static void create(int d) //inserts the node into the list
{
    Node n =new Node(d, null); // create node
    if(lastNode!=null) // if it is not the first node
    {
        lastNode.setNext(n);
        lastNode=n;
    }
    else //if n is the first node
    {
        firstNode=n;
        lastNode=n;
    }
}


 public static void removeLast(){
        if (firstNode == null || firstNode.next == null){
            firstNode = null;
            return;
        }
        Node current = firstNode;
        Node tmphead = current;
        while(current.next.next != null){
            current = current.next;
        }
        current.next = null;
        firstNode = tmphead ;
    }


 public static void removeFirst(){
     if(firstNode!=null)
     {
        firstNode= firstNode.next;
     }
    }

public static void main(String[] args) {

    create(10);
    create(20);
    create(30);
    create(40);
    create(50);
    create(60);
    display();
    System.out.println();
    removeLast();
    display();
    System.out.println();
    removeFirst();
    display();
}
}
试试这个:

public class Node {

private int data; //holds an arbitrary integer
private Node next; //reference to the next node

public Node(int d,Node n)
{
    data=d;
    next=n;
}

// these methods let us use our variables
public void setNext(Node n)
{
    next=n;
}

public void setData(int d)
{
    data=d;
}
public Node getNext()
{
    return next;
}

public int getData()
{
    return data;
}
private static Node firstNode; //a reference to the first Node
private static Node lastNode=null; //a reference to the last Node

public static void display() //displays all the data in nodes
{
    Node n=firstNode;
    while(n!=null) // loops forward displaying 
    {
        System.out.print(n.getData()+",  ");
        n=n.getNext(); //move to the next node in the list
    }
}

public static void create(int d) //inserts the node into the list
{
    Node n =new Node(d, null); // create node
    if(lastNode!=null) // if it is not the first node
    {
        lastNode.setNext(n);
        lastNode=n;
    }
    else //if n is the first node
    {
        firstNode=n;
        lastNode=n;
    }
}


 public static void removeLast(){
        if (firstNode == null || firstNode.next == null){
            firstNode = null;
            return;
        }
        Node current = firstNode;
        Node tmphead = current;
        while(current.next.next != null){
            current = current.next;
        }
        current.next = null;
        firstNode = tmphead ;
    }


 public static void removeFirst(){
     if(firstNode!=null)
     {
        firstNode= firstNode.next;
     }
    }

public static void main(String[] args) {

    create(10);
    create(20);
    create(30);
    create(40);
    create(50);
    create(60);
    display();
    System.out.println();
    removeLast();
    display();
    System.out.println();
    removeFirst();
    display();
}
}

到目前为止,卡尔·斯莫特里茨的答案是最符合逻辑的,当然其他人也很好地解释了这一点。 以下是我的解决方案:

  • 从当前节点开始,将其指向头部
  • 当当前节点的两个节点的头不为空时,将当前节点设置为下一个节点
  • 最后,将currentNode的下一个节点设置为null,换句话说,将最后一个节点设置为null/(删除它)
  • 代码

     Node currentNode = head; // start with current node and point it to head
        while (currentNode.next.next != null) // while the current next next  node is not null then set our  current node to next node
        {
            currentNode = currentNode.next;
        }
        // at the end set the Next node to the currtentnode to null
        // in other words, set the last node to null/ (remove it)
        currentNode.next = null;
    
    这是视觉效果

    currentNode     currentNode.next    currentNode.next.next
    (head)                              (last Node)                     
        +-+-+         +-+-+                  +-+-+           
        | | +-------> | | +--------------->  | | |           
        +-+-+         +-+-+                  +-+-+     
    

    到目前为止,卡尔·斯莫特里茨的答案是最符合逻辑的,当然其他人也很好地解释了这一点。 以下是我的解决方案:

  • 从当前节点开始,将其指向头部
  • 当当前节点的两个节点的头不为空时,将当前节点设置为下一个节点
  • 最后,将currentNode的下一个节点设置为null,换句话说,将最后一个节点设置为null/(删除它)
  • 代码

     Node currentNode = head; // start with current node and point it to head
        while (currentNode.next.next != null) // while the current next next  node is not null then set our  current node to next node
        {
            currentNode = currentNode.next;
        }
        // at the end set the Next node to the currtentnode to null
        // in other words, set the last node to null/ (remove it)
        currentNode.next = null;
    
    这是视觉效果

    currentNode     currentNode.next    currentNode.next.next
    (head)                              (last Node)                     
        +-+-+         +-+-+                  +-+-+           
        | | +-------> | | +--------------->  | | |           
        +-+-+         +-+-+                  +-+-+     
    

    这要么是J2ME要么是家庭作业,因为没有其他地球上的原因让你想这么做。egad,如果我回忆起1980年我的数据结构类,这个例子通过使用sentinel清理了99%。这要么是J2ME要么是家庭作业,因为没有其他地球上的原因让你想这么做。egad,如果我回忆起我的数据结构1980年的Structures类此示例使用sentinel清理了约99%。您基本上粘贴了一些代码,但没有对更改内容和原因进行任何真正的解释。答案实际上只是“这里有一些代码,试试看”不是很高的质量。如果你能对此代码提供解释,这将有助于提高答案的质量。你基本上粘贴了一些代码,但没有对更改内容和原因进行任何真正的解释。答案实际上只是“这里有一些代码,试试看”不是很高的质量。如果你能解释一下这段代码,它会在一定程度上提高答案的质量。