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

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
}

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

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

}

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

}

和输出:123 4 123 5

2 3 4 123 5

2 3 4 5

3 4 5 6 123

3 4 5 6 123

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

不能在列表中操作(添加,删除…项目),同时迭代它们。必须使用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

相关内容

  • 没有找到相关文章

最新更新