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

时间:2015-07-27 23:22:49

标签: java linked-list nodes

伙计们,我在从Link删除指定的LinkedList时遇到问题。例如,如果我有:

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

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

4 个答案:

答案 0 :(得分:1)

伪代码:

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
}

答案 1 :(得分:0)

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

    }
}

答案 2 :(得分:0)

在迭代它们时,您无法在列表中操作(添加,删除...项目)。你必须使用Iterator

 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

答案 3 :(得分:0)

这是一个试图使其与您的代码类似的示例

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 Node {

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 Simulate {

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

此致