从自定义LinkedList类中移除值



这个自定义类模仿了Java的LinkedList类的功能,只是它只接受整数,显然缺乏大部分功能。对于这个方法removeAll(),我将遍历列表的每个节点,并删除具有该值的所有节点。我的问题是,当列表中的第一个节点包含要删除的值时,它会忽略包含该值的所有后续节点。有什么问题吗?我是否以错误的方式移除前节点?例如,[1]->[1]->[1]应该返回一个空列表,但它离开前节点,我得到[1]

编辑:它似乎无法删除第二个节点而不是第一个节点。

这是一个类(将ListNodes存储为列表):

public class LinkedIntList {
    private ListNode front;  // first value in the list
    // post: constructs an empty list
    public LinkedIntList() {
        front = null;
    }
    // post: removes all occurrences of a particular value
    public void removeAll(int value) {
        ListNode current = front; // primes loop
        if (current == null) { // If empty list
            return;
        }
        if (front.data == value) { // If match on first elem
            front = current.next;
            current = current.next;
        }           
        while (current.next != null) { // If next node exists
            if (current.next.data == value) { // If match at next value
                current.next = current.next.next;
            } else { // If not a match
                current = current.next; // increment to next
            }
        }
    }
    // post: appends the given value to the end of the list
    public void add(int value) {
        if (front == null) {
            front = new ListNode(value);
        } else {
            ListNode current = front;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new ListNode(value);
        }
    }
    // Sets a particular index w/ a given value 
    public void set(int index, int value) {
        ListNode current = front;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        current.data = value;
    }
} 
下面是ListNode类(负责单个"节点"):
//ListNode is a class for storing a single node of a linked
//list.  This node class is for a list of integer values.
public class ListNode {
    public int data;       // data stored in this node
    public ListNode next;  // link to next node in the list
    // post: constructs a node with data 0 and null link
    public ListNode() {
        this(0, null);
    }
    // post: constructs a node with given data and null link
    public ListNode(int data) {
        this(data, null);
    }
    // post: constructs a node with given data and given link
    public ListNode(int data, ListNode next) {
        this.data = data;
        this.next = next;
    }
}

实际上留在列表中的[1]元素是第二个元素,它在代码中成为前面的元素:

if (front.data == value) { // If match on first elem
    front = current.next;
    current = current.next;
}

之后,只需遍历列表并删除匹配的元素。用下面的代码替换有问题的代码应该可以完成以下工作:

while (front.data == value) { // If match on first elem
    front = front.next;
    if (front == null) {
        return;
    }
}

从自定义链表中删除元素的最简单、最干净的方法之一是:

import java.util.concurrent.atomic.AtomicInteger;
public class LinkedList<T> {
    class Node<T> {
        private T data;
        private Node<T> next;
        Node(T data) {
            this.data = data;
            this.next = null;
        }
    }
    private Node<T> head;
    private Node<T> tail;
    private AtomicInteger size = new AtomicInteger();
    public void add(T data) {
        Node<T> n = new Node<T>(data);
        if (head == null) {
            head = n;
            tail = n;
            size.getAndIncrement();
        } else {
            tail.next = n;
            tail = n;
            size.getAndIncrement();
        }
    }
    public int size() {
        return size.get();
    }
    public void displayElement() {
        while (head.next != null) {
            System.out.println(head.data);
            head = head.next;
        }
        System.out.println(head.data);
    }
    public void remove(int position) throws IndexOutOfBoundsException {
        if (position > size.get()) {
            throw new IndexOutOfBoundsException("current size is:" + size.get());
        }
        Node<T> temp = head;
        for (int i = 0; i < position; i++) {
            temp = temp.next;
        }
        head = null;
        head = temp;
        size.getAndDecrement();
    }
}

相关内容

  • 没有找到相关文章

最新更新