链表数据结构的java实现

时间:2010-01-09 18:34:41

标签: java linked-list

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

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

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

}

包含头节点的My Linked List类:

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 one 
甚至它必须是这样的:

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

我的实施有什么问题?

5 个答案:

答案 0 :(得分:2)

如果你需要很多代码和/或很多变量来完成这样的任务,你可能会把自己简化为一个角落。我将如何做removeLast()

public void removeLast() {
  // avoid special case: List is empty
  if (this.head != null) {
    if (this.head.next == null) {
      // handle special case: List has only 1 node
      this.head = null;
    } else {
      LLNode prelast = this.head; // points at node before last (eventually)
      while (prelast.next.next != null) prelast = prelast.next;
      prelast.next = null;
    }
  }
}

未经测试!使用风险由您自行承担。没有退货的购买价格。

答案 1 :(得分:1)

current.next.next = null;

应该是

current.next = null;


并且在尝试删除空List的第一个元素

时,NPE的实现失败

答案 2 :(得分:1)

while(current.next.next != null)一直前进,直到它成真为止。此时您设置current.next.next = null;。它已经是。

答案 3 :(得分:0)

到目前为止,Carl Smotricz的回答是最合乎逻辑的,当然其他人也做得很好解释。 这是我的解决方案:

  1. 从当前节点开始并将其指向头
  2. 当两个节点当前节点的头部不为空时,则将当前节点设置为下一个节点
  3. 在结尾处将currtentnode的Next节点设置为null,换句话说,将最后一个节点设置为null /(将其删除)
  4. 代码

     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)                     
        +-+-+         +-+-+                  +-+-+           
        | | +-------> | | +--------------->  | | |           
        +-+-+         +-+-+                  +-+-+     
    

答案 4 :(得分:-1)

试试这个:

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();
}
}