从 `LinkedList` 中删除指定的 `Link`

Deleting a specified `Link` from `LinkedList`

各位,我在从 LinkedList 中删除指定的 Link 时遇到了问题。例如,如果我有:

"A" --> "B" --> "C"

并想删除一个 Link "B",所以结果将是:

"A" --> "C"

我在获取之前的 Link "A" 和对 "C" 的引用时遇到了问题。谁能帮我解决这个问题?方法是:

 public void delete(String data){
        if(isEmpty()){
            System.out.println("The list is empty!");
            System.exit(0);
        }
        else{
            Link current = firstLink;
            Link previous = firstLink;
            while(current != null){
                if(current.getData().equals(data)){
                    previous = current.getNext();   
                }
                else{
                    previous = current;
                    current = current.getNext();
                }
            }
        }
    }

Class Link

package LinkedList;

public class Link{

    private String data;
    private Link next;

    public Link(String data){
        this.data = data;
    }

    public void display(){
        System.out.println(data);
    }

    public String getData(){
        return this.data;
    }

    public Link getNext(){
        return this.next;
    }
}

class LinkedList{

    private Link firstLink;

    public LinkedList(){
        this.firstLink = null;
    }

    public boolean isEmpty(){
        return (this.firstLink == null);
    }

    public void insert(String data){
        Link newLink = new Link(data);
        Link newLinkNext = newLink.getNext();
        newLinkNext = firstLink;
        firstLink = newLink;
    }

    public Link deleteFirst(){
        if(isEmpty()){
            return null;
        }
        else {
            Link deletedOne = this.firstLink;
            Link nextLink = firstLink.getNext();
            firstLink = nextLink;
            return deletedOne;
        }
    }

    public void delete(String data){
    if(isEmpty()){
        System.out.println("The list is empty!");
        System.exit(0);
    }
    else{
        Link current = firstLink;
        Link previous = firstLink;
        while(current != null){
            if(current.getData().equals(data)){
                previous = current.getNext();   
            }
            else{
                previous = current;
                current = current.getNext();
            }
        }
    }
}

伪代码:

prev = null;
current = first;
while not at the end of the list
{
    if (current.data == the object I want) {
        if (prev == null) {
            first = current.next
            break
        }
        prev.next = current.next
        break;
    }
    prev = current
    current = current.next
}
class LLNode
    {
        int data;
        LLNode next;
        public LLNode(int data)
        {
            this.data=data;
        }
    }

public class DeleteNodeFromLinkedList 
{
    /* Link list node */


    /* Given a reference (pointer to pointer) to the head
    of a list and an int, push a new node on the front
    of the list. */

    public static void printList(LLNode head)
    {
       LLNode curr = head;
       while(curr != null)
       {
          System.out.print(curr.data+" ");
          curr = curr.next;
       }
    }

    static void deleteNode(LLNode DelNode)
    {
       LLNode temp = DelNode.next;
       DelNode.data = temp.data;
       DelNode.next = temp.next;

    }

    /* Drier program to test above function*/
    public static void main(String []args)
    {
        /* Start with the empty list */
        LLNode head = new LLNode(1);
        head.next = new LLNode(4);
        head.next.next = new LLNode(4);
        head.next.next.next = new LLNode(1);
        head.next.next.next.next = new LLNode(12);
        head.next.next.next.next.next = new LLNode(1);


        System.out.println(" Before deleting ");
        printList(head);

        /* I m deleting the head.next
            You can check for more cases */
       deleteNode(head.next);

       System.out.println(" \n After deleting ");
       printList(head);

    }
}

当您迭代列表时,您不能在列表中操作(添加、删除...项目)。你必须使用迭代器

 for(Iterator<EmpDedup> iter = list.iterator(); iter.hasNext();) {
     EmpDedup data = iter.next();
     if (data.getRecord() == rec1) {
         iter.remove();
     }
  }

http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html

这里有一个示例,试图使其与您的代码相似

public class LinkedList {

private Node firstNode = null;

public LinkedList() {
}

public Node getFirstNode() {
    return firstNode;
}

public void setFirstNode(Node firstNode) {
    this.firstNode = firstNode;
}

public void addNode(Node node) {
    if(firstNode == null){
        firstNode = node;
    }else{
        Node walker = firstNode;
        while(walker.getNext() != null)
            walker = walker.getNext();
        walker.setNext(node);
    }
}

public void delete(int value) {
    if (firstNode != null) {
        Node walker = firstNode;
        if (walker.getValue() == value) {
            if(walker.getNext()!=null){
                firstNode = walker.getNext();
            }else{
                setFirstNode(null);
            }
        } else {
            Node previous = walker;
            while (walker.getNext() != null) {
                previous = walker;
                walker = walker.getNext();
                if (walker.getValue() == value) {
                    previous.setNext(walker.getNext());
                    break;
                }
            }
        }

    } else {
        System.out.println("Nothing to delete");
    }
}

public void listValues (){
    if(firstNode != null){
        Node walker = firstNode;
        while(walker.getNext() != null)
        {
            System.out.println(walker.getValue());
            walker = walker.getNext();

        }
    }
}

}

public class节点{

private Node next = null;
private int value;

public Node(Node node, int value) {
    this.next = node;
    this.value = value;
}

public Node getNext() {
    return next;
}

public void setNext(Node node) {
    this.next = node;
}

public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

}

public class 模拟 {

public static void main(String[] args) {
    LinkedList list = new LinkedList();
    list.addNode(new Node(null, 1));
    list.addNode(new Node(null, 2));
    list.addNode(new Node(null, 3));
    list.addNode(new Node(null, 4));
    list.addNode(new Node(null, 123));
    list.addNode(new Node(null, 5));
    list.addNode(new Node(null, 6));
    list.listValues();


    list.delete(1);
    System.out.println("-----");
    list.listValues();

    list.delete(123);
    System.out.println("-----");
    list.listValues();


    list.addNode(new Node(null, 123));
    list.addNode(new Node(null, 5));

    list.delete(2);
    System.out.println("-----");
    list.listValues();

    list.delete(26);
    System.out.println("-----");
    list.listValues();
}

}

并输出:1 2 3 4 123 5

2 3 4 123 5

2 3 4 5

3 4 5 6 123

3 4 5 6 123

此致